mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-10-29 20:53:30 +00:00
[Web] Update composer libraries
This commit is contained in:
1297 files changed
+71585
-24460
No files matched your search
+465
-92
@@ -4,8 +4,15 @@ namespace Illuminate\Support;
|
||||
|
||||
use ArgumentCountError;
|
||||
use ArrayAccess;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use InvalidArgumentException;
|
||||
use JsonSerializable;
|
||||
use Random\Randomizer;
|
||||
use Traversable;
|
||||
use WeakMap;
|
||||
|
||||
class Arr
|
||||
{
|
||||
@@ -22,6 +29,33 @@ class Arr
|
||||
return is_array($value) || $value instanceof ArrayAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given value is arrayable.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @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)
|
||||
{
|
||||
return is_array($value)
|
||||
|| $value instanceof Arrayable
|
||||
|| $value instanceof Traversable
|
||||
|| $value instanceof Jsonable
|
||||
|| $value instanceof JsonSerializable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an element to an array using "dot" notation if it doesn't exist.
|
||||
*
|
||||
@@ -39,6 +73,42 @@ class Arr
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$value = Arr::get($array, $key, $default);
|
||||
|
||||
if (! is_array($value)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Array value for key [%s] must be an array, %s found.', $key, gettype($value))
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$value = Arr::get($array, $key, $default);
|
||||
|
||||
if (! is_bool($value)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Array value for key [%s] must be a boolean, %s found.', $key, gettype($value))
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapse an array of arrays into a single array.
|
||||
*
|
||||
@@ -51,12 +121,10 @@ class Arr
|
||||
|
||||
foreach ($array as $values) {
|
||||
if ($values instanceof Collection) {
|
||||
$values = $values->all();
|
||||
} elseif (! is_array($values)) {
|
||||
continue;
|
||||
$results[] = $values->all();
|
||||
} elseif (is_array($values)) {
|
||||
$results[] = $values;
|
||||
}
|
||||
|
||||
$results[] = $values;
|
||||
}
|
||||
|
||||
return array_merge([], ...$results);
|
||||
@@ -65,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)
|
||||
{
|
||||
@@ -92,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)
|
||||
{
|
||||
@@ -105,19 +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 = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value) && ! empty($value)) {
|
||||
$results = array_merge($results, static::dot($value, $prepend.$key.'.'));
|
||||
} else {
|
||||
$results[$prepend.$key] = $value;
|
||||
$flatten = function ($data, $prefix, $currentDepth) use (&$results, &$flatten, $depth): void {
|
||||
foreach ($data as $key => $value) {
|
||||
$newKey = $prefix.$key;
|
||||
|
||||
if (is_array($value) && ! empty($value) && $currentDepth < $depth) {
|
||||
$flatten($value, $newKey.'.', $currentDepth + 1);
|
||||
} else {
|
||||
$results[$newKey] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$flatten($array, $prepend, 0);
|
||||
|
||||
// Destroy self-referencing closure to avoid memory leak...
|
||||
$flatten = null;
|
||||
|
||||
return $results;
|
||||
}
|
||||
@@ -153,11 +236,28 @@ 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.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string|int $key
|
||||
* @param string|int|float $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function exists($array, $key)
|
||||
@@ -170,7 +270,7 @@ class Arr
|
||||
return $array->offsetExists($key);
|
||||
}
|
||||
|
||||
if (is_float($key)) {
|
||||
if (is_float($key) || is_null($key)) {
|
||||
$key = (string) $key;
|
||||
}
|
||||
|
||||
@@ -178,12 +278,16 @@ 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.
|
||||
*
|
||||
* @param iterable $array
|
||||
* @param callable|null $callback
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
* @template TKey
|
||||
* @template TValue
|
||||
* @template TFirstDefault
|
||||
*
|
||||
* @param iterable<TKey, TValue> $array
|
||||
* @param (callable(TValue, TKey): bool)|null $callback
|
||||
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
|
||||
* @return TValue|TFirstDefault
|
||||
*/
|
||||
public static function first($array, ?callable $callback = null, $default = null)
|
||||
{
|
||||
@@ -192,6 +296,10 @@ class Arr
|
||||
return value($default);
|
||||
}
|
||||
|
||||
if (is_array($array)) {
|
||||
return array_first($array);
|
||||
}
|
||||
|
||||
foreach ($array as $item) {
|
||||
return $item;
|
||||
}
|
||||
@@ -199,27 +307,29 @@ class Arr
|
||||
return value($default);
|
||||
}
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
if ($callback($value, $key)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
$array = static::from($array);
|
||||
|
||||
return value($default);
|
||||
$key = array_find_key($array, $callback);
|
||||
|
||||
return $key !== null ? $array[$key] : value($default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the last element in an array passing a given truth test.
|
||||
*
|
||||
* @param array $array
|
||||
* @param callable|null $callback
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
* @template TKey
|
||||
* @template TValue
|
||||
* @template TLastDefault
|
||||
*
|
||||
* @param iterable<TKey, TValue> $array
|
||||
* @param (callable(TValue, TKey): bool)|null $callback
|
||||
* @param TLastDefault|(\Closure(): TLastDefault) $default
|
||||
* @return TValue|TLastDefault
|
||||
*/
|
||||
public static function last($array, ?callable $callback = null, $default = null)
|
||||
{
|
||||
if (is_null($callback)) {
|
||||
return empty($array) ? value($default) : end($array);
|
||||
return empty($array) ? value($default) : array_last($array);
|
||||
}
|
||||
|
||||
return static::first(array_reverse($array, true), $callback, $default);
|
||||
@@ -271,6 +381,24 @@ class Arr
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$value = Arr::get($array, $key, $default);
|
||||
|
||||
if (! is_float($value)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Array value for key [%s] must be a float, %s found.', $key, gettype($value))
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove one or many array items from a given array using "dot" notation.
|
||||
*
|
||||
@@ -315,6 +443,32 @@ class Arr
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying array of items from the given argument.
|
||||
*
|
||||
* @template TKey of array-key = array-key
|
||||
* @template TValue = mixed
|
||||
*
|
||||
* @param array<TKey, TValue>|Enumerable<TKey, TValue>|Arrayable<TKey, TValue>|WeakMap<object, TValue>|Traversable<TKey, TValue>|Jsonable|JsonSerializable|object $items
|
||||
* @return ($items is WeakMap ? list<TValue> : array<TKey, TValue>)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function from($items)
|
||||
{
|
||||
return match (true) {
|
||||
is_array($items) => $items,
|
||||
$items instanceof Enumerable => $items->all(),
|
||||
$items instanceof Arrayable => $items->toArray(),
|
||||
$items instanceof WeakMap => iterator_to_array($items, false),
|
||||
$items instanceof Traversable => iterator_to_array($items),
|
||||
$items instanceof Jsonable => json_decode($items->toJson(), true),
|
||||
$items instanceof JsonSerializable => (array) $items->jsonSerialize(),
|
||||
is_object($items) => (array) $items,
|
||||
default => throw new InvalidArgumentException('Items cannot be represented by a scalar value.'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from an array using "dot" notation.
|
||||
*
|
||||
@@ -338,7 +492,7 @@ class Arr
|
||||
}
|
||||
|
||||
if (! str_contains($key, '.')) {
|
||||
return $array[$key] ?? value($default);
|
||||
return value($default);
|
||||
}
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
@@ -386,6 +540,30 @@ class Arr
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if all keys exist in an array using "dot" notation.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string|array $keys
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasAll($array, $keys)
|
||||
{
|
||||
$keys = (array) $keys;
|
||||
|
||||
if (! $array || $keys === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (! static::has($array, $key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any of the keys exist in an array using "dot" notation.
|
||||
*
|
||||
@@ -418,13 +596,55 @@ class Arr
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if all items pass the given truth test.
|
||||
*
|
||||
* @param iterable $array
|
||||
* @param (callable(mixed, array-key): bool) $callback
|
||||
* @return bool
|
||||
*/
|
||||
public static function every($array, callable $callback)
|
||||
{
|
||||
return array_all($array, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if some items pass the given truth test.
|
||||
*
|
||||
* @param iterable $array
|
||||
* @param (callable(mixed, array-key): bool) $callback
|
||||
* @return bool
|
||||
*/
|
||||
public static function some($array, callable $callback)
|
||||
{
|
||||
return array_any($array, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$value = Arr::get($array, $key, $default);
|
||||
|
||||
if (! is_int($value)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Array value for key [%s] must be an integer, %s found.', $key, gettype($value))
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an array is associative.
|
||||
*
|
||||
* 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)
|
||||
{
|
||||
@@ -437,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)
|
||||
{
|
||||
@@ -463,7 +683,7 @@ class Arr
|
||||
}
|
||||
|
||||
if (count($array) === 1) {
|
||||
return end($array);
|
||||
return array_last($array);
|
||||
}
|
||||
|
||||
$finalItem = array_pop($array);
|
||||
@@ -474,13 +694,13 @@ 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
|
||||
*/
|
||||
public static function keyBy($array, $keyBy)
|
||||
{
|
||||
return Collection::make($array)->keyBy($keyBy)->all();
|
||||
return (new Collection($array))->keyBy($keyBy)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -507,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.
|
||||
*
|
||||
@@ -537,8 +774,8 @@ class Arr
|
||||
* Pluck an array of values from an array.
|
||||
*
|
||||
* @param iterable $array
|
||||
* @param string|array|int|null $value
|
||||
* @param string|array|null $key
|
||||
* @param string|array|int|Closure|null $value
|
||||
* @param string|array|Closure|null $key
|
||||
* @return array
|
||||
*/
|
||||
public static function pluck($array, $value, $key = null)
|
||||
@@ -548,7 +785,9 @@ class Arr
|
||||
[$value, $key] = static::explodePluckParameters($value, $key);
|
||||
|
||||
foreach ($array as $item) {
|
||||
$itemValue = data_get($item, $value);
|
||||
$itemValue = $value instanceof Closure
|
||||
? $value($item)
|
||||
: data_get($item, $value);
|
||||
|
||||
// If the key is "null", we will just append the value to the array and keep
|
||||
// looping. Otherwise we will key the array using the value of the key we
|
||||
@@ -556,7 +795,9 @@ class Arr
|
||||
if (is_null($key)) {
|
||||
$results[] = $itemValue;
|
||||
} else {
|
||||
$itemKey = data_get($item, $key);
|
||||
$itemKey = $key instanceof Closure
|
||||
? $key($item)
|
||||
: data_get($item, $key);
|
||||
|
||||
if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
|
||||
$itemKey = (string) $itemKey;
|
||||
@@ -572,15 +813,15 @@ class Arr
|
||||
/**
|
||||
* Explode the "value" and "key" arguments passed to "pluck".
|
||||
*
|
||||
* @param string|array $value
|
||||
* @param string|array|null $key
|
||||
* @param Closure|array|string $value
|
||||
* @param string|array|Closure|null $key
|
||||
* @return array
|
||||
*/
|
||||
protected static function explodePluckParameters($value, $key)
|
||||
{
|
||||
$value = is_string($value) ? explode('.', $value) : $value;
|
||||
|
||||
$key = is_null($key) || is_array($key) ? $key : explode('.', $key);
|
||||
$key = is_null($key) || is_array($key) || $key instanceof Closure ? $key : explode('.', $key);
|
||||
|
||||
return [$value, $key];
|
||||
}
|
||||
@@ -634,6 +875,25 @@ class Arr
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a map over each nested chunk of items.
|
||||
*
|
||||
* @template TKey
|
||||
* @template TValue
|
||||
*
|
||||
* @param array<TKey, array> $array
|
||||
* @param callable(mixed...): TValue $callback
|
||||
* @return array<TKey, TValue>
|
||||
*/
|
||||
public static function mapSpread(array $array, callable $callback)
|
||||
{
|
||||
return static::map($array, function ($chunk, $key) use ($callback) {
|
||||
$chunk[] = $key;
|
||||
|
||||
return $callback(...$chunk);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Push an item onto the beginning of an array.
|
||||
*
|
||||
@@ -703,24 +963,24 @@ class Arr
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($array) || (! is_null($number) && $number <= 0)) {
|
||||
return is_null($number) ? null : [];
|
||||
}
|
||||
|
||||
$keys = (new Randomizer)->pickArrayKeys($array, $requested);
|
||||
|
||||
if (is_null($number)) {
|
||||
return $array[array_rand($array)];
|
||||
return $array[$keys[0]];
|
||||
}
|
||||
|
||||
if ((int) $number === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$keys = array_rand($array, $number);
|
||||
|
||||
$results = [];
|
||||
|
||||
if ($preserveKeys) {
|
||||
foreach ((array) $keys as $key) {
|
||||
foreach ($keys as $key) {
|
||||
$results[$key] = $array[$key];
|
||||
}
|
||||
} else {
|
||||
foreach ((array) $keys as $key) {
|
||||
foreach ($keys as $key) {
|
||||
$results[] = $array[$key];
|
||||
}
|
||||
}
|
||||
@@ -768,57 +1028,102 @@ class Arr
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push an item into an array using "dot" notation.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string|int|null $key
|
||||
* @param mixed $values
|
||||
* @return array
|
||||
*/
|
||||
public static function push(ArrayAccess|array &$array, string|int|null $key, mixed ...$values): array
|
||||
{
|
||||
$target = static::array($array, $key, []);
|
||||
|
||||
array_push($target, ...$values);
|
||||
|
||||
return static::set($array, $key, $target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffle the given array and return the result.
|
||||
*
|
||||
* @param array $array
|
||||
* @param int|null $seed
|
||||
* @return array
|
||||
*/
|
||||
public static function shuffle($array, $seed = null)
|
||||
public static function shuffle($array)
|
||||
{
|
||||
if (is_null($seed)) {
|
||||
shuffle($array);
|
||||
} else {
|
||||
mt_srand($seed);
|
||||
shuffle($array);
|
||||
mt_srand();
|
||||
return (new Randomizer)->shuffleArray($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first item in the array, but only if exactly one item exists. Otherwise, throw an exception.
|
||||
*
|
||||
* @param array $array
|
||||
* @param (callable(mixed, array-key): array)|null $callback
|
||||
*
|
||||
* @throws \Illuminate\Support\ItemNotFoundException
|
||||
* @throws \Illuminate\Support\MultipleItemsFoundException
|
||||
*/
|
||||
public static function sole($array, ?callable $callback = null)
|
||||
{
|
||||
if ($callback) {
|
||||
$array = static::where($array, $callback);
|
||||
}
|
||||
|
||||
return $array;
|
||||
$count = count($array);
|
||||
|
||||
if ($count === 0) {
|
||||
throw new ItemNotFoundException;
|
||||
}
|
||||
|
||||
if ($count > 1) {
|
||||
throw new MultipleItemsFoundException($count);
|
||||
}
|
||||
|
||||
return static::first($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
return Collection::make($array)->sortBy($callback)->all();
|
||||
return (new Collection($array))->sortBy($callback)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
return Collection::make($array)->sortByDesc($callback)->all();
|
||||
return (new Collection($array))->sortByDesc($callback)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
@@ -830,12 +1135,12 @@ class Arr
|
||||
|
||||
if (! array_is_list($array)) {
|
||||
$descending
|
||||
? krsort($array, $options)
|
||||
: ksort($array, $options);
|
||||
? krsort($array, $options)
|
||||
: ksort($array, $options);
|
||||
} else {
|
||||
$descending
|
||||
? rsort($array, $options)
|
||||
: sort($array, $options);
|
||||
? rsort($array, $options)
|
||||
: sort($array, $options);
|
||||
}
|
||||
|
||||
return $array;
|
||||
@@ -844,20 +1149,41 @@ 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)
|
||||
{
|
||||
return static::sortRecursive($array, $options, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$value = Arr::get($array, $key, $default);
|
||||
|
||||
if (! is_string($value)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Array value for key [%s] must be a string, %s found.', $key, gettype($value))
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditionally compile classes from an array into a CSS class list.
|
||||
*
|
||||
* @param array $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)
|
||||
{
|
||||
@@ -879,8 +1205,8 @@ class Arr
|
||||
/**
|
||||
* Conditionally compile styles from an array into a style list.
|
||||
*
|
||||
* @param array $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)
|
||||
{
|
||||
@@ -902,15 +1228,59 @@ 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)
|
||||
{
|
||||
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the array using the negation of the given callback.
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
return static::where($array, fn ($value, $key) => ! $callback($value, $key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Partition the array into two arrays using the given callback.
|
||||
*
|
||||
* @template TKey of array-key
|
||||
* @template TValue of mixed
|
||||
*
|
||||
* @param iterable<TKey, TValue> $array
|
||||
* @param callable(TValue, TKey): bool $callback
|
||||
* @return array<int<0, 1>, array<TKey, TValue>>
|
||||
*/
|
||||
public static function partition($array, callable $callback)
|
||||
{
|
||||
$passed = [];
|
||||
$failed = [];
|
||||
|
||||
foreach ($array as $key => $item) {
|
||||
if ($callback($item, $key)) {
|
||||
$passed[$key] = $item;
|
||||
} else {
|
||||
$failed[$key] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [$passed, $failed];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter items where the value is not null.
|
||||
*
|
||||
@@ -925,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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user