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

@@ -33,7 +33,19 @@ class Arr
* Determine whether the given value is arrayable.
*
* @param mixed $value
* @return bool
* @return ($value is array
* ? true
* : ($value is \Illuminate\Contracts\Support\Arrayable
* ? true
* : ($value is \Traversable
* ? true
* : ($value is \Illuminate\Contracts\Support\Jsonable
* ? true
* : ($value is \JsonSerializable ? true : false)
* )
* )
* )
* )
*/
public static function arrayable($value)
{
@@ -63,6 +75,8 @@ class Arr
/**
* Get an array item from an array using "dot" notation.
*
* @throws \InvalidArgumentException
*/
public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = null): array
{
@@ -79,6 +93,8 @@ class Arr
/**
* Get a boolean item from an array using "dot" notation.
*
* @throws \InvalidArgumentException
*/
public static function boolean(ArrayAccess|array $array, string|int|null $key, ?bool $default = null): bool
{
@@ -117,8 +133,10 @@ class Arr
/**
* Cross join the given arrays, returning all possible permutations.
*
* @param iterable ...$arrays
* @return array
* @template TValue
*
* @param iterable<TValue> ...$arrays
* @return array<int, array<array-key, TValue>>
*/
public static function crossJoin(...$arrays)
{
@@ -144,8 +162,11 @@ class Arr
/**
* Divide an array into two arrays. One with keys and the other with values.
*
* @param array $array
* @return array
* @template TKey of array-key
* @template TValue
*
* @param array<TKey, TValue> $array
* @return array{TKey[], TValue[]}
*/
public static function divide($array)
{
@@ -157,25 +178,29 @@ class Arr
*
* @param iterable $array
* @param string $prepend
* @param int $depth
* @return array
*/
public static function dot($array, $prepend = '')
public static function dot($array, $prepend = '', $depth = INF)
{
$results = [];
$flatten = function ($data, $prefix) use (&$results, &$flatten): void {
$flatten = function ($data, $prefix, $currentDepth) use (&$results, &$flatten, $depth): void {
foreach ($data as $key => $value) {
$newKey = $prefix.$key;
if (is_array($value) && ! empty($value)) {
$flatten($value, $newKey.'.');
if (is_array($value) && ! empty($value) && $currentDepth < $depth) {
$flatten($value, $newKey.'.', $currentDepth + 1);
} else {
$results[$newKey] = $value;
}
}
};
$flatten($array, $prepend);
$flatten($array, $prepend, 0);
// Destroy self-referencing closure to avoid memory leak...
$flatten = null;
return $results;
}
@@ -211,6 +236,23 @@ class Arr
return $array;
}
/**
* Get all of the given array except for a specified array of values.
*
* @param array $array
* @param mixed $values
* @param bool $strict
* @return array
*/
public static function exceptValues($array, $values, $strict = false)
{
$values = (array) $values;
return array_filter($array, function ($value) use ($values, $strict) {
return ! in_array($value, $values, $strict);
});
}
/**
* Determine if the given key exists in the provided array.
*
@@ -228,7 +270,7 @@ class Arr
return $array->offsetExists($key);
}
if (is_float($key)) {
if (is_float($key) || is_null($key)) {
$key = (string) $key;
}
@@ -236,7 +278,7 @@ class Arr
}
/**
* Return the first element in an array passing a given truth test.
* Return the first element in an iterable passing a given truth test.
*
* @template TKey
* @template TValue
@@ -265,6 +307,8 @@ class Arr
return value($default);
}
$array = static::from($array);
$key = array_find_key($array, $callback);
return $key !== null ? $array[$key] : value($default);
@@ -339,6 +383,8 @@ class Arr
/**
* Get a float item from an array using "dot" notation.
*
* @throws \InvalidArgumentException
*/
public static function float(ArrayAccess|array $array, string|int|null $key, ?float $default = null): float
{
@@ -446,7 +492,7 @@ class Arr
}
if (! str_contains($key, '.')) {
return $array[$key] ?? value($default);
return value($default);
}
foreach (explode('.', $key) as $segment) {
@@ -576,6 +622,8 @@ class Arr
/**
* Get an integer item from an array using "dot" notation.
*
* @throws \InvalidArgumentException
*/
public static function integer(ArrayAccess|array $array, string|int|null $key, ?int $default = null): int
{
@@ -596,7 +644,7 @@ class Arr
* An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
*
* @param array $array
* @return bool
* @return ($array is list ? false : true)
*/
public static function isAssoc(array $array)
{
@@ -609,7 +657,7 @@ class Arr
* An array is a "list" if all array keys are sequential integers starting from 0 with no gaps in between.
*
* @param array $array
* @return bool
* @return ($array is list ? true : false)
*/
public static function isList($array)
{
@@ -646,7 +694,7 @@ class Arr
/**
* Key an associative array by a field or using a callback.
*
* @param array $array
* @param iterable $array
* @param callable|array|string $keyBy
* @return array
*/
@@ -679,6 +727,23 @@ class Arr
return array_intersect_key($array, array_flip((array) $keys));
}
/**
* Get a subset of the items from the given array by value.
*
* @param array $array
* @param mixed $values
* @param bool $strict
* @return array
*/
public static function onlyValues($array, $values, $strict = false)
{
$values = (array) $values;
return array_filter($array, function ($value) use ($values, $strict) {
return in_array($value, $values, $strict);
});
}
/**
* Select an array of values from an array.
*
@@ -748,7 +813,7 @@ class Arr
/**
* Explode the "value" and "key" arguments passed to "pluck".
*
* @param string|array|Closure $value
* @param Closure|array|string $value
* @param string|array|Closure|null $key
* @return array
*/
@@ -1022,9 +1087,12 @@ class Arr
/**
* Sort the array using the given callback or "dot" notation.
*
* @param array $array
* @param callable|array|string|null $callback
* @return array
* @template TKey of array-key
* @template TValue
*
* @param iterable<TKey, TValue> $array
* @param callable|string|null|array<int, (callable(TValue, TValue): -1|0|1)|array{string, 'asc'|'desc'}> $callback
* @return array<TKey, TValue>
*/
public static function sort($array, $callback = null)
{
@@ -1034,9 +1102,12 @@ class Arr
/**
* Sort the array in descending order using the given callback or "dot" notation.
*
* @param array $array
* @param callable|array|string|null $callback
* @return array
* @template TKey of array-key
* @template TValue
*
* @param iterable<TKey, TValue> $array
* @param callable|string|null|array<int, (callable(TValue, TValue): -1|0|1)|array{string, 'asc'|'desc'}> $callback
* @return array<TKey, TValue>
*/
public static function sortDesc($array, $callback = null)
{
@@ -1046,10 +1117,13 @@ class Arr
/**
* Recursively sort an array by keys and values.
*
* @param array $array
* @param int $options
* @template TKey of array-key
* @template TValue
*
* @param array<TKey, TValue> $array
* @param int-mask-of<SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_LOCALE_STRING|SORT_NATURAL|SORT_FLAG_CASE> $options
* @param bool $descending
* @return array
* @return array<TKey, TValue>
*/
public static function sortRecursive($array, $options = SORT_REGULAR, $descending = false)
{
@@ -1075,9 +1149,12 @@ class Arr
/**
* Recursively sort an array by keys and values in descending order.
*
* @param array $array
* @param int $options
* @return array
* @template TKey of array-key
* @template TValue
*
* @param array<TKey, TValue> $array
* @param int-mask-of<SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_LOCALE_STRING|SORT_NATURAL|SORT_FLAG_CASE> $options
* @return array<TKey, TValue>
*/
public static function sortRecursiveDesc($array, $options = SORT_REGULAR)
{
@@ -1086,6 +1163,8 @@ class Arr
/**
* Get a string item from an array using "dot" notation.
*
* @throws \InvalidArgumentException
*/
public static function string(ArrayAccess|array $array, string|int|null $key, ?string $default = null): string
{
@@ -1103,8 +1182,8 @@ class Arr
/**
* Conditionally compile classes from an array into a CSS class list.
*
* @param array|string $array
* @return string
* @param array<string, bool>|array<int, string|int>|string $array
* @return ($array is array<string, false> ? '' : ($array is '' ? '' : ($array is array{} ? '' : non-empty-string)))
*/
public static function toCssClasses($array)
{
@@ -1126,8 +1205,8 @@ class Arr
/**
* Conditionally compile styles from an array into a style list.
*
* @param array|string $array
* @return string
* @param array<string, bool>|array<int, string|int>|string $array
* @return ($array is array<string, false> ? '' : ($array is '' ? '' : ($array is array{} ? '' : non-empty-string)))
*/
public static function toCssStyles($array)
{
@@ -1149,9 +1228,12 @@ class Arr
/**
* Filter the array using the given callback.
*
* @param array $array
* @param callable $callback
* @return array
* @template TKey of array-key
* @template TValue
*
* @param array<TKey, TValue> $array
* @param callable(TValue, TKey): bool $callback
* @return array<TKey, TValue>
*/
public static function where($array, callable $callback)
{
@@ -1161,9 +1243,12 @@ class Arr
/**
* Filter the array using the negation of the given callback.
*
* @param array $array
* @param callable $callback
* @return array
* @template TKey of array-key
* @template TValue
*
* @param array<TKey, TValue> $array
* @param callable(TValue, TKey): bool $callback
* @return array<TKey, TValue>
*/
public static function reject($array, callable $callback)
{
@@ -1210,8 +1295,11 @@ class Arr
/**
* If the given value is not an array and not null, wrap it in one.
*
* @param mixed $value
* @return array
* @template TKey of array-key = array-key
* @template TValue
*
* @param array<TKey, TValue>|TValue|null $value
* @return ($value is null ? array{} : ($value is array ? array<TKey, TValue> : array{TValue}))
*/
public static function wrap($value)
{