@@ -114,7 +114,7 @@ public static function setAndGet(&$collection, $key, $default = null)
114
114
public static function remove ($ collection , $ key )
115
115
{
116
116
// Recursive call
117
- if (is_array ($ key )) {
117
+ if (static :: isArrayLike ($ key )) {
118
118
foreach ($ key as $ k ) {
119
119
static ::internalRemove ($ collection , $ k );
120
120
}
@@ -137,7 +137,7 @@ public static function pluck($collection, $property)
137
137
}, (array ) $ collection );
138
138
139
139
// Convert back to object if necessary
140
- if (is_object ($ collection )) {
140
+ if (static :: isNonArrayAccessObject ($ collection )) {
141
141
$ plucked = (object ) $ plucked ;
142
142
}
143
143
@@ -199,7 +199,7 @@ public static function filterBy($collection, $property, $value, $comparisonOp =
199
199
200
200
return $ ops [$ comparisonOp ]($ item , $ property , $ value );
201
201
}));
202
- if (is_object ($ collection )) {
202
+ if (static :: isNonArrayAccessObject ($ collection )) {
203
203
$ result = (object ) $ result ;
204
204
}
205
205
@@ -300,6 +300,16 @@ public static function group($collection, $grouper, $saveKeys = false)
300
300
return $ result ;
301
301
}
302
302
303
+ protected static function isNonArrayAccessObject ($ collection )
304
+ {
305
+ return is_object ($ collection ) && !($ collection instanceof \ArrayAccess);
306
+ }
307
+
308
+ protected static function isArrayLike ($ collection )
309
+ {
310
+ return is_array ($ collection ) || $ collection instanceof \ArrayAccess;
311
+ }
312
+
303
313
////////////////////////////////////////////////////////////////////
304
314
////////////////////////////// HELPERS /////////////////////////////
305
315
////////////////////////////////////////////////////////////////////
@@ -321,10 +331,10 @@ protected static function internalSet(&$collection, $key, $value)
321
331
$ key = array_shift ($ keys );
322
332
323
333
// If we're dealing with an object
324
- if (is_object ($ collection )) {
334
+ if (static :: isNonArrayAccessObject ($ collection )) {
325
335
$ collection ->$ key = static ::get ($ collection , $ key , []);
326
336
$ collection = &$ collection ->$ key ;
327
- // If we're dealing with an array
337
+ // If we're dealing with an array
328
338
} else {
329
339
$ collection [$ key ] = static ::get ($ collection , $ key , []);
330
340
$ collection = &$ collection [$ key ];
@@ -333,7 +343,7 @@ protected static function internalSet(&$collection, $key, $value)
333
343
334
344
// Bind final tree on the collection
335
345
$ key = array_shift ($ keys );
336
- if (is_array ($ collection )) {
346
+ if (static :: isArrayLike ($ collection )) {
337
347
$ collection [$ key ] = $ value ;
338
348
} else {
339
349
$ collection ->$ key = $ value ;
@@ -357,16 +367,16 @@ protected static function internalRemove(&$collection, $key)
357
367
}
358
368
359
369
// If we're dealing with an object
360
- if (is_object ($ collection )) {
370
+ if (static :: isNonArrayAccessObject ($ collection )) {
361
371
$ collection = &$ collection ->$ key ;
362
- // If we're dealing with an array
372
+ // If we're dealing with an array
363
373
} else {
364
374
$ collection = &$ collection [$ key ];
365
375
}
366
376
}
367
377
368
378
$ key = array_shift ($ keys );
369
- if (is_object ($ collection )) {
379
+ if (static :: isNonArrayAccessObject ($ collection )) {
370
380
unset($ collection ->$ key );
371
381
} else {
372
382
unset($ collection [$ key ]);
0 commit comments