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

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

View File

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

View File

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

View File

@@ -625,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.
*
@@ -733,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);
@@ -985,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

View File

@@ -77,7 +77,9 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* @param int $from
* @param int $to
* @param int $step
* @return static<int, int>
* @return ($step is zero ? never : static<int, int>)
*
* @throws \InvalidArgumentException
*/
public static function range($from, $to, $step = 1)
{
@@ -303,23 +305,18 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Cross join the given iterables, returning all possible permutations.
*
* @template TCrossJoinKey
* @template TCrossJoinValue
*
* @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$arrays
* @return static<int, array<int, TValue|TCrossJoinValue>>
* {@inheritDoc}
*/
#[\Override]
public function crossJoin(...$arrays)
{
return $this->passthru('crossJoin', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Count the number of items in the collection by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key|\UnitEnum)|string|null $countBy
* @param (callable(TValue, TKey): (array-key|\UnitEnum))|string|null $countBy
* @return static<array-key, int>
*/
public function countBy($countBy = null)
@@ -346,110 +343,84 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Get the items that are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function diff($items)
{
return $this->passthru('diff', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items that are not present in 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
* {@inheritDoc}
*/
#[\Override]
public function diffUsing($items, callable $callback)
{
return $this->passthru('diffUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys and values are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffAssoc($items)
{
return $this->passthru('diffAssoc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys and values are not present in the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffAssocUsing($items, callable $callback)
{
return $this->passthru('diffAssocUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys are not present in the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffKeys($items)
{
return $this->passthru('diffKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get the items whose keys are not present in the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function diffKeysUsing($items, callable $callback)
{
return $this->passthru('diffKeysUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Retrieve duplicate items.
*
* @template TMapValue
*
* @param (callable(TValue): TMapValue)|string|null $callback
* @param bool $strict
* @return static
* {@inheritDoc}
*/
#[\Override]
public function duplicates($callback = null, $strict = false)
{
return $this->passthru('duplicates', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Retrieve duplicate items using strict comparison.
*
* @template TMapValue
*
* @param (callable(TValue): TMapValue)|string|null $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function duplicatesStrict($callback = null)
{
return $this->passthru('duplicatesStrict', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Get all items except for those with the specified keys.
*
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys
* @return static
* {@inheritDoc}
*/
#[\Override]
public function except($keys)
{
return $this->passthru('except', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -565,26 +536,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Group an associative array by a field or using a callback.
* {@inheritDoc}
*
* @template TGroupKey of array-key
* @template TGroupKey of array-key|\UnitEnum|\Stringable
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>>
* @return static<
* ($groupBy is (array|string)
* ? array-key
* : (TGroupKey is \UnitEnum ? array-key : (TGroupKey is \Stringable ? string : TGroupKey))),
* static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>
* >
*/
#[\Override]
public function groupBy($groupBy, $preserveKeys = false)
{
return $this->passthru('groupBy', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Key an associative array by a field or using a callback.
*
* @template TNewKey of array-key
* @template TNewKey of array-key|\UnitEnum
*
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
* @return static<($keyBy is string ? array-key : ($keyBy is array ? array-key : TNewKey)), TValue>
* @return static<($keyBy is (array|string) ? array-key : (TNewKey is \UnitEnum ? array-key : TNewKey)), TValue>
*/
public function keyBy($keyBy)
{
@@ -655,60 +631,48 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Intersect the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersect($items)
{
return $this->passthru('intersect', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* 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
* {@inheritDoc}
*/
#[\Override]
public function intersectUsing($items, callable $callback)
{
return $this->passthru('intersectUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items with additional index check.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectAssoc($items)
{
return $this->passthru('intersectAssoc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* 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
* {@inheritDoc}
*/
#[\Override]
public function intersectAssocUsing($items, callable $callback)
{
return $this->passthru('intersectAssocUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Intersect the collection with the given items by key.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function intersectByKeys($items)
{
return $this->passthru('intersectByKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -724,11 +688,26 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Determine if the collection contains a single item.
*
* @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->take(2)->count() === 1;
return $this->hasSole($callback);
}
/**
* Determine if the collection contains multiple items.
*
* @return bool
*
* @deprecated 12.50.0 Use the `hasMany()` method instead.
*/
public function containsManyItems(): bool
{
return $this->hasMany();
}
/**
@@ -831,19 +810,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Run a dictionary map over the items.
*
* The callback should return an associative array with a single key/value pair.
*
* @template TMapToDictionaryKey of array-key
* @template TMapToDictionaryValue
*
* @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback
* @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>>
* {@inheritDoc}
*/
#[\Override]
public function mapToDictionary(callable $callback)
{
return $this->passthru('mapToDictionary', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -867,27 +839,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Merge the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function merge($items)
{
return $this->passthru('merge', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Recursively merge the collection with the given items.
*
* @template TMergeRecursiveValue
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items
* @return static<TKey, TValue|TMergeRecursiveValue>
* {@inheritDoc}
*/
#[\Override]
public function mergeRecursive($items)
{
return $this->passthru('mergeRecursive', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -898,7 +864,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*/
public function multiply(int $multiplier)
{
return $this->passthru('multiply', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -935,14 +901,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Union the collection with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function union($items)
{
return $this->passthru('union', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -950,10 +914,16 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* @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.');
}
return new static(function () use ($step, $offset) {
$position = 0;
@@ -1058,11 +1028,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Get one or a specified number of items randomly from the collection.
*
* @param int|null $number
* @param bool $preserveKeys
* @return static<int, TValue>|TValue
*
* @throws \InvalidArgumentException
*/
public function random($number = null)
public function random($number = null, $preserveKeys = false)
{
$result = $this->collect()->random(...func_get_args());
@@ -1097,24 +1068,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Recursively replace the collection items with the given items.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
* {@inheritDoc}
*/
#[\Override]
public function replaceRecursive($items)
{
return $this->passthru('replaceRecursive', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Reverse items order.
*
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function reverse()
{
return $this->passthru('reverse', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -1203,24 +1171,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Shuffle the items in the collection.
*
* @return static<TKey, TValue>
* {@inheritDoc}
*/
#[\Override]
public function shuffle()
{
return $this->passthru('shuffle', []);
return $this->passthru(__FUNCTION__, []);
}
/**
* 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.');
}
return new static(function () use ($size, $step) {
$iterator = $this->getIterator();
@@ -1313,16 +1288,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Get a slice of items from the enumerable.
*
* @param int $offset
* @param int|null $length
* @return static
* {@inheritDoc}
*/
#[\Override]
public function slice($offset, $length = null)
{
if ($offset < 0 || $length < 0) {
return $this->passthru('slice', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
$instance = $this->skip($offset);
@@ -1331,20 +1303,24 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Split a collection into a certain number of groups.
* {@inheritDoc}
*
* @param int $numberOfGroups
* @return static<int, static>
* @throws \InvalidArgumentException
*/
#[\Override]
public function split($numberOfGroups)
{
return $this->passthru('split', func_get_args());
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* 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
@@ -1366,10 +1342,31 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
->sole();
}
/**
* Determine if the collection contains a single item or a single item 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)
->take(2)
->count() === 1;
}
/**
* 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
@@ -1439,10 +1436,16 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Split a collection into a certain number of groups, and fill the first groups completely.
*
* @param int $numberOfGroups
* @return static<int, static>
* @return ($numberOfGroups is positive-int ? static<int, static> : never)
*
* @throws \InvalidArgumentException
*/
public function splitIn($numberOfGroups)
{
if ($numberOfGroups < 1) {
throw new InvalidArgumentException('Number of groups must be at least 1.');
}
return $this->chunk((int) ceil($this->count() / $numberOfGroups));
}
@@ -1484,84 +1487,66 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Sort through each item with a callback.
*
* @param (callable(TValue, TValue): int)|null|int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sort($callback = null)
{
return $this->passthru('sort', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort items in descending order.
*
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortDesc($options = SORT_REGULAR)
{
return $this->passthru('sortDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection using the given callback.
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @param bool $descending
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
{
return $this->passthru('sortBy', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection in descending order using the given callback.
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->passthru('sortByDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys.
*
* @param int $options
* @param bool $descending
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeys($options = SORT_REGULAR, $descending = false)
{
return $this->passthru('sortKeys', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys in descending order.
*
* @param int $options
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeysDesc($options = SORT_REGULAR)
{
return $this->passthru('sortKeysDesc', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
* Sort the collection keys using a callback.
*
* @param callable(TKey, TKey): int $callback
* @return static
* {@inheritDoc}
*/
#[\Override]
public function sortKeysUsing(callable $callback)
{
return $this->passthru('sortKeysUsing', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
/**
@@ -1630,17 +1615,22 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Take items in the collection until a given point in time.
* Take items in the collection until a given point in time, with an optional callback on timeout.
*
* @param \DateTimeInterface $timeout
* @param callable(TValue|null, TKey|null): mixed|null $callback
* @return static<TKey, TValue>
*/
public function takeUntilTimeout(DateTimeInterface $timeout)
public function takeUntilTimeout(DateTimeInterface $timeout, ?callable $callback = null)
{
$timeout = $timeout->getTimestamp();
return new static(function () use ($timeout) {
return new static(function () use ($timeout, $callback) {
if ($this->now() >= $timeout) {
if ($callback) {
$callback(null, null);
}
return;
}
@@ -1648,6 +1638,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
yield $key => $value;
if ($this->now() >= $timeout) {
if ($callback) {
$callback($value, $key);
}
break;
}
}
@@ -1710,21 +1704,21 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Flatten a multi-dimensional associative array with dots.
*
* @param int $depth
* @return static
*/
public function dot()
public function dot($depth = INF)
{
return $this->passthru('dot', []);
return $this->passthru(__FUNCTION__, [$depth]);
}
/**
* Convert a flatten "dot" notation array into an expanded array.
*
* @return static
* {@inheritDoc}
*/
#[\Override]
public function undot()
{
return $this->passthru('undot', []);
return $this->passthru(__FUNCTION__, []);
}
/**
@@ -1830,18 +1824,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
}
/**
* Pad collection to the specified length with a value.
*
* @template TPadValue
*
* @param int $size
* @param TPadValue $value
* @return static<int, TValue|TPadValue>
* {@inheritDoc}
*/
#[\Override]
public function pad($size, $value)
{
if ($size < 0) {
return $this->passthru('pad', func_get_args());
return $this->passthru(__FUNCTION__, func_get_args());
}
return new static(function () use ($size, $value) {

View File

@@ -33,6 +33,8 @@ use function Illuminate\Support\enum_value;
* @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
@@ -81,6 +83,8 @@ trait EnumeratesValues
'first',
'flatMap',
'groupBy',
'hasMany',
'hasSole',
'keyBy',
'last',
'map',
@@ -329,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.
*
@@ -340,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);
}
/**
@@ -961,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());
}

View File

@@ -2,9 +2,12 @@
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
{
@@ -47,6 +50,18 @@ trait TransformsToResourceCollection
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) {
@@ -65,4 +80,42 @@ trait TransformsToResourceCollection
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;
}
}

View File

@@ -18,6 +18,7 @@
"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"
},

View File

@@ -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.
@@ -241,10 +271,14 @@ if (! function_exists('when')) {
/**
* Return a value if the given condition is true.
*
* @template TValue
* @template TArgs
* @template TDefault
*
* @param mixed $condition
* @param \Closure|mixed $value
* @param \Closure|mixed $default
* @return mixed
* @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)
{