Framework Update
This commit is contained in:
+37
-4
@@ -617,7 +617,7 @@ class Arr
|
||||
*
|
||||
* @param array $array
|
||||
* @param int|null $number
|
||||
* @param bool|false $preserveKeys
|
||||
* @param bool $preserveKeys
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
@@ -731,6 +731,18 @@ class Arr
|
||||
return Collection::make($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
|
||||
*/
|
||||
public static function sortDesc($array, $callback = null)
|
||||
{
|
||||
return Collection::make($array)->sortByDesc($callback)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively sort an array by keys and values.
|
||||
*
|
||||
@@ -783,6 +795,29 @@ class Arr
|
||||
return implode(' ', $classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditionally compile styles from an array into a style list.
|
||||
*
|
||||
* @param array $array
|
||||
* @return string
|
||||
*/
|
||||
public static function toCssStyles($array)
|
||||
{
|
||||
$styleList = static::wrap($array);
|
||||
|
||||
$styles = [];
|
||||
|
||||
foreach ($styleList as $class => $constraint) {
|
||||
if (is_numeric($class)) {
|
||||
$styles[] = Str::finish($constraint, ';');
|
||||
} elseif ($constraint) {
|
||||
$styles[] = Str::finish($class, ';');
|
||||
}
|
||||
}
|
||||
|
||||
return implode(' ', $styles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the array using the given callback.
|
||||
*
|
||||
@@ -803,9 +838,7 @@ class Arr
|
||||
*/
|
||||
public static function whereNotNull($array)
|
||||
{
|
||||
return static::where($array, function ($value) {
|
||||
return ! is_null($value);
|
||||
});
|
||||
return static::where($array, fn ($value) => ! is_null($value));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+46
-21
@@ -84,11 +84,9 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
{
|
||||
$callback = $this->valueRetriever($callback);
|
||||
|
||||
$items = $this->map(function ($value) use ($callback) {
|
||||
return $callback($value);
|
||||
})->filter(function ($value) {
|
||||
return ! is_null($value);
|
||||
});
|
||||
$items = $this
|
||||
->map(fn ($value) => $callback($value))
|
||||
->filter(fn ($value) => ! is_null($value));
|
||||
|
||||
if ($count = $items->count()) {
|
||||
return $items->sum() / $count;
|
||||
@@ -349,14 +347,10 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
protected function duplicateComparator($strict)
|
||||
{
|
||||
if ($strict) {
|
||||
return function ($a, $b) {
|
||||
return $a === $b;
|
||||
};
|
||||
return fn ($a, $b) => $a === $b;
|
||||
}
|
||||
|
||||
return function ($a, $b) {
|
||||
return $a == $b;
|
||||
};
|
||||
return fn ($a, $b) => $a == $b;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,7 +373,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Run a filter over each of the items.
|
||||
*
|
||||
* @param (callable(TValue, TKey): bool)|null $callback
|
||||
* @param (callable(TValue, TKey): bool)|null $callback
|
||||
* @return static
|
||||
*/
|
||||
public function filter(callable $callback = null)
|
||||
@@ -627,6 +621,41 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
return new static(array_intersect($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
return new static(array_uintersect($this->items, $this->getArrayableItems($items), $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)
|
||||
{
|
||||
return new static(array_intersect_assoc($this->items, $this->getArrayableItems($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)
|
||||
{
|
||||
return new static(array_intersect_uassoc($this->items, $this->getArrayableItems($items), $callback));
|
||||
}
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items by key.
|
||||
*
|
||||
@@ -719,7 +748,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
*
|
||||
* @param string|int|array<array-key, string> $value
|
||||
* @param string|null $key
|
||||
* @return static<int, mixed>
|
||||
* @return static<array-key, mixed>
|
||||
*/
|
||||
public function pluck($value, $key = null)
|
||||
{
|
||||
@@ -872,7 +901,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
/**
|
||||
* Get the items with the specified keys.
|
||||
*
|
||||
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
|
||||
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string|null $keys
|
||||
* @return static
|
||||
*/
|
||||
public function only($keys)
|
||||
@@ -1598,13 +1627,9 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
|
||||
*/
|
||||
public function zip($items)
|
||||
{
|
||||
$arrayableItems = array_map(function ($items) {
|
||||
return $this->getArrayableItems($items);
|
||||
}, func_get_args());
|
||||
$arrayableItems = array_map(fn ($items) => $this->getArrayableItems($items), func_get_args());
|
||||
|
||||
$params = array_merge([function () {
|
||||
return new static(func_get_args());
|
||||
}, $this->items], $arrayableItems);
|
||||
$params = array_merge([fn () => new static(func_get_args()), $this->items], $arrayableItems);
|
||||
|
||||
return new static(array_map(...$params));
|
||||
}
|
||||
@@ -1646,7 +1671,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): mixed)|string|null $countBy
|
||||
* @param (callable(TValue, TKey): array-key)|string|null $countBy
|
||||
* @return static<array-key, int>
|
||||
*/
|
||||
public function countBy($countBy = null)
|
||||
|
||||
+3
-4
@@ -51,11 +51,10 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Wrap the given value in a collection if applicable.
|
||||
*
|
||||
* @template TWrapKey of array-key
|
||||
* @template TWrapValue
|
||||
*
|
||||
* @param iterable<TWrapKey, TWrapValue> $value
|
||||
* @return static<TWrapKey, TWrapValue>
|
||||
* @param iterable<array-key, TWrapValue>|TWrapValue $value
|
||||
* @return static<array-key, TWrapValue>
|
||||
*/
|
||||
public static function wrap($value);
|
||||
|
||||
@@ -1168,7 +1167,7 @@ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable,
|
||||
/**
|
||||
* Count the number of items in the collection by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): mixed)|string|null $countBy
|
||||
* @param (callable(TValue, TKey): array-key)|string|null $countBy
|
||||
* @return static<array-key, int>
|
||||
*/
|
||||
public function countBy($countBy = null);
|
||||
|
||||
+47
-8
@@ -292,7 +292,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
|
||||
/**
|
||||
* Count the number of items in the collection by a field or using a callback.
|
||||
*
|
||||
* @param (callable(TValue, TKey): mixed)|string|null $countBy
|
||||
* @param (callable(TValue, TKey): array-key)|string|null $countBy
|
||||
* @return static<array-key, int>
|
||||
*/
|
||||
public function countBy($countBy = null)
|
||||
@@ -430,9 +430,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
|
||||
public function filter(callable $callback = null)
|
||||
{
|
||||
if (is_null($callback)) {
|
||||
$callback = function ($value) {
|
||||
return (bool) $value;
|
||||
};
|
||||
$callback = fn ($value) => (bool) $value;
|
||||
}
|
||||
|
||||
return new static(function () use ($callback) {
|
||||
@@ -632,6 +630,41 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
|
||||
return $this->passthru('intersect', 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
|
||||
*/
|
||||
public function intersectUsing()
|
||||
{
|
||||
return $this->passthru('intersectUsing', 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
|
||||
*/
|
||||
public function intersectAssoc($items)
|
||||
{
|
||||
return $this->passthru('intersectAssoc', 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
|
||||
*/
|
||||
public function intersectAssocUsing($items, callable $callback)
|
||||
{
|
||||
return $this->passthru('intersectAssocUsing', func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Intersect the collection with the given items by key.
|
||||
*
|
||||
@@ -1465,9 +1498,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
|
||||
/** @var callable(TValue, TKey): bool $callback */
|
||||
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);
|
||||
|
||||
return $this->takeUntil(function ($item, $key) use ($callback) {
|
||||
return ! $callback($item, $key);
|
||||
});
|
||||
return $this->takeUntil(fn ($item, $key) => ! $callback($item, $key));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1636,7 +1667,15 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
|
||||
return new ArrayIterator($source);
|
||||
}
|
||||
|
||||
return $source();
|
||||
if (is_callable($source)) {
|
||||
$maybeTraversable = $source();
|
||||
|
||||
return $maybeTraversable instanceof Traversable
|
||||
? $maybeTraversable
|
||||
: new ArrayIterator(Arr::wrap($maybeTraversable));
|
||||
}
|
||||
|
||||
return new ArrayIterator((array) $source);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -114,11 +114,10 @@ trait EnumeratesValues
|
||||
/**
|
||||
* Wrap the given value in a collection if applicable.
|
||||
*
|
||||
* @template TWrapKey of array-key
|
||||
* @template TWrapValue
|
||||
*
|
||||
* @param iterable<TWrapKey, TWrapValue> $value
|
||||
* @return static<TWrapKey, TWrapValue>
|
||||
* @param iterable<array-key, TWrapValue>|TWrapValue $value
|
||||
* @return static<array-key, TWrapValue>
|
||||
*/
|
||||
public static function wrap($value)
|
||||
{
|
||||
|
||||
+2
-2
@@ -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 = null)
|
||||
function collect($value = [])
|
||||
{
|
||||
return new Collection($value);
|
||||
}
|
||||
@@ -180,7 +180,7 @@ if (! function_exists('value')) {
|
||||
* Return the default value of the given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $args
|
||||
* @param mixed ...$args
|
||||
* @return mixed
|
||||
*/
|
||||
function value($value, ...$args)
|
||||
|
||||
Reference in New Issue
Block a user