Allow PHP-8.2 and up Compatibility instead of just PHP-8.4

This commit is contained in:
johnnyq
2026-06-12 17:06:10 -04:00
parent 2204bd52f4
commit d3a93652f3
220 changed files with 7198 additions and 2635 deletions

View File

@@ -95,7 +95,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
return;
}
$middle = (int) ($count / 2);
$middle = intdiv($count, 2);
if ($count % 2) {
return $values->get($middle);
@@ -473,12 +473,14 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*
* @template TGetDefault
*
* @param TKey $key
* @param TKey|null $key
* @param TGetDefault|(\Closure(): TGetDefault) $default
* @return TValue|TGetDefault
*/
public function get($key, $default = null)
{
$key ??= '';
if (array_key_exists($key, $this->items)) {
return $this->items[$key];
}
@@ -497,8 +499,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*/
public function getOrPut($key, $value)
{
if (array_key_exists($key, $this->items)) {
return $this->items[$key];
if (array_key_exists($key ?? '', $this->items)) {
return $this->items[$key ?? ''];
}
$this->offsetSet($key, $value = value($value));
@@ -509,11 +511,16 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Group an associative array by a field or using a callback.
*
* @template TGroupKey of array-key
* @template TGroupKey of array-key|\UnitEnum|\Stringable
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>>
* @return static<
* ($groupBy is (array|string)
* ? array-key
* : (TGroupKey is \UnitEnum ? array-key : (TGroupKey is \Stringable ? string : TGroupKey))),
* static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>
* >
*/
public function groupBy($groupBy, $preserveKeys = false)
{
@@ -538,7 +545,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
$groupKey = match (true) {
is_bool($groupKey) => (int) $groupKey,
$groupKey instanceof \UnitEnum => enum_value($groupKey),
$groupKey instanceof \Stringable => (string) $groupKey,
$groupKey instanceof \Stringable, is_null($groupKey) => (string) $groupKey,
default => $groupKey,
};
@@ -562,10 +569,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Key an associative array by a field or using a callback.
*
* @template TNewKey of array-key
* @template TNewKey of array-key|\UnitEnum
*
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
* @return static<($keyBy is string ? array-key : ($keyBy is array ? array-key : TNewKey)), TValue>
* @return static<($keyBy is (array|string) ? array-key : (TNewKey is \UnitEnum ? array-key : TNewKey)), TValue>
*/
public function keyBy($keyBy)
{
@@ -600,7 +607,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
{
$keys = is_array($key) ? $key : func_get_args();
return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
return array_all($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
}
/**
@@ -617,7 +624,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
$keys = is_array($key) ? $key : func_get_args();
return array_any($keys, fn ($key) => array_key_exists($key, $this->items));
return array_any($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
}
/**
@@ -722,14 +729,25 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*
* @param (callable(TValue, TKey): bool)|null $callback
* @return bool
*
* @deprecated 12.49.0 Use the `hasSole()` method instead.
*/
public function containsOneItem(?callable $callback = null): bool
{
if ($callback) {
return $this->filter($callback)->count() === 1;
}
return $this->hasSole($callback);
}
return $this->count() === 1;
/**
* Determine if the collection contains multiple items.
*
* @param (callable(TValue, TKey): bool)|null $callback
* @return bool
*
* @deprecated 12.50.0 Use the `hasMany()` method instead.
*/
public function containsManyItems(?callable $callback = null): bool
{
return $this->hasMany($callback);
}
/**
@@ -737,7 +755,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*
* @param string $glue
* @param string $finalGlue
* @return string
* @return TValue|string
*/
public function join($glue, $finalGlue = '')
{
@@ -789,8 +807,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Get the values of a given key.
*
* @param string|int|array<array-key, string>|null $value
* @param string|null $key
* @param \Closure|string|int|array<array-key, string>|null $value
* @param \Closure|string|null $key
* @return static<array-key, mixed>
*/
public function pluck($value, $key = null)
@@ -862,8 +880,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Merge the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* @template TMergeValue
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeValue>|iterable<TKey, TMergeValue> $items
* @return static<TKey, TValue|TMergeValue>
*/
public function merge($items)
{
@@ -929,10 +949,16 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*
* @param int $step
* @param int $offset
* @return static
* @return ($step is positive-int ? static : never)
*
* @throws \InvalidArgumentException
*/
public function nth($step, $offset = 0)
{
if ($step < 1) {
throw new InvalidArgumentException('Step value must be at least 1.');
}
$new = [];
$position = 0;
@@ -1030,7 +1056,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
*/
public function prepend($value, $key = null)
{
$this->items = Arr::prepend($this->items, ...func_get_args());
$this->items = Arr::prepend($this->items, ...(func_num_args() > 1 ? func_get_args() : [$value]));
return $this;
}
@@ -1279,12 +1305,20 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Create chunks representing a "sliding window" view of the items in the collection.
*
* @param int $size
* @param int $step
* @param positive-int $size
* @param positive-int $step
* @return static<int, static>
*
* @throws \InvalidArgumentException
*/
public function sliding($size = 2, $step = 1)
{
if ($size < 1) {
throw new InvalidArgumentException('Size value must be at least 1.');
} elseif ($step < 1) {
throw new InvalidArgumentException('Step value must be at least 1.');
}
$chunks = floor(($this->count() - $size) / $step) + 1;
return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size));
@@ -1339,10 +1373,16 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
* Split a collection into a certain number of groups.
*
* @param int $numberOfGroups
* @return static<int, static>
* @return ($numberOfGroups is positive-int ? static<int, static> : never)
*
* @throws \InvalidArgumentException
*/
public function split($numberOfGroups)
{
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
if ($this->isEmpty()) {
return new static;
}
@@ -1376,17 +1416,23 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
* Split a collection into a certain number of groups, and fill the first groups completely.
*
* @param int $numberOfGroups
* @return static<int, static>
* @return ($numberOfGroups is positive-int ? static<int, static> : never)
*
* @throws \InvalidArgumentException
*/
public function splitIn($numberOfGroups)
{
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
return $this->chunk((int) ceil($this->count() / $numberOfGroups));
}
/**
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
*
* @param (callable(TValue, TKey): bool)|string $key
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return TValue
@@ -1415,6 +1461,26 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
return $items->first();
}
/**
* Determine if the collection contains a single item, optionally matching the given criteria.
*
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function hasSole($key = null, $operator = null, $value = null): bool
{
$filter = func_num_args() > 1
? $this->operatorForWhere(...func_get_args())
: $key;
return $this
->unless($filter == null)
->filter($filter)
->count() === 1;
}
/**
* Get the first item in the collection but throw an exception if no matching items exist.
*
@@ -1584,7 +1650,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
}
} else {
$result = match ($options) {
SORT_NUMERIC => intval($values[0]) <=> intval($values[1]),
SORT_NUMERIC => (int) $values[0] <=> (int) $values[1],
SORT_STRING => strcmp($values[0], $values[1]),
SORT_NATURAL => strnatcmp((string) $values[0], (string) $values[1]),
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
@@ -1742,11 +1808,12 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Flatten a multi-dimensional associative array with dots.
*
* @param int $depth
* @return static
*/
public function dot()
public function dot($depth = INF)
{
return new static(Arr::dot($this->all()));
return new static(Arr::dot($this->all(), '', $depth));
}
/**
@@ -1852,7 +1919,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Count the number of items in the collection by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key|\UnitEnum)|string|null $countBy
* @param (callable(TValue, TKey): (array-key|\UnitEnum))|string|null $countBy
* @return static<array-key, int>
*/
public function countBy($countBy = null)