Skip to content

Commit

Permalink
[11.x] SORT_NATURAL on Collection no longer throws warning for nulls (#…
Browse files Browse the repository at this point in the history
…52557)

* casting occasional nulls to strings

* update

* Updated test

---------

Co-authored-by: Scott Chaplinski <scott@scotts-mbp.lan>
  • Loading branch information
Chaplinski and Scott Chaplinski authored Aug 23, 2024
1 parent 0d7fdc3 commit d16ce25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ protected function sortByMany(array $comparisons = [], int $options = SORT_REGUL
$result = match ($options) {
SORT_NUMERIC => intval($values[0]) <=> intval($values[1]),
SORT_STRING => strcmp($values[0], $values[1]),
SORT_NATURAL => strnatcmp($values[0], $values[1]),
SORT_NATURAL => strnatcmp((string) $values[0], (string) $values[1]),
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
default => $values[0] <=> $values[1],
};
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,32 @@ public function testSortByMany($collection)
}

#[DataProvider('collectionClassProvider')]
/**
* @dataProvider collectionClassProvider
*/
public function testNaturalSortByManyWithNull($collection)
{
$itemFoo = new \stdClass();
$itemFoo->first = 'f';
$itemFoo->second = null;
$itemBar = new \stdClass();
$itemBar->first = 'f';
$itemBar->second = 's';

$data = new $collection([$itemFoo, $itemBar]);
$data = $data->sortBy([
['first', 'desc'],
['second', 'desc'],
], SORT_NATURAL);

$this->assertEquals($itemBar, $data->first());
$this->assertEquals($itemFoo, $data->skip(1)->first());
}

#[DataProvider('collectionClassProvider')]
/**
* @dataProvider collectionClassProvider
*/
public function testSortKeys($collection)
{
$data = new $collection(['b' => 'dayle', 'a' => 'taylor']);
Expand Down

0 comments on commit d16ce25

Please sign in to comment.