Skip to content

Commit a7970f7

Browse files
authored
Support iterables objects on data_get nested arrays (#37453)
1 parent d39c6c1 commit a7970f7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Illuminate/Collections/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function data_get($target, $key, $default = null)
5858
if ($segment === '*') {
5959
if ($target instanceof Collection) {
6060
$target = $target->all();
61-
} elseif (! is_array($target)) {
61+
} elseif (! is_iterable($target)) {
6262
return value($default);
6363
}
6464

tests/Support/SupportHelpersTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Illuminate\Tests\Support;
44

55
use ArrayAccess;
6+
use ArrayIterator;
67
use Illuminate\Contracts\Support\Htmlable;
78
use Illuminate\Support\Env;
89
use Illuminate\Support\Optional;
10+
use IteratorAggregate;
911
use LogicException;
1012
use Mockery as m;
1113
use PHPUnit\Framework\TestCase;
@@ -87,10 +89,18 @@ public function testDataGetWithNestedArrays()
8789
['name' => 'abigail'],
8890
['name' => 'dayle'],
8991
];
92+
$arrayIterable = new SupportTestArrayIterable([
93+
['name' => 'taylor', 'email' => 'taylorotwell@gmail.com'],
94+
['name' => 'abigail'],
95+
['name' => 'dayle'],
96+
]);
9097

9198
$this->assertEquals(['taylor', 'abigail', 'dayle'], data_get($array, '*.name'));
9299
$this->assertEquals(['taylorotwell@gmail.com', null, null], data_get($array, '*.email', 'irrelevant'));
93100

101+
$this->assertEquals(['taylor', 'abigail', 'dayle'], data_get($arrayIterable, '*.name'));
102+
$this->assertEquals(['taylorotwell@gmail.com', null, null], data_get($arrayIterable, '*.email', 'irrelevant'));
103+
94104
$array = [
95105
'users' => [
96106
['first' => 'taylor', 'last' => 'otwell', 'email' => 'taylorotwell@gmail.com'],
@@ -792,3 +802,18 @@ public function offsetUnset($offset)
792802
unset($this->attributes[$offset]);
793803
}
794804
}
805+
806+
class SupportTestArrayIterable implements IteratorAggregate
807+
{
808+
protected $items = [];
809+
810+
public function __construct($items = [])
811+
{
812+
$this->items = $items;
813+
}
814+
815+
public function getIterator()
816+
{
817+
return new ArrayIterator($this->items);
818+
}
819+
}

0 commit comments

Comments
 (0)