Skip to content

Commit d16ce25

Browse files
ChaplinskiScott Chaplinski
and
Scott Chaplinski
authored
[11.x] SORT_NATURAL on Collection no longer throws warning for nulls (#52557)
* casting occasional nulls to strings * update * Updated test --------- Co-authored-by: Scott Chaplinski <[email protected]>
1 parent 0d7fdc3 commit d16ce25

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Illuminate/Collections/Collection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ protected function sortByMany(array $comparisons = [], int $options = SORT_REGUL
15611561
$result = match ($options) {
15621562
SORT_NUMERIC => intval($values[0]) <=> intval($values[1]),
15631563
SORT_STRING => strcmp($values[0], $values[1]),
1564-
SORT_NATURAL => strnatcmp($values[0], $values[1]),
1564+
SORT_NATURAL => strnatcmp((string) $values[0], (string) $values[1]),
15651565
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
15661566
default => $values[0] <=> $values[1],
15671567
};

tests/Support/SupportCollectionTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,32 @@ public function testSortByMany($collection)
20032003
}
20042004

20052005
#[DataProvider('collectionClassProvider')]
2006+
/**
2007+
* @dataProvider collectionClassProvider
2008+
*/
2009+
public function testNaturalSortByManyWithNull($collection)
2010+
{
2011+
$itemFoo = new \stdClass();
2012+
$itemFoo->first = 'f';
2013+
$itemFoo->second = null;
2014+
$itemBar = new \stdClass();
2015+
$itemBar->first = 'f';
2016+
$itemBar->second = 's';
2017+
2018+
$data = new $collection([$itemFoo, $itemBar]);
2019+
$data = $data->sortBy([
2020+
['first', 'desc'],
2021+
['second', 'desc'],
2022+
], SORT_NATURAL);
2023+
2024+
$this->assertEquals($itemBar, $data->first());
2025+
$this->assertEquals($itemFoo, $data->skip(1)->first());
2026+
}
2027+
2028+
#[DataProvider('collectionClassProvider')]
2029+
/**
2030+
* @dataProvider collectionClassProvider
2031+
*/
20062032
public function testSortKeys($collection)
20072033
{
20082034
$data = new $collection(['b' => 'dayle', 'a' => 'taylor']);

0 commit comments

Comments
 (0)