11<?php
2- class MyArrayObject implements IteratorAggregate , ArrayAccess, Countable
2+ class MyArrayObject implements Iterator , ArrayAccess, Countable
33 {
44 protected $ _array ;
55
@@ -8,28 +8,70 @@ public function __construct($input = array())
88 $ this ->_array = (array ) $ input ;
99 }
1010
11- public function merge (array $ input )
11+ public function merge ($ input )
1212 {
13- // /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
13+ $ input = $ this -> _toArray ( $ input );
1414 $ this ->_array = array_merge ($ this ->_array , $ input );
1515 return $ this ;
1616 }
1717
18- public function merge_recursive (array $ input )
18+ public function merge_recursive ($ input )
1919 {
20- // /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
20+ $ input = $ this -> _toArray ( $ input );
2121 $ this ->_array = array_merge_recursive ($ this ->_array , $ input );
2222 return $ this ;
2323 }
2424
25+ public function replace ($ input )
26+ {
27+ $ input = $ this ->_toArray ($ input );
28+ $ this ->_array = array_replace ($ this ->_array , $ input );
29+ return $ this ;
30+ }
31+
32+ public function replace_recursive ($ input )
33+ {
34+ $ input = $ this ->_toArray ($ input );
35+ $ this ->_array = array_replace_recursive ($ this ->_array , $ input );
36+ return $ this ;
37+ }
38+
39+ public function keys ()
40+ {
41+ return array_keys ($ this ->_array );
42+ }
43+
2544 public function key_exists ($ key )
2645 {
2746 return array_key_exists ($ key , $ this ->_array );
2847 }
2948
30- public function getIterator ()
49+ public function rewind ()
50+ {
51+ $ return = reset ($ this ->_array );
52+ return $ this ->_format ($ return );
53+ }
54+
55+ public function current ()
3156 {
32- return new ArrayIterator ($ this ->_array );
57+ $ return = current ($ this ->_array );
58+ return $ this ->_format ($ return );
59+ }
60+
61+ public function key ()
62+ {
63+ return key ($ this ->_array );
64+ }
65+
66+ public function next ()
67+ {
68+ $ return = next ($ this ->_array );
69+ return $ this ->_format ($ return );
70+ }
71+
72+ public function valid ()
73+ {
74+ return (key ($ this ->_array ) !== null );
3375 }
3476
3577 public function offsetSet ($ offset , $ value )
@@ -66,9 +108,65 @@ public function count()
66108 return count ($ this ->_array );
67109 }
68110
111+ public function toArray ()
112+ {
113+ return $ this ->_toArray ($ this ->_array , true );
114+ }
115+
116+ public function toObject ()
117+ {
118+ $ array = $ this ->_toArray ($ this ->_array , true );
119+ return new ArrayObject ($ array , ArrayObject::ARRAY_AS_PROPS );
120+ }
121+
122+ protected function _toArray ($ input , $ recursive = false )
123+ {
124+ /**
125+ * Ne pas utiliser de référence foreach($input as &$array) sinon:
126+ * Uncaught Error: An iterator cannot be used with foreach by reference
127+ */
128+ foreach ($ input as $ key => $ array )
129+ {
130+ if (is_array ($ array ) || $ array instanceof MyArrayObject || $ array instanceof ArrayObject) {
131+ $ input [$ key ] = $ this ->_toArray ($ array );
132+ }
133+ }
134+
135+ if (is_array ($ input )) {
136+ return $ input ;
137+ }
138+ elseif (is_object ($ input ))
139+ {
140+ // /!\ If get_class() is called with anything other than an object, an E_WARNING level error is raised
141+ switch (get_class ($ input ))
142+ {
143+ //case get_class(): {
144+ case static ::class: {
145+ return $ input ->toArray ();
146+ }
147+ case 'ArrayObject ' : {
148+ return $ input ->getArrayCopy ();
149+ }
150+ }
151+ }
152+
153+ throw new Exception ('Argument 1 passed must be of the type array or ArrayObject ' , E_USER_ERROR );
154+ }
155+
156+ protected function _format ($ data )
157+ {
158+ if (is_array ($ data )) {
159+ $ className = static ::class;
160+ return new $ className ($ data );
161+ }
162+ else {
163+ return $ data ;
164+ }
165+ }
166+
69167 public function __isset ($ name )
70168 {
71- return isset ( $ this ->_array [ $ name ] );
169+ return array_key_exists ( $ name , $ this ->_array );
72170 }
73171
74172 public function __unset ($ name )
@@ -78,18 +176,16 @@ public function __unset($name)
78176
79177 public function __get ($ name )
80178 {
81- if (array_key_exists ($ name , $ this ->_array ))
82- {
83- if (is_array ($ this ->_array [$ name ])) {
84- $ className = static ::class;
85- return new $ className ($ this ->_array [$ name ]);
86- }
87- else {
88- return $ this ->_array [$ name ];
89- }
179+ if (isset ($ this ->{$ name })) {
180+ return $ this ->_format ($ this ->_array [$ name ]);
90181 }
91182 else {
92183 throw new Exception ("This attribute " .$ name ." does not exist " , E_USER_ERROR );
93184 }
94185 }
186+
187+ public function debug ()
188+ {
189+ return $ this ->toArray ();
190+ }
95191 }
0 commit comments