@@ -136,15 +136,12 @@ public function offsetExists($offset)
136
136
{
137
137
if (is_string ($ offset ))
138
138
{
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 )))
146
143
{
147
- $ childKey = substr ($ offset , 0 , $ slashPos );
144
+ $ childKey = $ this -> _prefix . substr ($ subPath , 0 , strpos ( $ subPath , ' / ' ) + 1 );
148
145
if (isset ($ this ->_children [$ childKey ]))
149
146
return isset ($ this ->_children [$ childKey ][$ offset ]);
150
147
}
@@ -163,15 +160,11 @@ public function offsetGet($offset)
163
160
{
164
161
if (is_string ($ offset ))
165
162
{
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 )) )
169
166
{
170
- $ offset = $ subPath ;
171
- }
172
- else
173
- {
174
- $ childKey = substr ($ subPath , 0 , $ slashPos );
167
+ $ childKey = $ this ->_prefix .substr ($ subPath , 0 , strpos ($ subPath , '/ ' ) + 1 );
175
168
if (isset ($ this [$ childKey ]))
176
169
return $ this ->_children [$ childKey ][$ offset ];
177
170
}
@@ -199,23 +192,28 @@ public function offsetGet($offset)
199
192
*/
200
193
public function offsetSet ($ offset , $ value )
201
194
{
202
- switch ( gettype ($ offset ))
195
+ if ( ' string ' === gettype ($ offset ))
203
196
{
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 , '/ ' );
214
199
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 ;
219
217
}
220
218
}
221
219
@@ -267,9 +265,30 @@ public function jsonSerialize()
267
265
$ json [$ this ->_prefix ] = $ child ;
268
266
else if ($ child instanceof KVPair)
269
267
$ json [$ this ->_prefix ][$ child ->Key ] = $ child ;
270
- else
271
- $ json [$ this ->_prefix ][$ k ] = $ child ;
272
268
}
273
269
return $ json ;
274
270
}
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
+ }
275
294
}
0 commit comments