Skip to content

Commit 288a770

Browse files
authored
[5.x] Handle collection instances in first modifier (#11608)
1 parent 52721e0 commit 288a770

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Modifiers/CoreModifiers.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,10 @@ public function first($value, $params)
738738
return Arr::first($value);
739739
}
740740

741+
if ($value instanceof Collection) {
742+
return $value->first();
743+
}
744+
741745
return Stringy::first($value, Arr::get($params, 0));
742746
}
743747

tests/Modifiers/FirstTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ public static function arrayProvider()
4545
];
4646
}
4747

48+
#[Test]
49+
#[DataProvider('collectionProvider')]
50+
public function it_gets_the_first_value_of_a_collection($value, $expected)
51+
{
52+
$this->assertEquals($expected, $this->modify($value));
53+
}
54+
55+
public static function collectionProvider()
56+
{
57+
return [
58+
'list' => [
59+
collect(['alfa', 'bravo', 'charlie']),
60+
'alfa',
61+
],
62+
'associative' => [
63+
collect(['alfa' => 'bravo', 'charlie' => 'delta']),
64+
'bravo',
65+
],
66+
];
67+
}
68+
4869
private function modify($value, $arg = null)
4970
{
5071
return Modify::value($value)->first($arg)->fetch();

0 commit comments

Comments
 (0)