Skip to content

Commit 8432caa

Browse files
JonKaricjasonvargaclaude
authored
[6.x] Add support for Collection in last modifier (#14778)
Co-authored-by: Jason Varga <jason@pixelfear.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 68b4d77 commit 8432caa

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/Modifiers/CoreModifiers.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,16 +1526,20 @@ public function keys($value)
15261526
}
15271527

15281528
/**
1529-
* Returns the last $params[0] characters of a string, or the last element of an array.
1529+
* Returns the last $params[0] characters of a string, or the last element of an array or Collection.
15301530
*
1531-
* @return string
1531+
* @return mixed
15321532
*/
15331533
public function last($value, $params)
15341534
{
15351535
if (is_array($value)) {
15361536
return Arr::last($value);
15371537
}
15381538

1539+
if ($value instanceof Collection) {
1540+
return $value->last();
1541+
}
1542+
15391543
return Stringy::last($value, Arr::get($params, 0));
15401544
}
15411545

tests/Modifiers/LastTest.php

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

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

0 commit comments

Comments
 (0)