Skip to content

Commit 5574cd9

Browse files
author
Pantea Marius-ciclistu
committed
1 parent 70f23b7 commit 5574cd9

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

illuminate/Collections/Collection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,11 @@ public function undot()
16881688
public function unique($key = null, $strict = false)
16891689
{
16901690
if ($key === null && $strict === false) {
1691-
return new static(\array_unique($this->items, \func_get_args()[2] ?? SORT_REGULAR));
1691+
$flags = \func_get_args()[2] ?? SORT_REGULAR;
1692+
1693+
return new static(
1694+
SORT_REGULAR === $flags ? \arrayUniqueSortRegular($this->items) : \array_unique($this->items, $flags)
1695+
);
16921696
}
16931697

16941698
$callback = $this->valueRetriever($key);

illuminate/Foundation/Support/Providers/EventServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function register()
4040
$events = $this->getEvents();
4141

4242
foreach ($events as $event => $listeners) {
43-
foreach (array_unique($listeners, SORT_REGULAR) as $listener) {
43+
foreach (\arrayUniqueSortRegular($listeners) as $listener) {
4444
Event::listen($event, $listener);
4545
}
4646
}

illuminate/Foundation/helpers.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,57 @@ function view($view = null, $data = [], $mergeData = [])
10291029
return $factory->make($view, $data, $mergeData);
10301030
}
10311031
}
1032+
1033+
if (!\function_exists('arrayUniqueSortRegular')) {
1034+
/**
1035+
* Backward compatible array_unique($items, SORT_REGULAR) fix
1036+
* see https://github.com/php/php-src/issues/20262#issuecomment-3441217772
1037+
* see https://github.com/laravel/framework/pull/57501#issuecomment-3441380946
1038+
*/
1039+
function arrayUniqueSortRegular(array $items): array
1040+
{
1041+
$ui = $us = $u = $sp = [];
1042+
1043+
foreach ($items as $k => $v) {
1044+
if (\is_string($v)) {
1045+
$us[$v] ??= $k;
1046+
1047+
continue;
1048+
}
1049+
1050+
if (\is_int($v)) {
1051+
$ui[$v] ??= $k;
1052+
1053+
continue;
1054+
}
1055+
1056+
if (null === $v) {
1057+
$sp['null'] ??= $k;
1058+
1059+
continue;
1060+
}
1061+
1062+
if (false === $v) {
1063+
$sp['false'] ??= $k;
1064+
1065+
continue;
1066+
}
1067+
1068+
if (true === $v) {
1069+
$sp['true'] ??= $k;
1070+
1071+
continue;
1072+
}
1073+
1074+
$u[$k] = $v;
1075+
}
1076+
1077+
return \array_intersect_key(
1078+
$items,
1079+
\array_flip($ui) +
1080+
\array_flip($us) +
1081+
\array_flip($sp) +
1082+
\array_unique($u, SORT_REGULAR)
1083+
);
1084+
}
1085+
}

src/helpers.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,57 @@ function view($view = null, $data = [], $mergeData = [])
433433
return $factory->make($view, $data, $mergeData);
434434
}
435435
}
436+
437+
if (!\function_exists('arrayUniqueSortRegular')) {
438+
/**
439+
* Backward compatible array_unique($items, SORT_REGULAR) fix
440+
* see https://github.com/php/php-src/issues/20262#issuecomment-3441217772
441+
* see https://github.com/laravel/framework/pull/57501#issuecomment-3441380946
442+
*/
443+
function arrayUniqueSortRegular(array $items): array
444+
{
445+
$ui = $us = $u = $sp = [];
446+
447+
foreach ($items as $k => $v) {
448+
if (\is_string($v)) {
449+
$us[$v] ??= $k;
450+
451+
continue;
452+
}
453+
454+
if (\is_int($v)) {
455+
$ui[$v] ??= $k;
456+
457+
continue;
458+
}
459+
460+
if (null === $v) {
461+
$sp['null'] ??= $k;
462+
463+
continue;
464+
}
465+
466+
if (false === $v) {
467+
$sp['false'] ??= $k;
468+
469+
continue;
470+
}
471+
472+
if (true === $v) {
473+
$sp['true'] ??= $k;
474+
475+
continue;
476+
}
477+
478+
$u[$k] = $v;
479+
}
480+
481+
return \array_intersect_key(
482+
$items,
483+
\array_flip($ui) +
484+
\array_flip($us) +
485+
\array_flip($sp) +
486+
\array_unique($u, SORT_REGULAR)
487+
);
488+
}
489+
}

0 commit comments

Comments
 (0)