Skip to content

Commit 263ea42

Browse files
authored
[12.x] Fix Arr::first for ArrayObject and AsArrayObject values (#57969)
* Fix Arr::first for ArrayObject and other array-like values * Update Arr::first docblock to mention iterable
1 parent 8064d95 commit 263ea42

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public static function exists($array, $key)
242242
}
243243

244244
/**
245-
* Return the first element in an array passing a given truth test.
245+
* Return the first element in an iterable passing a given truth test.
246246
*
247247
* @template TKey
248248
* @template TValue
@@ -271,6 +271,8 @@ public static function first($array, ?callable $callback = null, $default = null
271271
return value($default);
272272
}
273273

274+
$array = static::from($array);
275+
274276
$key = array_find_key($array, $callback);
275277

276278
return $key !== null ? $array[$key] : value($default);

tests/Support/SupportArrTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ public function testFirst()
376376
$this->assertNull(Arr::first($cursor));
377377
}
378378

379+
public function testFirstWorksWithArrayObject()
380+
{
381+
$arrayObject = new ArrayObject([0, 10, 20]);
382+
383+
$result = Arr::first($arrayObject, fn ($value) => $value === 0);
384+
385+
$this->assertSame(0, $result);
386+
}
387+
379388
public function testJoin()
380389
{
381390
$this->assertSame('a, b, c', Arr::join(['a', 'b', 'c'], ', '));

0 commit comments

Comments
 (0)