Skip to content

Commit 48fc6b3

Browse files
author
Pantea Marius-ciclistu
committed
Retroactive fix port Maravel-Framework 10.51.18 feature-fix for php/php-src#20262 (comment) and laravel/framework#57528
1 parent 6dbbf61 commit 48fc6b3

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/Helpers/GeneralHelper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,54 @@ public static function isDebug(): bool
5959
&& \function_exists('request')
6060
&& null !== \request('sqlDebug');
6161
}
62+
63+
/**
64+
* Backward compatible array_unique($items, SORT_REGULAR) fix
65+
* see https://github.com/php/php-src/issues/20262#issuecomment-3441217772
66+
* see https://github.com/laravel/framework/pull/57501#issuecomment-3441380946
67+
* see https://github.com/laravel/framework/issues/57528
68+
*/
69+
public static function arrayUniqueSortRegular(array $items): array
70+
{
71+
$ui = $us = $u = $sp = [];
72+
73+
foreach ($items as $k => $v) {
74+
if (\is_string($v)) {
75+
$us[$v] ??= $k;
76+
77+
continue;
78+
}
79+
80+
if (\is_int($v)) {
81+
$ui[$v] ??= $k;
82+
83+
continue;
84+
}
85+
86+
if (null === $v) {
87+
$sp['null'] ??= $k;
88+
89+
continue;
90+
}
91+
92+
if (false === $v) {
93+
$sp['false'] ??= $k;
94+
95+
continue;
96+
}
97+
98+
if (true === $v) {
99+
$sp['true'] ??= $k;
100+
101+
continue;
102+
}
103+
104+
$u[$k] = $v;
105+
}
106+
107+
return \array_intersect_key(
108+
$items,
109+
\array_flip($ui) + \array_flip($us) + \array_flip($sp) + \array_unique($u, SORT_REGULAR)
110+
);
111+
}
62112
}

src/Models/BaseModel.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,67 @@ public function __call($method, $parameters)
492492

493493
return parent::__call($method, $parameters);
494494
}
495+
496+
/**
497+
* @inheritdoc
498+
*/
499+
public function newCollection(array $models = []): \Illuminate\Database\Eloquent\Collection
500+
{
501+
return \function_exists('arrayUniqueSortRegular')
502+
&& \Composer\InstalledVersions::isInstalled('macropay-solutions/maravel-framework') ?
503+
parent::newCollection($models) :
504+
new class ($models) extends \Illuminate\Database\Eloquent\Collection {
505+
/**
506+
* @inheritdoc
507+
*/
508+
public function toBase(): Collection
509+
{
510+
return new class ($this) extends Collection {
511+
/**
512+
* Return only unique items from the collection array.
513+
*
514+
* @param mixed $key
515+
* @param bool $strict
516+
* @param int $flags [optional] <p>
517+
* The optional second parameter sort_flags
518+
* may be used to modify the sorting behavior using these values:
519+
* </p>
520+
* <p>
521+
* Sorting type flags:
522+
* </p><ul>
523+
* <li>
524+
* <b>SORT_REGULAR</b> - compare items normally
525+
* (don't change types)
526+
* </li>
527+
* <li>
528+
* <b>SORT_NUMERIC</b> - compare items numerically
529+
* </li>
530+
* <li>
531+
* <b>SORT_STRING</b> - compare items as strings
532+
* </li>
533+
* <li>
534+
* <b>SORT_LOCALE_STRING</b> - compare items as strings,
535+
* based on the current locale
536+
* </li>
537+
* </ul>
538+
* @return static
539+
*/
540+
public function unique($key = null, $strict = false): Collection
541+
{
542+
if ($key === null && $strict === false) {
543+
$flags = \func_get_args()[2] ?? SORT_REGULAR;
544+
545+
return new static(
546+
SORT_REGULAR === $flags ?
547+
GeneralHelper::arrayUniqueSortRegular($this->items) :
548+
\array_unique($this->items, $flags)
549+
);
550+
}
551+
552+
return parent::unique($key, $strict);
553+
}
554+
};
555+
}
556+
};
557+
}
495558
}

0 commit comments

Comments
 (0)