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)
|
||||
{
|
||||
|
||||
+283
-103
@@ -7,6 +7,7 @@ use ArrayIterator;
|
||||
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
|
||||
use Illuminate\Support\Traits\EnumeratesValues;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use Illuminate\Support\Traits\TransformsToResourceCollection;
|
||||
use InvalidArgumentException;
|
||||
use stdClass;
|
||||
use Traversable;
|
||||
@@ -24,7 +25,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* @use \Illuminate\Support\Traits\EnumeratesValues<TKey, TValue>
|
||||
*/
|
||||
use EnumeratesValues, Macroable;
|
||||
use EnumeratesValues, Macroable, TransformsToResourceCollection;
|
||||
|
||||
/**
|
||||
* The items contained in the collection.
|
||||
@@ -37,7 +38,6 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
* Create a new collection.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($items = [])
|
||||
{
|
||||
@@ -49,11 +49,12 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
*
|
||||
* @param int $from
|
||||
* @param int $to
|
||||
* @param int $step
|
||||
* @return static<int, int>
|
||||
*/
|
||||
public static function range($from, $to)
|
||||
public static function range($from, $to, $step = 1)
|
||||
{
|
||||
return new static(range($from, $to));
|
||||
return new static(range($from, $to, $step));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,25 +77,6 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return new LazyCollection($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the average value of a given key.
|
||||
*
|
||||
* @param (callable(TValue): float|int)|string|null $callback
|
||||
* @return float|int|null
|
||||
*/
|
||||
public function avg($callback = null)
|
||||
{
|
||||
$callback = $this->valueRetriever($callback);
|
||||
|
||||
$items = $this
|
||||
->map(fn ($value) => $callback($value))
|
||||
->filter(fn ($value) => ! is_null($value));
|
||||
|
||||
if ($count = $items->count()) {
|
||||
return $items->sum() / $count;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the median of a given key.
|
||||
*
|
||||
@@ -104,7 +86,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
public function median($key = null)
|
||||
{
|
||||
$values = (isset($key) ? $this->pluck($key) : $this)
|
||||
->filter(fn ($item) => ! is_null($item))
|
||||
->reject(fn ($item) => is_null($item))
|
||||
->sort()->values();
|
||||
|
||||
$count = $values->count();
|
||||
@@ -113,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);
|
||||
@@ -160,6 +142,36 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return new static(Arr::collapse($this->items));
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapse the collection of items into a single array while preserving its keys.
|
||||
*
|
||||
* @return static<mixed, mixed>
|
||||
*/
|
||||
public function collapseWithKeys()
|
||||
{
|
||||
if (! $this->items) {
|
||||
return new static;
|
||||
}
|
||||
|
||||
$results = [];
|
||||
|
||||
foreach ($this->items as $key => $values) {
|
||||
if ($values instanceof Collection) {
|
||||
$values = $values->all();
|
||||
} elseif (! is_array($values)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$results[$key] = $values;
|
||||
}
|
||||
|
||||
if (! $results) {
|
||||
return new static;
|
||||
}
|
||||
|
||||
return new static(array_replace(...$results));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an item exists in the collection.
|
||||
*
|
||||
@@ -172,9 +184,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
{
|
||||
if (func_num_args() === 1) {
|
||||
if ($this->useAsCallable($key)) {
|
||||
$placeholder = new stdClass;
|
||||
|
||||
return $this->first($key, $placeholder) !== $placeholder;
|
||||
return array_any($this->items, $key);
|
||||
}
|
||||
|
||||
return in_array($key, $this->items);
|
||||
@@ -216,6 +226,19 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return ! $this->contains(...func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an item is not contained in the enumerable, using strict comparison.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $operator
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function doesntContainStrict($key, $operator = null, $value = null)
|
||||
{
|
||||
return ! $this->containsStrict(...func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cross join with the given lists, returning all possible permutations.
|
||||
*
|
||||
@@ -228,7 +251,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
public function crossJoin(...$lists)
|
||||
{
|
||||
return new static(Arr::crossJoin(
|
||||
$this->items, ...array_map([$this, 'getArrayableItems'], $lists)
|
||||
$this->items, ...array_map($this->getArrayableItems(...), $lists)
|
||||
));
|
||||
}
|
||||
|
||||
@@ -281,7 +304,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Get the items in the collection whose keys are not present in the given items.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @return static
|
||||
*/
|
||||
public function diffKeys($items)
|
||||
@@ -292,7 +315,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Get the items in the collection whose keys are not present in the given items, using the callback.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @param callable(TKey, TKey): int $callback
|
||||
* @return static
|
||||
*/
|
||||
@@ -304,7 +327,9 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Retrieve duplicate items from the collection.
|
||||
*
|
||||
* @param (callable(TValue): bool)|string|null $callback
|
||||
* @template TMapValue
|
||||
*
|
||||
* @param (callable(TValue): TMapValue)|string|null $callback
|
||||
* @param bool $strict
|
||||
* @return static
|
||||
*/
|
||||
@@ -332,7 +357,9 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Retrieve duplicate items from the collection using strict comparison.
|
||||
*
|
||||
* @param (callable(TValue): bool)|string|null $callback
|
||||
* @template TMapValue
|
||||
*
|
||||
* @param (callable(TValue): TMapValue)|string|null $callback
|
||||
* @return static
|
||||
*/
|
||||
public function duplicatesStrict($callback = null)
|
||||
@@ -429,8 +456,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Remove an item from the collection by key.
|
||||
*
|
||||
* \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
|
||||
* @return $this
|
||||
*/
|
||||
public function forget($keys)
|
||||
@@ -447,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];
|
||||
}
|
||||
@@ -471,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));
|
||||
@@ -483,9 +511,16 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Group an associative array by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
|
||||
* @template TGroupKey of array-key|\UnitEnum|\Stringable
|
||||
*
|
||||
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
|
||||
* @param bool $preserveKeys
|
||||
* @return static<array-key, static<array-key, 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)
|
||||
{
|
||||
@@ -509,8 +544,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
foreach ($groupKeys as $groupKey) {
|
||||
$groupKey = match (true) {
|
||||
is_bool($groupKey) => (int) $groupKey,
|
||||
$groupKey instanceof \BackedEnum => $groupKey->value,
|
||||
$groupKey instanceof \Stringable => (string) $groupKey,
|
||||
$groupKey instanceof \UnitEnum => enum_value($groupKey),
|
||||
$groupKey instanceof \Stringable, is_null($groupKey) => (string) $groupKey,
|
||||
default => $groupKey,
|
||||
};
|
||||
|
||||
@@ -534,8 +569,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Key an associative array by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
|
||||
* @return static<array-key, TValue>
|
||||
* @template TNewKey of array-key|\UnitEnum
|
||||
*
|
||||
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
|
||||
* @return static<($keyBy is (array|string) ? array-key : (TNewKey is \UnitEnum ? array-key : TNewKey)), TValue>
|
||||
*/
|
||||
public function keyBy($keyBy)
|
||||
{
|
||||
@@ -546,6 +583,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
foreach ($this->items as $key => $item) {
|
||||
$resolvedKey = $keyBy($item, $key);
|
||||
|
||||
if ($resolvedKey instanceof \UnitEnum) {
|
||||
$resolvedKey = enum_value($resolvedKey);
|
||||
}
|
||||
|
||||
if (is_object($resolvedKey)) {
|
||||
$resolvedKey = (string) $resolvedKey;
|
||||
}
|
||||
@@ -566,19 +607,13 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
{
|
||||
$keys = is_array($key) ? $key : func_get_args();
|
||||
|
||||
foreach ($keys as $value) {
|
||||
if (! array_key_exists($value, $this->items)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return array_all($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any of the keys exist in the collection.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param TKey|array<array-key, TKey> $key
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAny($key)
|
||||
@@ -589,19 +624,13 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
|
||||
$keys = is_array($key) ? $key : func_get_args();
|
||||
|
||||
foreach ($keys as $value) {
|
||||
if ($this->has($value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return array_any($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate values of a given key as a string.
|
||||
*
|
||||
* @param callable|string $value
|
||||
* @param (callable(TValue, TKey): mixed)|string|null $value
|
||||
* @param string|null $glue
|
||||
* @return string
|
||||
*/
|
||||
@@ -669,7 +698,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Intersect the collection with the given items by key.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @return static
|
||||
*/
|
||||
public function intersectByKeys($items)
|
||||
@@ -682,6 +711,12 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Determine if the collection is empty or not.
|
||||
*
|
||||
* @phpstan-assert-if-true null $this->first()
|
||||
* @phpstan-assert-if-true null $this->last()
|
||||
*
|
||||
* @phpstan-assert-if-false TValue $this->first()
|
||||
* @phpstan-assert-if-false TValue $this->last()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
@@ -690,13 +725,29 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the collection contains a single item.
|
||||
* Determine if the collection contains exactly one item. If a callback is provided, determine if exactly one item matches the condition.
|
||||
*
|
||||
* @param (callable(TValue, TKey): bool)|null $callback
|
||||
* @return bool
|
||||
*
|
||||
* @deprecated 12.49.0 Use the `hasSole()` method instead.
|
||||
*/
|
||||
public function containsOneItem()
|
||||
public function containsOneItem(?callable $callback = null): bool
|
||||
{
|
||||
return $this->count() === 1;
|
||||
return $this->hasSole($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -704,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 = '')
|
||||
{
|
||||
@@ -756,8 +807,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Get the values of a given key.
|
||||
*
|
||||
* @param string|int|array<array-key, string> $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)
|
||||
@@ -829,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)
|
||||
{
|
||||
@@ -850,6 +903,23 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return new static(array_merge_recursive($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply the items in the collection by the multiplier.
|
||||
*
|
||||
* @param int $multiplier
|
||||
* @return static
|
||||
*/
|
||||
public function multiply(int $multiplier)
|
||||
{
|
||||
$new = new static;
|
||||
|
||||
for ($i = 0; $i < $multiplier; $i++) {
|
||||
$new->push(...$this->items);
|
||||
}
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a collection by using this collection for keys and another for its values.
|
||||
*
|
||||
@@ -879,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;
|
||||
@@ -944,10 +1020,14 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
* Get and remove the last N items from the collection.
|
||||
*
|
||||
* @param int $count
|
||||
* @return static<int, TValue>|TValue|null
|
||||
* @return ($count is 1 ? TValue|null : static<int, TValue>)
|
||||
*/
|
||||
public function pop($count = 1)
|
||||
{
|
||||
if ($count < 1) {
|
||||
return new static;
|
||||
}
|
||||
|
||||
if ($count === 1) {
|
||||
return array_pop($this->items);
|
||||
}
|
||||
@@ -961,7 +1041,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
$collectionCount = $this->count();
|
||||
|
||||
foreach (range(1, min($count, $collectionCount)) as $item) {
|
||||
array_push($results, array_pop($this->items));
|
||||
$results[] = array_pop($this->items);
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
@@ -976,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;
|
||||
}
|
||||
@@ -996,6 +1076,19 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepend one or more items to the beginning of the collection.
|
||||
*
|
||||
* @param TValue ...$values
|
||||
* @return $this
|
||||
*/
|
||||
public function unshift(...$values)
|
||||
{
|
||||
array_unshift($this->items, ...$values);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push all of the given items onto the collection.
|
||||
*
|
||||
@@ -1049,7 +1142,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
*
|
||||
* @param (callable(self<TKey, TValue>): int)|int|null $number
|
||||
* @param bool $preserveKeys
|
||||
* @return static<int, TValue>|TValue
|
||||
* @return ($number is null ? TValue : static<int, TValue>)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
@@ -1111,20 +1204,62 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return array_search($value, $this->items, $strict);
|
||||
}
|
||||
|
||||
foreach ($this->items as $key => $item) {
|
||||
if ($value($item, $key)) {
|
||||
return $key;
|
||||
}
|
||||
return array_find_key($this->items, $value) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the item before the given item.
|
||||
*
|
||||
* @param TValue|(callable(TValue,TKey): bool) $value
|
||||
* @param bool $strict
|
||||
* @return TValue|null
|
||||
*/
|
||||
public function before($value, $strict = false)
|
||||
{
|
||||
$key = $this->search($value, $strict);
|
||||
|
||||
if ($key === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return false;
|
||||
$position = ($keys = $this->keys())->search($key);
|
||||
|
||||
if ($position === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->get($keys->get($position - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the item after the given item.
|
||||
*
|
||||
* @param TValue|(callable(TValue,TKey): bool) $value
|
||||
* @param bool $strict
|
||||
* @return TValue|null
|
||||
*/
|
||||
public function after($value, $strict = false)
|
||||
{
|
||||
$key = $this->search($value, $strict);
|
||||
|
||||
if ($key === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$position = ($keys = $this->keys())->search($key);
|
||||
|
||||
if ($position === $keys->count() - 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->get($keys->get($position + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and remove the first N items from the collection.
|
||||
*
|
||||
* @param int $count
|
||||
* @return static<int, TValue>|TValue|null
|
||||
* @param int<0, max> $count
|
||||
* @return ($count is 1 ? TValue|null : static<int, TValue>)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
@@ -1151,7 +1286,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
$collectionCount = $this->count();
|
||||
|
||||
foreach (range(1, min($count, $collectionCount)) as $item) {
|
||||
array_push($results, array_shift($this->items));
|
||||
$results[] = array_shift($this->items);
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
@@ -1160,23 +1295,30 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Shuffle the items in the collection.
|
||||
*
|
||||
* @param int|null $seed
|
||||
* @return static
|
||||
*/
|
||||
public function shuffle($seed = null)
|
||||
public function shuffle()
|
||||
{
|
||||
return new static(Arr::shuffle($this->items, $seed));
|
||||
return new static(Arr::shuffle($this->items));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
@@ -1231,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;
|
||||
}
|
||||
@@ -1268,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)
|
||||
{
|
||||
return $this->chunk(ceil($this->count() / $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
|
||||
@@ -1307,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.
|
||||
*
|
||||
@@ -1338,9 +1512,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
* Chunk the collection into chunks of the given size.
|
||||
*
|
||||
* @param int $size
|
||||
* @return static<int, static>
|
||||
* @param bool $preserveKeys
|
||||
* @return ($preserveKeys is true ? static<int, static> : static<int, static<int, TValue>>)
|
||||
*/
|
||||
public function chunk($size)
|
||||
public function chunk($size, $preserveKeys = true)
|
||||
{
|
||||
if ($size <= 0) {
|
||||
return new static;
|
||||
@@ -1348,7 +1523,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
|
||||
$chunks = [];
|
||||
|
||||
foreach (array_chunk($this->items, $size, true) as $chunk) {
|
||||
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
|
||||
$chunks[] = new static($chunk);
|
||||
}
|
||||
|
||||
@@ -1358,8 +1533,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Chunk the collection into chunks with a callback.
|
||||
*
|
||||
* @param callable(TValue, TKey, static<int, TValue>): bool $callback
|
||||
* @return static<int, static<int, TValue>>
|
||||
* @param callable(TValue, TKey, static<TKey, TValue>): bool $callback
|
||||
* @return static<int, static<TKey, TValue>>
|
||||
*/
|
||||
public function chunkWhile(callable $callback)
|
||||
{
|
||||
@@ -1475,9 +1650,9 @@ 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($values[0], $values[1]),
|
||||
SORT_NATURAL => strnatcmp((string) $values[0], (string) $values[1]),
|
||||
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
|
||||
default => $values[0] <=> $values[1],
|
||||
};
|
||||
@@ -1616,8 +1791,12 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Transform each item in the collection using a callback.
|
||||
*
|
||||
* @param callable(TValue, TKey): TValue $callback
|
||||
* @template TMapValue
|
||||
*
|
||||
* @param callable(TValue, TKey): TMapValue $callback
|
||||
* @return $this
|
||||
*
|
||||
* @phpstan-this-out static<TKey, TMapValue>
|
||||
*/
|
||||
public function transform(callable $callback)
|
||||
{
|
||||
@@ -1629,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));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1729,7 +1909,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Count the number of items in the collection.
|
||||
*
|
||||
* @return int
|
||||
* @return int<0, max>
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
@@ -1739,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)|string|null $countBy
|
||||
* @param (callable(TValue, TKey): (array-key|\UnitEnum))|string|null $countBy
|
||||
* @return static<array-key, int>
|
||||
*/
|
||||
public function countBy($countBy = null)
|
||||
|
||||
+85
-18
@@ -45,9 +45,10 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*
|
||||
* @param int $from
|
||||
* @param int $to
|
||||
* @param int $step
|
||||
* @return static
|
||||
*/
|
||||
public static function range($from, $to);
|
||||
public static function range($from, $to, $step = 1);
|
||||
|
||||
/**
|
||||
* Wrap the given value in a collection if applicable.
|
||||
@@ -184,9 +185,10 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Dump the collection.
|
||||
*
|
||||
* @param mixed ...$args
|
||||
* @return $this
|
||||
*/
|
||||
public function dump();
|
||||
public function dump(...$args);
|
||||
|
||||
/**
|
||||
* Get the items that are not present in the given items.
|
||||
@@ -225,7 +227,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Get the items whose keys are not present in the given items.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @return static
|
||||
*/
|
||||
public function diffKeys($items);
|
||||
@@ -233,7 +235,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Get the items whose keys are not present in the given items, using the callback.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @param callable(TKey, TKey): int $callback
|
||||
* @return static
|
||||
*/
|
||||
@@ -333,7 +335,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
public function whenNotEmpty(callable $callback, ?callable $default = null);
|
||||
|
||||
/**
|
||||
* Apply the callback if the given "value" is (or resolves to) truthy.
|
||||
* Apply the callback if the given "value" is (or resolves to) falsy.
|
||||
*
|
||||
* @template TUnlessReturnType
|
||||
*
|
||||
@@ -517,17 +519,21 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Group an associative array by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
|
||||
* @template TGroupKey of array-key
|
||||
*
|
||||
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
|
||||
* @param bool $preserveKeys
|
||||
* @return static<array-key, static<array-key, TValue>>
|
||||
* @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>>
|
||||
*/
|
||||
public function groupBy($groupBy, $preserveKeys = false);
|
||||
|
||||
/**
|
||||
* Key an associative array by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
|
||||
* @return static<array-key, TValue>
|
||||
* @template TNewKey of array-key
|
||||
*
|
||||
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
|
||||
* @return static<($keyBy is string ? array-key : ($keyBy is array ? array-key : TNewKey)), TValue>
|
||||
*/
|
||||
public function keyBy($keyBy);
|
||||
|
||||
@@ -550,7 +556,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Concatenate values of a given key as a string.
|
||||
*
|
||||
* @param callable|string $value
|
||||
* @param (callable(TValue, TKey): mixed)|string $value
|
||||
* @param string|null $glue
|
||||
* @return string
|
||||
*/
|
||||
@@ -565,11 +571,37 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
public function intersect($items);
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items by key.
|
||||
* Intersect the collection with the given items, using the callback.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
|
||||
* @param callable(TValue, TValue): int $callback
|
||||
* @return static
|
||||
*/
|
||||
public function intersectUsing($items, callable $callback);
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items with additional index check.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
||||
* @return static
|
||||
*/
|
||||
public function intersectAssoc($items);
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items with additional index check, using the callback.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
|
||||
* @param callable(TValue, TValue): int $callback
|
||||
* @return static
|
||||
*/
|
||||
public function intersectAssocUsing($items, callable $callback);
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items by key.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
|
||||
* @return static
|
||||
*/
|
||||
public function intersectByKeys($items);
|
||||
|
||||
/**
|
||||
@@ -593,6 +625,13 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*/
|
||||
public function containsOneItem();
|
||||
|
||||
/**
|
||||
* Determine if the collection contains multiple items.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function containsManyItems();
|
||||
|
||||
/**
|
||||
* Join all items from the collection using a string. The final items can use a separate glue string.
|
||||
*
|
||||
@@ -701,8 +740,10 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* 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);
|
||||
|
||||
@@ -815,7 +856,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*
|
||||
* @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
|
||||
* @param TReduceInitial $initial
|
||||
* @return TReduceReturnType
|
||||
* @return TReduceInitial|TReduceReturnType
|
||||
*/
|
||||
public function reduce(callable $callback, $initial = null);
|
||||
|
||||
@@ -862,13 +903,30 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*/
|
||||
public function search($value, $strict = false);
|
||||
|
||||
/**
|
||||
* Get the item before the given item.
|
||||
*
|
||||
* @param TValue|(callable(TValue,TKey): bool) $value
|
||||
* @param bool $strict
|
||||
* @return TValue|null
|
||||
*/
|
||||
public function before($value, $strict = false);
|
||||
|
||||
/**
|
||||
* Get the item after the given item.
|
||||
*
|
||||
* @param TValue|(callable(TValue,TKey): bool) $value
|
||||
* @param bool $strict
|
||||
* @return TValue|null
|
||||
*/
|
||||
public function after($value, $strict = false);
|
||||
|
||||
/**
|
||||
* Shuffle the items in the collection.
|
||||
*
|
||||
* @param int|null $seed
|
||||
* @return static
|
||||
*/
|
||||
public function shuffle($seed = null);
|
||||
public function shuffle();
|
||||
|
||||
/**
|
||||
* Create chunks representing a "sliding window" view of the items in the collection.
|
||||
@@ -936,7 +994,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Get the first item in the collection but throw an exception if no matching items exist.
|
||||
*
|
||||
* @param (callable(TValue, TKey): bool)|string $key
|
||||
* @param (callable(TValue, TKey): bool)|string|null $key
|
||||
* @param mixed $operator
|
||||
* @param mixed $value
|
||||
* @return TValue
|
||||
@@ -1102,7 +1160,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*
|
||||
* @param string|array<array-key, string> $value
|
||||
* @param string|null $key
|
||||
* @return static<int, mixed>
|
||||
* @return static<array-key, mixed>
|
||||
*/
|
||||
public function pluck($value, $key = null);
|
||||
|
||||
@@ -1220,6 +1278,15 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
*/
|
||||
public function toJson($options = 0);
|
||||
|
||||
/**
|
||||
* Get the collection of items as pretty print formatted JSON.
|
||||
*
|
||||
*
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
public function toPrettyJson(int $options = 0);
|
||||
|
||||
/**
|
||||
* Get a CachingIterator instance.
|
||||
*
|
||||
|
||||
+11
-5
@@ -3,14 +3,19 @@
|
||||
namespace Illuminate\Support;
|
||||
|
||||
/**
|
||||
* @mixin \Illuminate\Support\Enumerable
|
||||
* @template TKey of array-key
|
||||
*
|
||||
* @template-covariant TValue
|
||||
*
|
||||
* @mixin \Illuminate\Support\Enumerable<TKey, TValue>
|
||||
* @mixin TValue
|
||||
*/
|
||||
class HigherOrderCollectionProxy
|
||||
{
|
||||
/**
|
||||
* The collection being operated on.
|
||||
*
|
||||
* @var \Illuminate\Support\Enumerable
|
||||
* @var \Illuminate\Support\Enumerable<TKey, TValue>
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
@@ -24,9 +29,8 @@ class HigherOrderCollectionProxy
|
||||
/**
|
||||
* Create a new proxy instance.
|
||||
*
|
||||
* @param \Illuminate\Support\Enumerable $collection
|
||||
* @param \Illuminate\Support\Enumerable<TKey, TValue> $collection
|
||||
* @param string $method
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Enumerable $collection, $method)
|
||||
{
|
||||
@@ -57,7 +61,9 @@ class HigherOrderCollectionProxy
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
return $this->collection->{$this->method}(function ($value) use ($method, $parameters) {
|
||||
return $value->{$method}(...$parameters);
|
||||
return is_string($value)
|
||||
? $value::{$method}(...$parameters)
|
||||
: $value->{$method}(...$parameters);
|
||||
});
|
||||
}
|
||||
}
|
||||
+437
-248
File diff suppressed because it is too large.
Load diff
-1
@@ -19,7 +19,6 @@ class MultipleItemsFoundException extends RuntimeException
|
||||
* @param int $count
|
||||
* @param int $code
|
||||
* @param \Throwable|null $previous
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($count, $code = 0, $previous = null)
|
||||
{
|
||||
|
||||
+158
-97
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Illuminate\Support\Traits;
|
||||
|
||||
use BackedEnum;
|
||||
use CachingIterator;
|
||||
use Closure;
|
||||
use Exception;
|
||||
@@ -11,48 +12,49 @@ use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Enumerable;
|
||||
use Illuminate\Support\HigherOrderCollectionProxy;
|
||||
use InvalidArgumentException;
|
||||
use JsonSerializable;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
use Traversable;
|
||||
use UnexpectedValueException;
|
||||
use UnitEnum;
|
||||
use WeakMap;
|
||||
|
||||
use function Illuminate\Support\enum_value;
|
||||
|
||||
/**
|
||||
* @template TKey of array-key
|
||||
*
|
||||
* @template-covariant TValue
|
||||
*
|
||||
* @property-read HigherOrderCollectionProxy $average
|
||||
* @property-read HigherOrderCollectionProxy $avg
|
||||
* @property-read HigherOrderCollectionProxy $contains
|
||||
* @property-read HigherOrderCollectionProxy $doesntContain
|
||||
* @property-read HigherOrderCollectionProxy $each
|
||||
* @property-read HigherOrderCollectionProxy $every
|
||||
* @property-read HigherOrderCollectionProxy $filter
|
||||
* @property-read HigherOrderCollectionProxy $first
|
||||
* @property-read HigherOrderCollectionProxy $flatMap
|
||||
* @property-read HigherOrderCollectionProxy $groupBy
|
||||
* @property-read HigherOrderCollectionProxy $keyBy
|
||||
* @property-read HigherOrderCollectionProxy $map
|
||||
* @property-read HigherOrderCollectionProxy $max
|
||||
* @property-read HigherOrderCollectionProxy $min
|
||||
* @property-read HigherOrderCollectionProxy $partition
|
||||
* @property-read HigherOrderCollectionProxy $percentage
|
||||
* @property-read HigherOrderCollectionProxy $reject
|
||||
* @property-read HigherOrderCollectionProxy $skipUntil
|
||||
* @property-read HigherOrderCollectionProxy $skipWhile
|
||||
* @property-read HigherOrderCollectionProxy $some
|
||||
* @property-read HigherOrderCollectionProxy $sortBy
|
||||
* @property-read HigherOrderCollectionProxy $sortByDesc
|
||||
* @property-read HigherOrderCollectionProxy $sum
|
||||
* @property-read HigherOrderCollectionProxy $takeUntil
|
||||
* @property-read HigherOrderCollectionProxy $takeWhile
|
||||
* @property-read HigherOrderCollectionProxy $unique
|
||||
* @property-read HigherOrderCollectionProxy $unless
|
||||
* @property-read HigherOrderCollectionProxy $until
|
||||
* @property-read HigherOrderCollectionProxy $when
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $average
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $avg
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $contains
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $doesntContain
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $each
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $every
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $filter
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $first
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $flatMap
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $groupBy
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $hasMany
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $hasSole
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $keyBy
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $last
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $map
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $max
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $min
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $partition
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $percentage
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $reject
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $skipUntil
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $skipWhile
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $some
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $sortBy
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $sortByDesc
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $sum
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $takeUntil
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $takeWhile
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $unique
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $unless
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $until
|
||||
* @property-read HigherOrderCollectionProxy<TKey, TValue> $when
|
||||
*/
|
||||
trait EnumeratesValues
|
||||
{
|
||||
@@ -81,7 +83,10 @@ trait EnumeratesValues
|
||||
'first',
|
||||
'flatMap',
|
||||
'groupBy',
|
||||
'hasMany',
|
||||
'hasSole',
|
||||
'keyBy',
|
||||
'last',
|
||||
'map',
|
||||
'max',
|
||||
'min',
|
||||
@@ -175,6 +180,41 @@ trait EnumeratesValues
|
||||
->map($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection by decoding a JSON string.
|
||||
*
|
||||
* @param string $json
|
||||
* @param int $depth
|
||||
* @param int $flags
|
||||
* @return static<TKey, TValue>
|
||||
*/
|
||||
public static function fromJson($json, $depth = 512, $flags = 0)
|
||||
{
|
||||
return new static(json_decode($json, true, $depth, $flags));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the average value of a given key.
|
||||
*
|
||||
* @param (callable(TValue): float|int)|string|null $callback
|
||||
* @return float|int|null
|
||||
*/
|
||||
public function avg($callback = null)
|
||||
{
|
||||
$callback = $this->valueRetriever($callback);
|
||||
|
||||
$reduced = $this->reduce(static function (&$reduce, $value) use ($callback) {
|
||||
if (! is_null($resolved = $callback($value))) {
|
||||
$reduce[0] += $resolved;
|
||||
$reduce[1]++;
|
||||
}
|
||||
|
||||
return $reduce;
|
||||
}, [0, 0]);
|
||||
|
||||
return $reduced[1] ? $reduced[0] / $reduced[1] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for the "avg" method.
|
||||
*
|
||||
@@ -200,30 +240,25 @@ trait EnumeratesValues
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the items and end the script.
|
||||
* Dump the given arguments and terminate execution.
|
||||
*
|
||||
* @param mixed ...$args
|
||||
* @return never
|
||||
*/
|
||||
public function dd(...$args)
|
||||
{
|
||||
$this->dump(...$args);
|
||||
|
||||
exit(1);
|
||||
dd($this->all(), ...$args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the items.
|
||||
*
|
||||
* @param mixed ...$args
|
||||
* @return $this
|
||||
*/
|
||||
public function dump()
|
||||
public function dump(...$args)
|
||||
{
|
||||
(new Collection(func_get_args()))
|
||||
->push($this->all())
|
||||
->each(function ($item) {
|
||||
VarDumper::dump($item);
|
||||
});
|
||||
dump($this->all(), ...$args);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -298,6 +333,27 @@ trait EnumeratesValues
|
||||
return $this->first($this->operatorForWhere(...func_get_args()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the collection contains multiple items, optionally matching the given criteria.
|
||||
*
|
||||
* @param (callable(TValue, TKey): bool)|string|null $key
|
||||
* @param mixed $operator
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMany($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)
|
||||
->take(2)
|
||||
->count() === 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single key's value from the first matching item in the collection.
|
||||
*
|
||||
@@ -309,11 +365,11 @@ trait EnumeratesValues
|
||||
*/
|
||||
public function value($key, $default = null)
|
||||
{
|
||||
if ($value = $this->firstWhere($key)) {
|
||||
return data_get($value, $key, $default);
|
||||
}
|
||||
$value = $this->first(function ($target) use ($key) {
|
||||
return data_has($target, $key);
|
||||
});
|
||||
|
||||
return value($default);
|
||||
return data_get($value, $key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -321,7 +377,7 @@ trait EnumeratesValues
|
||||
*
|
||||
* @template TEnsureOfType
|
||||
*
|
||||
* @param class-string<TEnsureOfType>|array<array-key, class-string<TEnsureOfType>> $type
|
||||
* @param class-string<TEnsureOfType>|array<array-key, class-string<TEnsureOfType>>|'string'|'int'|'float'|'bool'|'array'|'null' $type
|
||||
* @return static<TKey, TEnsureOfType>
|
||||
*
|
||||
* @throws \UnexpectedValueException
|
||||
@@ -330,7 +386,7 @@ trait EnumeratesValues
|
||||
{
|
||||
$allowedTypes = is_array($type) ? $type : [$type];
|
||||
|
||||
return $this->each(function ($item) use ($allowedTypes) {
|
||||
return $this->each(function ($item, $index) use ($allowedTypes) {
|
||||
$itemType = get_debug_type($item);
|
||||
|
||||
foreach ($allowedTypes as $allowedType) {
|
||||
@@ -340,7 +396,7 @@ trait EnumeratesValues
|
||||
}
|
||||
|
||||
throw new UnexpectedValueException(
|
||||
sprintf("Collection should only include [%s] items, but '%s' found.", implode(', ', $allowedTypes), $itemType)
|
||||
sprintf("Collection should only include [%s] items, but '%s' found at position %d.", implode(', ', $allowedTypes), $itemType, $index)
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -348,6 +404,12 @@ trait EnumeratesValues
|
||||
/**
|
||||
* Determine if the collection is not empty.
|
||||
*
|
||||
* @phpstan-assert-if-true TValue $this->first()
|
||||
* @phpstan-assert-if-true TValue $this->last()
|
||||
*
|
||||
* @phpstan-assert-if-false null $this->first()
|
||||
* @phpstan-assert-if-false null $this->last()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isNotEmpty()
|
||||
@@ -387,7 +449,7 @@ trait EnumeratesValues
|
||||
{
|
||||
$groups = $this->mapToDictionary($callback);
|
||||
|
||||
return $groups->map([$this, 'make']);
|
||||
return $groups->map($this->make(...));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -414,6 +476,10 @@ trait EnumeratesValues
|
||||
*/
|
||||
public function mapInto($class)
|
||||
{
|
||||
if (is_subclass_of($class, BackedEnum::class)) {
|
||||
return $this->map(fn ($value, $key) => $class::from($value));
|
||||
}
|
||||
|
||||
return $this->map(fn ($value, $key) => new $class($value, $key));
|
||||
}
|
||||
|
||||
@@ -428,7 +494,7 @@ trait EnumeratesValues
|
||||
$callback = $this->valueRetriever($callback);
|
||||
|
||||
return $this->map(fn ($value) => $callback($value))
|
||||
->filter(fn ($value) => ! is_null($value))
|
||||
->reject(fn ($value) => is_null($value))
|
||||
->reduce(fn ($result, $value) => is_null($result) || $value < $result ? $value : $result);
|
||||
}
|
||||
|
||||
@@ -442,7 +508,7 @@ trait EnumeratesValues
|
||||
{
|
||||
$callback = $this->valueRetriever($callback);
|
||||
|
||||
return $this->filter(fn ($value) => ! is_null($value))->reduce(function ($result, $item) use ($callback) {
|
||||
return $this->reject(fn ($value) => is_null($value))->reduce(function ($result, $item) use ($callback) {
|
||||
$value = $callback($item);
|
||||
|
||||
return is_null($result) || $value > $result ? $value : $result;
|
||||
@@ -467,26 +533,17 @@ trait EnumeratesValues
|
||||
* Partition the collection into two arrays using the given callback or key.
|
||||
*
|
||||
* @param (callable(TValue, TKey): bool)|TValue|string $key
|
||||
* @param TValue|string|null $operator
|
||||
* @param TValue|null $value
|
||||
* @param mixed $operator
|
||||
* @param mixed $value
|
||||
* @return static<int<0, 1>, static<TKey, TValue>>
|
||||
*/
|
||||
public function partition($key, $operator = null, $value = null)
|
||||
{
|
||||
$passed = [];
|
||||
$failed = [];
|
||||
|
||||
$callback = func_num_args() === 1
|
||||
? $this->valueRetriever($key)
|
||||
: $this->operatorForWhere(...func_get_args());
|
||||
? $this->valueRetriever($key)
|
||||
: $this->operatorForWhere(...func_get_args());
|
||||
|
||||
foreach ($this as $key => $item) {
|
||||
if ($callback($item, $key)) {
|
||||
$passed[$key] = $item;
|
||||
} else {
|
||||
$failed[$key] = $item;
|
||||
}
|
||||
}
|
||||
[$passed, $failed] = Arr::partition($this->getIterator(), $callback);
|
||||
|
||||
return new static([new static($passed), new static($failed)]);
|
||||
}
|
||||
@@ -513,8 +570,10 @@ trait EnumeratesValues
|
||||
/**
|
||||
* Get the sum of the given values.
|
||||
*
|
||||
* @param (callable(TValue): mixed)|string|null $callback
|
||||
* @return mixed
|
||||
* @template TReturnType
|
||||
*
|
||||
* @param (callable(TValue): TReturnType)|string|null $callback
|
||||
* @return ($callback is callable ? TReturnType : mixed)
|
||||
*/
|
||||
public function sum($callback = null)
|
||||
{
|
||||
@@ -767,7 +826,7 @@ trait EnumeratesValues
|
||||
*/
|
||||
public function pipeThrough($callbacks)
|
||||
{
|
||||
return Collection::make($callbacks)->reduce(
|
||||
return (new Collection($callbacks))->reduce(
|
||||
fn ($carry, $callback) => $callback($carry),
|
||||
$this,
|
||||
);
|
||||
@@ -927,15 +986,12 @@ trait EnumeratesValues
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return array_map(function ($value) {
|
||||
if ($value instanceof JsonSerializable) {
|
||||
return $value->jsonSerialize();
|
||||
} elseif ($value instanceof Jsonable) {
|
||||
return json_decode($value->toJson(), true);
|
||||
} elseif ($value instanceof Arrayable) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
return $value;
|
||||
return match (true) {
|
||||
$value instanceof JsonSerializable => $value->jsonSerialize(),
|
||||
$value instanceof Jsonable => json_decode($value->toJson(), true),
|
||||
$value instanceof Arrayable => $value->toArray(),
|
||||
default => $value,
|
||||
};
|
||||
}, $this->all());
|
||||
}
|
||||
|
||||
@@ -950,6 +1006,17 @@ trait EnumeratesValues
|
||||
return json_encode($this->jsonSerialize(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of items as pretty print formatted JSON.
|
||||
*
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
public function toPrettyJson(int $options = 0)
|
||||
{
|
||||
return $this->toJson(JSON_PRETTY_PRINT | $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a CachingIterator instance.
|
||||
*
|
||||
@@ -969,8 +1036,8 @@ trait EnumeratesValues
|
||||
public function __toString()
|
||||
{
|
||||
return $this->escapeWhenCastingToString
|
||||
? e($this->toJson())
|
||||
: $this->toJson();
|
||||
? e($this->toJson())
|
||||
: $this->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1022,20 +1089,9 @@ trait EnumeratesValues
|
||||
*/
|
||||
protected function getArrayableItems($items)
|
||||
{
|
||||
if (is_array($items)) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
return match (true) {
|
||||
$items instanceof WeakMap => throw new InvalidArgumentException('Collections can not be created using instances of WeakMap.'),
|
||||
$items instanceof Enumerable => $items->all(),
|
||||
$items instanceof Arrayable => $items->toArray(),
|
||||
$items instanceof Traversable => iterator_to_array($items),
|
||||
$items instanceof Jsonable => json_decode($items->toJson(), true),
|
||||
$items instanceof JsonSerializable => (array) $items->jsonSerialize(),
|
||||
$items instanceof UnitEnum => [$items],
|
||||
default => (array) $items,
|
||||
};
|
||||
return is_null($items) || is_scalar($items) || $items instanceof UnitEnum
|
||||
? Arr::wrap($items)
|
||||
: Arr::from($items);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1065,10 +1121,15 @@ trait EnumeratesValues
|
||||
}
|
||||
|
||||
return function ($item) use ($key, $operator, $value) {
|
||||
$retrieved = data_get($item, $key);
|
||||
$retrieved = enum_value(data_get($item, $key));
|
||||
$value = enum_value($value);
|
||||
|
||||
$strings = array_filter([$retrieved, $value], function ($value) {
|
||||
return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
|
||||
return match (true) {
|
||||
is_string($value) => true,
|
||||
$value instanceof \Stringable => true,
|
||||
default => false,
|
||||
};
|
||||
});
|
||||
|
||||
if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Support\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\UseResource;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseResourceCollection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use LogicException;
|
||||
use ReflectionClass;
|
||||
|
||||
trait TransformsToResourceCollection
|
||||
{
|
||||
/**
|
||||
* Create a new resource collection instance for the given resource.
|
||||
*
|
||||
* @param class-string<\Illuminate\Http\Resources\Json\JsonResource>|null $resourceClass
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function toResourceCollection(?string $resourceClass = null): ResourceCollection
|
||||
{
|
||||
if ($resourceClass === null) {
|
||||
return $this->guessResourceCollection();
|
||||
}
|
||||
|
||||
return $resourceClass::collection($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess the resource collection for the items.
|
||||
*
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
protected function guessResourceCollection(): ResourceCollection
|
||||
{
|
||||
if ($this->isEmpty()) {
|
||||
return new ResourceCollection($this);
|
||||
}
|
||||
|
||||
$model = $this->items[0] ?? null;
|
||||
|
||||
throw_unless(is_object($model), LogicException::class, 'Resource collection guesser expects the collection to contain objects.');
|
||||
|
||||
/** @var class-string<Model> $className */
|
||||
$className = get_class($model);
|
||||
|
||||
throw_unless(method_exists($className, 'guessResourceName'), LogicException::class, sprintf('Expected class %s to implement guessResourceName method. Make sure the model uses the TransformsToResource trait.', $className));
|
||||
|
||||
$useResourceCollection = $this->resolveResourceCollectionFromAttribute($className);
|
||||
|
||||
if ($useResourceCollection !== null && class_exists($useResourceCollection)) {
|
||||
return new $useResourceCollection($this);
|
||||
}
|
||||
|
||||
$useResource = $this->resolveResourceFromAttribute($className);
|
||||
|
||||
if ($useResource !== null && class_exists($useResource)) {
|
||||
return $useResource::collection($this);
|
||||
}
|
||||
|
||||
$resourceClasses = $className::guessResourceName();
|
||||
|
||||
foreach ($resourceClasses as $resourceClass) {
|
||||
$resourceCollection = $resourceClass.'Collection';
|
||||
|
||||
if (is_string($resourceCollection) && class_exists($resourceCollection)) {
|
||||
return new $resourceCollection($this);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($resourceClasses as $resourceClass) {
|
||||
if (is_string($resourceClass) && class_exists($resourceClass)) {
|
||||
return $resourceClass::collection($this);
|
||||
}
|
||||
}
|
||||
|
||||
throw new LogicException(sprintf('Failed to find resource class for model [%s].', $className));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource class from the class attribute.
|
||||
*
|
||||
* @param class-string<\Illuminate\Http\Resources\Json\JsonResource> $class
|
||||
* @return class-string<*>|null
|
||||
*/
|
||||
protected function resolveResourceFromAttribute(string $class): ?string
|
||||
{
|
||||
if (! class_exists($class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributes = (new ReflectionClass($class))->getAttributes(UseResource::class);
|
||||
|
||||
return $attributes !== []
|
||||
? $attributes[0]->newInstance()->class
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource collection class from the class attribute.
|
||||
*
|
||||
* @param class-string<\Illuminate\Http\Resources\Json\ResourceCollection> $class
|
||||
* @return class-string<*>|null
|
||||
*/
|
||||
protected function resolveResourceCollectionFromAttribute(string $class): ?string
|
||||
{
|
||||
if (! class_exists($class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributes = (new ReflectionClass($class))->getAttributes(UseResourceCollection::class);
|
||||
|
||||
return $attributes !== []
|
||||
? $attributes[0]->newInstance()->class
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -14,26 +14,31 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.1",
|
||||
"illuminate/conditionable": "^10.0",
|
||||
"illuminate/contracts": "^10.0",
|
||||
"illuminate/macroable": "^10.0"
|
||||
"php": "^8.2",
|
||||
"illuminate/conditionable": "^12.0",
|
||||
"illuminate/contracts": "^12.0",
|
||||
"illuminate/macroable": "^12.0",
|
||||
"symfony/polyfill-php83": "^1.33",
|
||||
"symfony/polyfill-php84": "^1.33",
|
||||
"symfony/polyfill-php85": "^1.33"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Illuminate\\Support\\": ""
|
||||
},
|
||||
"files": [
|
||||
"functions.php",
|
||||
"helpers.php"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "10.x-dev"
|
||||
"dev-master": "12.x-dev"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"symfony/var-dumper": "Required to use the dump method (^6.2)."
|
||||
"illuminate/http": "Required to convert collections to API resources (^12.0).",
|
||||
"symfony/var-dumper": "Required to use the dump method (^7.2)."
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
|
||||
+73
-6
@@ -13,7 +13,7 @@ if (! function_exists('collect')) {
|
||||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value
|
||||
* @return \Illuminate\Support\Collection<TKey, TValue>
|
||||
*/
|
||||
function collect($value = [])
|
||||
function collect($value = []): Collection
|
||||
{
|
||||
return new Collection($value);
|
||||
}
|
||||
@@ -34,6 +34,36 @@ if (! function_exists('data_fill')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('data_has')) {
|
||||
/**
|
||||
* Determine if a key / property exists on an array or object using "dot" notation.
|
||||
*
|
||||
* @param mixed $target
|
||||
* @param string|array|int|null $key
|
||||
* @return bool
|
||||
*/
|
||||
function data_has($target, $key): bool
|
||||
{
|
||||
if (is_null($key) || $key === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = is_array($key) ? $key : explode('.', $key);
|
||||
|
||||
foreach ($key as $segment) {
|
||||
if (Arr::accessible($target) && Arr::exists($target, $segment)) {
|
||||
$target = $target[$segment];
|
||||
} elseif (is_object($target) && property_exists($target, $segment)) {
|
||||
$target = $target->{$segment};
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('data_get')) {
|
||||
/**
|
||||
* Get an item from an array or object using "dot" notation.
|
||||
@@ -74,6 +104,15 @@ if (! function_exists('data_get')) {
|
||||
return in_array('*', $key) ? Arr::collapse($result) : $result;
|
||||
}
|
||||
|
||||
$segment = match ($segment) {
|
||||
'\*' => '*',
|
||||
'\{first}' => '{first}',
|
||||
'{first}' => array_key_first(Arr::from($target)),
|
||||
'\{last}' => '{last}',
|
||||
'{last}' => array_key_last(Arr::from($target)),
|
||||
default => $segment,
|
||||
};
|
||||
|
||||
if (Arr::accessible($target) && Arr::exists($target, $segment)) {
|
||||
$target = $target[$segment];
|
||||
} elseif (is_object($target) && isset($target->{$segment})) {
|
||||
@@ -194,7 +233,7 @@ if (! function_exists('head')) {
|
||||
*/
|
||||
function head($array)
|
||||
{
|
||||
return reset($array);
|
||||
return empty($array) ? false : array_first($array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +246,7 @@ if (! function_exists('last')) {
|
||||
*/
|
||||
function last($array)
|
||||
{
|
||||
return end($array);
|
||||
return empty($array) ? false : array_last($array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,12 +254,40 @@ if (! function_exists('value')) {
|
||||
/**
|
||||
* Return the default value of the given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed ...$args
|
||||
* @return mixed
|
||||
* @template TValue
|
||||
* @template TArgs
|
||||
*
|
||||
* @param TValue|\Closure(TArgs): TValue $value
|
||||
* @param TArgs ...$args
|
||||
* @return TValue
|
||||
*/
|
||||
function value($value, ...$args)
|
||||
{
|
||||
return $value instanceof Closure ? $value(...$args) : $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('when')) {
|
||||
/**
|
||||
* Return a value if the given condition is true.
|
||||
*
|
||||
* @template TValue
|
||||
* @template TArgs
|
||||
* @template TDefault
|
||||
*
|
||||
* @param mixed $condition
|
||||
* @param TValue|\Closure(TArgs): TValue $value
|
||||
* @param TDefault|\Closure(): TDefault $default
|
||||
* @return ($condition is true|positive-int|non-falsy-string|non-empty-array ? TValue : ($condition is callable ? TValue|TDefault : TDefault))
|
||||
*/
|
||||
function when($condition, $value, $default = null)
|
||||
{
|
||||
$condition = $condition instanceof Closure ? $condition() : $condition;
|
||||
|
||||
if ($condition) {
|
||||
return value($value, $condition);
|
||||
}
|
||||
|
||||
return value($default, $condition);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user