@@ -136,15 +136,12 @@ public function offsetExists($offset)
136136 {
137137 if (is_string ($ offset ))
138138 {
139- $ subPath = trim (str_replace ($ this ->_prefix , '' , $ offset ), "/ " );
140- $ slashPos = strpos ($ subPath , '/ ' );
141- if (false === $ slashPos )
142- {
143- $ offset = $ subPath ;
144- }
145- else
139+ $ subPath = str_replace ($ this ->_prefix , '' , $ offset );
140+ $ cnt = substr_count ($ subPath , '/ ' );
141+
142+ if (1 < $ cnt || (1 === $ cnt && '/ ' !== substr ($ subPath , -1 )))
146143 {
147- $ childKey = substr ($ offset , 0 , $ slashPos );
144+ $ childKey = $ this -> _prefix . substr ($ subPath , 0 , strpos ( $ subPath , ' / ' ) + 1 );
148145 if (isset ($ this ->_children [$ childKey ]))
149146 return isset ($ this ->_children [$ childKey ][$ offset ]);
150147 }
@@ -163,15 +160,11 @@ public function offsetGet($offset)
163160 {
164161 if (is_string ($ offset ))
165162 {
166- $ subPath = trim ( str_replace ($ this ->_prefix , '' , $ offset), " / " );
167- $ slashPos = strpos ($ subPath , '/ ' );
168- if (false === $ slashPos )
163+ $ subPath = str_replace ($ this ->_prefix , '' , $ offset );
164+ $ cnt = substr_count ($ subPath , '/ ' );
165+ if (1 < $ cnt || ( 1 === $ cnt && ' / ' !== substr ( $ subPath , - 1 )) )
169166 {
170- $ offset = $ subPath ;
171- }
172- else
173- {
174- $ childKey = substr ($ subPath , 0 , $ slashPos );
167+ $ childKey = $ this ->_prefix .substr ($ subPath , 0 , strpos ($ subPath , '/ ' ) + 1 );
175168 if (isset ($ this [$ childKey ]))
176169 return $ this ->_children [$ childKey ][$ offset ];
177170 }
@@ -199,23 +192,28 @@ public function offsetGet($offset)
199192 */
200193 public function offsetSet ($ offset , $ value )
201194 {
202- switch ( gettype ($ offset ))
195+ if ( ' string ' === gettype ($ offset ))
203196 {
204- case 'NULL ' :
205- $ this ->_children [] = $ value ;
206- break ;
207- case 'integer ' :
208- case 'double ' :
209- $ this ->_children [$ offset ] = $ value ;
210- break ;
211- case 'string ' :
212- $ childPath = trim (str_replace ($ this ->_prefix , '' , $ offset ), "/ " );
213- $ slashPos = strpos ($ childPath , '/ ' );
197+ $ subPath = str_replace ($ this ->_prefix , '' , $ offset );
198+ $ cnt = substr_count ($ subPath , '/ ' );
214199
215- if (false === $ slashPos )
216- $ this ->_children [$ childPath ] = $ value ;
217- else
218- $ this ->_children [substr ($ childPath , 0 , $ slashPos )][$ offset ] = $ value ;
200+ if (1 < $ cnt || (1 === $ cnt && '/ ' !== substr ($ subPath , -1 )))
201+ {
202+ $ childKey = $ this ->_prefix .substr ($ subPath , 0 , strpos ($ subPath , '/ ' ) + 1 );
203+ $ this ->_children [$ childKey ][$ offset ] = $ value ;
204+ }
205+ else
206+ {
207+ $ this ->_children [$ offset ] = $ value ;
208+ }
209+ }
210+ else if (null === $ offset )
211+ {
212+ $ this ->_children [] = $ value ;
213+ }
214+ else
215+ {
216+ $ this ->_children [$ offset ] = $ value ;
219217 }
220218 }
221219
@@ -267,9 +265,30 @@ public function jsonSerialize()
267265 $ json [$ this ->_prefix ] = $ child ;
268266 else if ($ child instanceof KVPair)
269267 $ json [$ this ->_prefix ][$ child ->Key ] = $ child ;
270- else
271- $ json [$ this ->_prefix ][$ k ] = $ child ;
272268 }
273269 return $ json ;
274270 }
271+
272+ /**
273+ * @return string
274+ */
275+ public function __toString ()
276+ {
277+ return $ this ->_prefix ;
278+ }
279+
280+ /**
281+ * This method is called by var_dump() when dumping an object to get the properties that should be shown.
282+ * If the method isn't defined on an object, then all public, protected and private properties will be shown.
283+ *
284+ * @return array
285+ * @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
286+ */
287+ public function __debugInfo ()
288+ {
289+ return array (
290+ 'prefix ' => $ this ->_prefix ,
291+ 'children ' => $ this ->_children
292+ );
293+ }
275294}
0 commit comments