[Web] update directorytree/ldaprecord

This commit is contained in:
FreddleSpl0it
2024-02-20 10:30:11 +01:00
parent 40146839ef
commit d479d18507
481 changed files with 13919 additions and 6171 deletions
@@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [Ubuntu, macOS]
php: [7.3, 7.4, 8.0, 8.1]
php: [8.0, 8.1, 8.2]
include:
- os: Ubuntu
@@ -25,7 +25,7 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
uses: actions/checkout@v1
- name: Setup PHP
uses: shivammathur/setup-php@v2
+1 -2
View File
@@ -10,7 +10,7 @@
}
],
"require": {
"php": "^7.3|^8.0",
"php": "^8.0",
"symfony/var-dumper": "^3.4 || ^4.0 || ^5.0 || ^6.0"
},
"require-dev": {
@@ -33,7 +33,6 @@
"tests/files/Support/HtmlString.php",
"tests/files/Support/HigherOrderTapProxy.php",
"tests/files/Support/Str.php",
"tests/files/Support/Traits/Conditionable.php",
"tests/files/Support/Stringable.php",
"tests/files/Support/ItemNotFoundException.php",
"tests/files/Support/MultipleItemsFoundException.php",
@@ -0,0 +1,109 @@
<?php
namespace Tightenco\Collect\Conditionable;
class HigherOrderWhenProxy
{
/**
* The target being conditionally operated on.
*
* @var mixed
*/
protected $target;
/**
* The condition for proxying.
*
* @var bool
*/
protected $condition;
/**
* Indicates whether the proxy has a condition.
*
* @var bool
*/
protected $hasCondition = false;
/**
* Determine whether the condition should be negated.
*
* @var bool
*/
protected $negateConditionOnCapture;
/**
* Create a new proxy instance.
*
* @param mixed $target
* @return void
*/
public function __construct($target)
{
$this->target = $target;
}
/**
* Set the condition on the proxy.
*
* @param bool $condition
* @return $this
*/
public function condition($condition)
{
[$this->condition, $this->hasCondition] = [$condition, true];
return $this;
}
/**
* Indicate that the condition should be negated.
*
* @return $this
*/
public function negateConditionOnCapture()
{
$this->negateConditionOnCapture = true;
return $this;
}
/**
* Proxy accessing an attribute onto the target.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if (! $this->hasCondition) {
$condition = $this->target->{$key};
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
}
return $this->condition
? $this->target->{$key}
: $this->target;
}
/**
* Proxy a method call on the target.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (! $this->hasCondition) {
$condition = $this->target->{$method}(...$parameters);
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
}
return $this->condition
? $this->target->{$method}(...$parameters)
: $this->target;
}
}
@@ -2,12 +2,16 @@
namespace Tightenco\Collect\Contracts\Support;
/**
* @template TKey of array-key
* @template TValue
*/
interface Arrayable
{
/**
* Get the instance as an array.
*
* @return array
* @return array<TKey, TValue>
*/
public function toArray();
}
@@ -2,6 +2,7 @@
namespace Tightenco\Collect\Support;
use ArgumentCountError;
use ArrayAccess;
use Tightenco\Collect\Support\Traits\Macroable;
use InvalidArgumentException;
@@ -25,7 +26,7 @@ class Arr
* Add an element to an array using "dot" notation if it doesn't exist.
*
* @param array $array
* @param string $key
* @param string|int|float $key
* @param mixed $value
* @return array
*/
@@ -142,7 +143,7 @@ class Arr
* Get all of the given array except for a specified array of keys.
*
* @param array $array
* @param array|string $keys
* @param array|string|int|float $keys
* @return array
*/
public static function except($array, $keys)
@@ -169,6 +170,10 @@ class Arr
return $array->offsetExists($key);
}
if (is_float($key)) {
$key = (string) $key;
}
return array_key_exists($key, $array);
}
@@ -252,7 +257,7 @@ class Arr
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array|string $keys
* @param array|string|int|float $keys
* @return void
*/
public static function forget(&$array, $keys)
@@ -281,7 +286,7 @@ class Arr
while (count($parts) > 1) {
$part = array_shift($parts);
if (isset($array[$part]) && is_array($array[$part])) {
if (isset($array[$part]) && static::accessible($array[$part])) {
$array = &$array[$part];
} else {
continue 2;
@@ -314,7 +319,7 @@ class Arr
return $array[$key];
}
if (strpos($key, '.') === false) {
if (! str_contains($key, '.')) {
return $array[$key] ?? value($default);
}
@@ -423,6 +428,59 @@ class Arr
return ! self::isAssoc($array);
}
/**
* Join all items using a string. The final items can use a separate glue string.
*
* @param array $array
* @param string $glue
* @param string $finalGlue
* @return string
*/
public static function join($array, $glue, $finalGlue = '')
{
if ($finalGlue === '') {
return implode($glue, $array);
}
if (count($array) === 0) {
return '';
}
if (count($array) === 1) {
return end($array);
}
$finalItem = array_pop($array);
return implode($glue, $array).$finalGlue.$finalItem;
}
/**
* Key an associative array by a field or using a callback.
*
* @param array $array
* @param callable|array|string $keyBy
* @return array
*/
public static function keyBy($array, $keyBy)
{
return Collection::make($array)->keyBy($keyBy)->all();
}
/**
* Prepend the key names of an associative array.
*
* @param array $array
* @param string $prependWith
* @return array
*/
public static function prependKeysWith($array, $prependWith)
{
return Collection::make($array)->mapWithKeys(function ($item, $key) use ($prependWith) {
return [$prependWith.$key => $item];
})->all();
}
/**
* Get a subset of the items from the given array.
*
@@ -487,6 +545,26 @@ class Arr
return [$value, $key];
}
/**
* Run a map over each of the items in the array.
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function map(array $array, callable $callback)
{
$keys = array_keys($array);
try {
$items = array_map($callback, $array, $keys);
} catch (ArgumentCountError) {
$items = array_map($callback, $array);
}
return array_combine($keys, $items);
}
/**
* Push an item onto the beginning of an array.
*
@@ -510,7 +588,7 @@ class Arr
* Get a value from the array, and remove it.
*
* @param array $array
* @param string $key
* @param string|int $key
* @param mixed $default
* @return mixed
*/
@@ -539,7 +617,7 @@ class Arr
*
* @param array $array
* @param int|null $number
* @param bool|false $preserveKeys
* @param bool $preserveKeys
* @return mixed
*
* @throws \InvalidArgumentException
@@ -587,7 +665,7 @@ class Arr
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string|null $key
* @param string|int|null $key
* @param mixed $value
* @return array
*/
@@ -653,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.
*
@@ -705,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.
*
@@ -725,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));
}
/**
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,63 +0,0 @@
<?php
namespace Tightenco\Collect\Support;
/**
* @mixin \Tightenco\Collect\Support\Enumerable
*/
class HigherOrderWhenProxy
{
/**
* The collection being operated on.
*
* @var \Tightenco\Collect\Support\Enumerable
*/
protected $collection;
/**
* The condition for proxying.
*
* @var bool
*/
protected $condition;
/**
* Create a new proxy instance.
*
* @param \Tightenco\Collect\Support\Enumerable $collection
* @param bool $condition
* @return void
*/
public function __construct(Enumerable $collection, $condition)
{
$this->condition = $condition;
$this->collection = $collection;
}
/**
* Proxy accessing an attribute onto the collection.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->condition
? $this->collection->{$key}
: $this->collection;
}
/**
* Proxy a method call onto the collection.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->condition
? $this->collection->{$method}(...$parameters)
: $this->collection;
}
}
@@ -5,27 +5,39 @@ namespace Tightenco\Collect\Support;
use ArrayIterator;
use Closure;
use DateTimeInterface;
use Generator;
use Tightenco\Collect\Contracts\Support\CanBeEscapedWhenCastToString;
use Tightenco\Collect\Support\Traits\EnumeratesValues;
use Tightenco\Collect\Support\Traits\Macroable;
use InvalidArgumentException;
use IteratorAggregate;
use stdClass;
use Traversable;
/**
* @template TKey of array-key
* @template TValue
*
* @implements \Tightenco\Collect\Support\Enumerable<TKey, TValue>
*/
class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
{
/**
* @use \Tightenco\Collect\Support\Traits\EnumeratesValues<TKey, TValue>
*/
use EnumeratesValues, Macroable;
/**
* The source from which to generate items.
*
* @var callable|static
* @var (Closure(): \Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue>
*/
public $source;
/**
* Create a new lazy collection instance.
*
* @param mixed $source
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null $source
* @return void
*/
public function __construct($source = null)
@@ -34,17 +46,35 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
$this->source = $source;
} elseif (is_null($source)) {
$this->source = static::empty();
} elseif ($source instanceof Generator) {
throw new InvalidArgumentException(
'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.'
);
} else {
$this->source = $this->getArrayableItems($source);
}
}
/**
* Create a new collection instance if the value isn't one already.
*
* @template TMakeKey of array-key
* @template TMakeValue
*
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|(Closure(): \Generator<TMakeKey, TMakeValue, mixed, void>)|self<TMakeKey, TMakeValue>|array<TMakeKey, TMakeValue>|null $items
* @return static<TMakeKey, TMakeValue>
*/
public static function make($items = [])
{
return new static($items);
}
/**
* Create a collection with the given range.
*
* @param int $from
* @param int $to
* @return static
* @return static<int, int>
*/
public static function range($from, $to)
{
@@ -64,7 +94,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get all items in the enumerable.
*
* @return array
* @return array<TKey, TValue>
*/
public function all()
{
@@ -126,8 +156,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the average value of a given key.
*
* @param callable|string|null $callback
* @return mixed
* @param (callable(TValue): float|int)|string|null $callback
* @return float|int|null
*/
public function avg($callback = null)
{
@@ -137,8 +167,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the median of a given key.
*
* @param string|array|null $key
* @return mixed
* @param string|array<array-key, string>|null $key
* @return float|int|null
*/
public function median($key = null)
{
@@ -148,8 +178,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the mode of a given key.
*
* @param string|array|null $key
* @return array|null
* @param string|array<string>|null $key
* @return array<int, float|int>|null
*/
public function mode($key = null)
{
@@ -159,7 +189,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Collapse the collection of items into a single array.
*
* @return static
* @return static<int, mixed>
*/
public function collapse()
{
@@ -177,7 +207,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Determine if an item exists in the enumerable.
*
* @param mixed $key
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param mixed $operator
* @param mixed $value
* @return bool
@@ -187,6 +217,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
if (func_num_args() === 1 && $this->useAsCallable($key)) {
$placeholder = new stdClass;
/** @var callable $key */
return $this->first($key, $placeholder) !== $placeholder;
}
@@ -205,6 +236,32 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
return $this->contains($this->operatorForWhere(...func_get_args()));
}
/**
* Determine if an item exists, using strict comparison.
*
* @param (callable(TValue): bool)|TValue|array-key $key
* @param TValue|null $value
* @return bool
*/
public function containsStrict($key, $value = null)
{
if (func_num_args() === 2) {
return $this->contains(fn ($item) => data_get($item, $key) === $value);
}
if ($this->useAsCallable($key)) {
return ! is_null($this->first($key));
}
foreach ($this as $item) {
if ($item === $key) {
return true;
}
}
return false;
}
/**
* Determine if an item is not contained in the enumerable.
*
@@ -221,8 +278,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Cross join the given iterables, returning all possible permutations.
*
* @param array ...$arrays
* @return static
* @template TCrossJoinKey
* @template TCrossJoinValue
*
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$arrays
* @return static<int, array<int, TValue|TCrossJoinValue>>
*/
public function crossJoin(...$arrays)
{
@@ -232,8 +292,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Count the number of items in the collection by a field or using a callback.
*
* @param callable|string $countBy
* @return static
* @param (callable(TValue, TKey): array-key)|string|null $countBy
* @return static<array-key, int>
*/
public function countBy($countBy = null)
{
@@ -261,7 +321,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items that are not present in the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @return static
*/
public function diff($items)
@@ -272,8 +332,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items that are not present in the given items, using the callback.
*
* @param mixed $items
* @param callable $callback
* @param \Tightenco\Collect\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function diffUsing($items, callable $callback)
@@ -284,7 +344,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items whose keys and values are not present in the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function diffAssoc($items)
@@ -295,8 +355,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items whose keys and values are not present in the given items, using the callback.
*
* @param mixed $items
* @param callable $callback
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @param callable(TKey, TKey): int $callback
* @return static
*/
public function diffAssocUsing($items, callable $callback)
@@ -307,7 +367,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items whose keys are not present in the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function diffKeys($items)
@@ -318,8 +378,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items whose keys are not present in the given items, using the callback.
*
* @param mixed $items
* @param callable $callback
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @param callable(TKey, TKey): int $callback
* @return static
*/
public function diffKeysUsing($items, callable $callback)
@@ -330,7 +390,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Retrieve duplicate items.
*
* @param callable|string|null $callback
* @param (callable(TValue): bool)|string|null $callback
* @param bool $strict
* @return static
*/
@@ -342,7 +402,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Retrieve duplicate items using strict comparison.
*
* @param callable|string|null $callback
* @param (callable(TValue): bool)|string|null $callback
* @return static
*/
public function duplicatesStrict($callback = null)
@@ -353,7 +413,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get all items except for those with the specified keys.
*
* @param mixed $keys
* @param \Tightenco\Collect\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys
* @return static
*/
public function except($keys)
@@ -364,15 +424,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Run a filter over each of the items.
*
* @param callable|null $callback
* @param (callable(TValue, TKey): bool)|null $callback
* @return static
*/
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) {
@@ -387,9 +445,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the first item from the enumerable passing the given truth test.
*
* @param callable|null $callback
* @param mixed $default
* @return mixed
* @template TFirstDefault
*
* @param (callable(TValue): bool)|null $callback
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
* @return TValue|TFirstDefault
*/
public function first(callable $callback = null, $default = null)
{
@@ -416,7 +476,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Get a flattened list of the items in the collection.
*
* @param int $depth
* @return static
* @return static<int, mixed>
*/
public function flatten($depth = INF)
{
@@ -438,7 +498,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Flip the items in the collection.
*
* @return static
* @return static<TValue, TKey>
*/
public function flip()
{
@@ -452,9 +512,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get an item by key.
*
* @param mixed $key
* @param mixed $default
* @return mixed
* @template TGetDefault
*
* @param TKey|null $key
* @param TGetDefault|(\Closure(): TGetDefault) $default
* @return TValue|TGetDefault
*/
public function get($key, $default = null)
{
@@ -474,9 +536,9 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Group an associative array by a field or using a callback.
*
* @param array|callable|string $groupBy
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
* @param bool $preserveKeys
* @return static
* @return static<array-key, static<array-key, TValue>>
*/
public function groupBy($groupBy, $preserveKeys = false)
{
@@ -486,8 +548,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Key an associative array by a field or using a callback.
*
* @param callable|string $keyBy
* @return static
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
* @return static<array-key, TValue>
*/
public function keyBy($keyBy)
{
@@ -548,7 +610,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Concatenate values of a given key as a string.
*
* @param string $value
* @param callable|string $value
* @param string|null $glue
* @return string
*/
@@ -560,7 +622,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Intersect the collection with the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function intersect($items)
@@ -568,10 +630,45 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
return $this->passthru('intersect', func_get_args());
}
/**
* Intersect the collection with the given items, using the callback.
*
* @param \Tightenco\Collect\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 \Tightenco\Collect\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 \Tightenco\Collect\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.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function intersectByKeys($items)
@@ -614,7 +711,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the keys of the collection items.
*
* @return static
* @return static<int, TKey>
*/
public function keys()
{
@@ -628,9 +725,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the last item from the collection.
*
* @param callable|null $callback
* @param mixed $default
* @return mixed
* @template TLastDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TLastDefault|(\Closure(): TLastDefault) $default
* @return TValue|TLastDefault
*/
public function last(callable $callback = null, $default = null)
{
@@ -648,9 +747,9 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the values of a given key.
*
* @param string|array $value
* @param string|array<array-key, string> $value
* @param string|null $key
* @return static
* @return static<int, mixed>
*/
public function pluck($value, $key = null)
{
@@ -678,8 +777,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Run a map over each of the items.
*
* @param callable $callback
* @return static
* @template TMapValue
*
* @param callable(TValue, TKey): TMapValue $callback
* @return static<TKey, TMapValue>
*/
public function map(callable $callback)
{
@@ -695,8 +796,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* The callback should return an associative array with a single key/value pair.
*
* @param callable $callback
* @return static
* @template TMapToDictionaryKey of array-key
* @template TMapToDictionaryValue
*
* @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback
* @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>>
*/
public function mapToDictionary(callable $callback)
{
@@ -708,8 +812,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* The callback should return an associative array with a single key/value pair.
*
* @param callable $callback
* @return static
* @template TMapWithKeysKey of array-key
* @template TMapWithKeysValue
*
* @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback
* @return static<TMapWithKeysKey, TMapWithKeysValue>
*/
public function mapWithKeys(callable $callback)
{
@@ -723,7 +830,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Merge the collection with the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function merge($items)
@@ -734,8 +841,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Recursively merge the collection with the given items.
*
* @param mixed $items
* @return static
* @template TMergeRecursiveValue
*
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items
* @return static<TKey, TValue|TMergeRecursiveValue>
*/
public function mergeRecursive($items)
{
@@ -745,8 +854,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Create a collection by using this collection for keys and another for its values.
*
* @param mixed $values
* @return static
* @template TCombineValue
*
* @param \IteratorAggregate<array-key, TCombineValue>|array<array-key, TCombineValue>|(callable(): \Generator<array-key, TCombineValue>) $values
* @return static<TValue, TCombineValue>
*/
public function combine($values)
{
@@ -776,7 +887,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Union the collection with the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function union($items)
@@ -796,8 +907,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
return new static(function () use ($step, $offset) {
$position = 0;
foreach ($this as $item) {
if ($position % $step === $offset) {
foreach ($this->slice($offset) as $item) {
if ($position % $step === 0) {
yield $item;
}
@@ -809,7 +920,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the items with the specified keys.
*
* @param mixed $keys
* @param \Tightenco\Collect\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
* @return static
*/
public function only($keys)
@@ -844,7 +955,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Push all of the given items onto the collection.
*
* @param iterable $source
* @param iterable<array-key, TValue> $source
* @return static
*/
public function concat($source)
@@ -859,7 +970,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Get one or a specified number of items randomly from the collection.
*
* @param int|null $number
* @return static|mixed
* @return static<int, TValue>|TValue
*
* @throws \InvalidArgumentException
*/
@@ -873,7 +984,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Replace the collection items with the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function replace($items)
@@ -900,7 +1011,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Recursively replace the collection items with the given items.
*
* @param mixed $items
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function replaceRecursive($items)
@@ -921,12 +1032,13 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Search the collection for a given value and return the corresponding key if successful.
*
* @param mixed $value
* @param TValue|(callable(TValue,TKey): bool) $value
* @param bool $strict
* @return mixed
* @return TKey|bool
*/
public function search($value, $strict = false)
{
/** @var (callable(TValue,TKey): bool) $predicate */
$predicate = $this->useAsCallable($value)
? $value
: function ($item) use ($value, $strict) {
@@ -958,7 +1070,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* @param int $size
* @param int $step
* @return static
* @return static<int, static>
*/
public function sliding($size = 2, $step = 1)
{
@@ -971,7 +1083,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
$chunk[$iterator->key()] = $iterator->current();
if (count($chunk) == $size) {
yield tap(new static($chunk), function () use (&$chunk, $step) {
yield (new static($chunk))->tap(function () use (&$chunk, $step) {
$chunk = array_slice($chunk, $step, null, true);
});
@@ -1018,7 +1130,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Skip items in the collection until the given condition is met.
*
* @param mixed $value
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function skipUntil($value)
@@ -1031,7 +1143,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Skip items in the collection while the given condition is met.
*
* @param mixed $value
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function skipWhile($value)
@@ -1075,7 +1187,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Split a collection into a certain number of groups.
*
* @param int $numberOfGroups
* @return static
* @return static<int, static>
*/
public function split($numberOfGroups)
{
@@ -1085,10 +1197,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
*
* @param mixed $key
* @param (callable(TValue, TKey): bool)|string $key
* @param mixed $operator
* @param mixed $value
* @return mixed
* @return TValue
*
* @throws \Tightenco\Collect\Support\ItemNotFoundException
* @throws \Tightenco\Collect\Support\MultipleItemsFoundException
@@ -1100,7 +1212,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
: $key;
return $this
->when($filter)
->unless($filter == null)
->filter($filter)
->take(2)
->collect()
@@ -1110,10 +1222,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the first item in the collection but throw an exception if no matching items exist.
*
* @param mixed $key
* @param (callable(TValue, TKey): bool)|string $key
* @param mixed $operator
* @param mixed $value
* @return mixed
* @return TValue
*
* @throws \Tightenco\Collect\Support\ItemNotFoundException
*/
@@ -1124,7 +1236,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
: $key;
return $this
->when($filter)
->unless($filter == null)
->filter($filter)
->take(1)
->collect()
@@ -1135,7 +1247,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Chunk the collection into chunks of the given size.
*
* @param int $size
* @return static
* @return static<int, static>
*/
public function chunk($size)
{
@@ -1174,7 +1286,7 @@ 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
* @return static<int, static>
*/
public function splitIn($numberOfGroups)
{
@@ -1184,8 +1296,8 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Chunk the collection into chunks with a callback.
*
* @param callable $callback
* @return static
* @param callable(TValue, TKey, Collection<TKey, TValue>): bool $callback
* @return static<int, static<int, TValue>>
*/
public function chunkWhile(callable $callback)
{
@@ -1221,7 +1333,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Sort through each item with a callback.
*
* @param callable|null|int $callback
* @param (callable(TValue, TValue): int)|null|int $callback
* @return static
*/
public function sort($callback = null)
@@ -1243,7 +1355,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Sort the collection using the given callback.
*
* @param callable|string $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
@@ -1256,7 +1368,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Sort the collection in descending order using the given callback.
*
* @param callable|string $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
*/
@@ -1291,7 +1403,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Sort the collection keys using a callback.
*
* @param callable $callback
* @param callable(TKey, TKey): int $callback
* @return static
*/
public function sortKeysUsing(callable $callback)
@@ -1331,11 +1443,12 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Take items in the collection until the given condition is met.
*
* @param mixed $value
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function takeUntil($value)
{
/** @var callable(TValue, TKey): bool $callback */
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);
return new static(function () use ($callback) {
@@ -1359,30 +1472,39 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
{
$timeout = $timeout->getTimestamp();
return $this->takeWhile(function () use ($timeout) {
return $this->now() < $timeout;
return new static(function () use ($timeout) {
if ($this->now() >= $timeout) {
return;
}
foreach ($this as $key => $value) {
yield $key => $value;
if ($this->now() >= $timeout) {
break;
}
}
});
}
/**
* Take items in the collection while the given condition is met.
*
* @param mixed $value
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function takeWhile($value)
{
/** @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));
}
/**
* Pass each item in the collection to the given callback, lazily.
*
* @param callable $callback
* @param callable(TValue, TKey): mixed $callback
* @return static
*/
public function tapEach(callable $callback)
@@ -1409,7 +1531,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Return only unique items from the collection array.
*
* @param string|callable|null $key
* @param (callable(TValue, TKey): mixed)|string|null $key
* @param bool $strict
* @return static
*/
@@ -1433,7 +1555,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Reset the keys on the underlying array.
*
* @return static
* @return static<int, TValue>
*/
public function values()
{
@@ -1450,8 +1572,10 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]);
* => [[1, 4], [2, 5], [3, 6]]
*
* @param mixed ...$items
* @return static
* @template TZipValue
*
* @param \Tightenco\Collect\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
* @return static<int, static<int, TValue|TZipValue>>
*/
public function zip($items)
{
@@ -1473,9 +1597,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Pad collection to the specified length with a value.
*
* @template TPadValue
*
* @param int $size
* @param mixed $value
* @return static
* @param TPadValue $value
* @return static<int, TValue|TPadValue>
*/
public function pad($size, $value)
{
@@ -1501,10 +1627,9 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Get the values iterator.
*
* @return \Traversable
* @return \Traversable<TKey, TValue>
*/
#[\ReturnTypeWillChange]
public function getIterator()
public function getIterator(): Traversable
{
return $this->makeIterator($this->source);
}
@@ -1514,8 +1639,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
*
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
public function count(): int
{
if (is_array($this->source)) {
return count($this->source);
@@ -1527,8 +1651,11 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
/**
* Make an iterator from the given source.
*
* @param mixed $source
* @return \Traversable
* @template TIteratorKey of array-key
* @template TIteratorValue
*
* @param \IteratorAggregate<TIteratorKey, TIteratorValue>|array<TIteratorKey, TIteratorValue>|(callable(): \Generator<TIteratorKey, TIteratorValue>) $source
* @return \Traversable<TIteratorKey, TIteratorValue>
*/
protected function makeIterator($source)
{
@@ -1540,15 +1667,23 @@ 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);
}
/**
* Explode the "value" and "key" arguments passed to "pluck".
*
* @param string|array $value
* @param string|array|null $key
* @return array
* @param string|string[] $value
* @param string|string[]|null $key
* @return array{string[],string[]|null}
*/
protected function explodePluckParameters($value, $key)
{
@@ -1563,7 +1698,7 @@ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
* Pass this lazy collection through a method on the collection class.
*
* @param string $method
* @param array $params
* @param array<mixed> $params
* @return static
*/
protected function passthru($method, array $params)
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,73 @@
<?php
namespace Tightenco\Collect\Support\Traits;
use Closure;
use Tightenco\Collect\Conditionable\HigherOrderWhenProxy;
trait Conditionable
{
/**
* Apply the callback if the given "value" is (or resolves to) truthy.
*
* @template TWhenParameter
* @template TWhenReturnType
*
* @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default
* @return $this|TWhenReturnType
*/
public function when($value = null, callable $callback = null, callable $default = null)
{
$value = $value instanceof Closure ? $value($this) : $value;
if (func_num_args() === 0) {
return new HigherOrderWhenProxy($this);
}
if (func_num_args() === 1) {
return (new HigherOrderWhenProxy($this))->condition($value);
}
if ($value) {
return $callback($this, $value) ?? $this;
} elseif ($default) {
return $default($this, $value) ?? $this;
}
return $this;
}
/**
* Apply the callback if the given "value" is (or resolves to) falsy.
*
* @template TUnlessParameter
* @template TUnlessReturnType
*
* @param (\Closure($this): TUnlessParameter)|TUnlessParameter|null $value
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default
* @return $this|TUnlessReturnType
*/
public function unless($value = null, callable $callback = null, callable $default = null)
{
$value = $value instanceof Closure ? $value($this) : $value;
if (func_num_args() === 0) {
return (new HigherOrderWhenProxy($this))->negateConditionOnCapture();
}
if (func_num_args() === 1) {
return (new HigherOrderWhenProxy($this))->condition(! $value);
}
if (! $value) {
return $callback($this, $value) ?? $this;
} elseif ($default) {
return $default($this, $value) ?? $this;
}
return $this;
}
}
@@ -11,13 +11,16 @@ use Tightenco\Collect\Support\Arr;
use Tightenco\Collect\Support\Collection;
use Tightenco\Collect\Support\Enumerable;
use Tightenco\Collect\Support\HigherOrderCollectionProxy;
use Tightenco\Collect\Support\HigherOrderWhenProxy;
use JsonSerializable;
use Symfony\Component\VarDumper\VarDumper;
use Traversable;
use UnexpectedValueException;
use UnitEnum;
/**
* @template TKey of array-key
* @template TValue
*
* @property-read HigherOrderCollectionProxy $average
* @property-read HigherOrderCollectionProxy $avg
* @property-read HigherOrderCollectionProxy $contains
@@ -34,19 +37,23 @@ use UnexpectedValueException;
* @property-read HigherOrderCollectionProxy $min
* @property-read HigherOrderCollectionProxy $partition
* @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 $skipUntil
* @property-read HigherOrderCollectionProxy $skipWhile
* @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
*/
trait EnumeratesValues
{
use Conditionable;
/**
* Indicates that the object's string representation should be escaped when __toString is invoked.
*
@@ -57,7 +64,7 @@ trait EnumeratesValues
/**
* The methods that can be proxied.
*
* @var string[]
* @var array<int, string>
*/
protected static $proxies = [
'average',
@@ -85,14 +92,19 @@ trait EnumeratesValues
'takeUntil',
'takeWhile',
'unique',
'unless',
'until',
'when',
];
/**
* Create a new collection instance if the value isn't one already.
*
* @param mixed $items
* @return static
* @template TMakeKey of array-key
* @template TMakeValue
*
* @param \Tightenco\Collect\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|null $items
* @return static<TMakeKey, TMakeValue>
*/
public static function make($items = [])
{
@@ -102,8 +114,10 @@ trait EnumeratesValues
/**
* Wrap the given value in a collection if applicable.
*
* @param mixed $value
* @return static
* @template TWrapValue
*
* @param iterable<array-key, TWrapValue>|TWrapValue $value
* @return static<array-key, TWrapValue>
*/
public static function wrap($value)
{
@@ -115,8 +129,11 @@ trait EnumeratesValues
/**
* Get the underlying items from the given collection if applicable.
*
* @param array|static $value
* @return array
* @template TUnwrapKey of array-key
* @template TUnwrapValue
*
* @param array<TUnwrapKey, TUnwrapValue>|static<TUnwrapKey, TUnwrapValue> $value
* @return array<TUnwrapKey, TUnwrapValue>
*/
public static function unwrap($value)
{
@@ -136,9 +153,11 @@ trait EnumeratesValues
/**
* Create a new collection by invoking the callback a given amount of times.
*
* @template TTimesValue
*
* @param int $number
* @param callable|null $callback
* @return static
* @param (callable(int): TTimesValue)|null $callback
* @return static<int, TTimesValue>
*/
public static function times($number, callable $callback = null)
{
@@ -147,15 +166,15 @@ trait EnumeratesValues
}
return static::range(1, $number)
->when($callback)
->unless($callback == null)
->map($callback);
}
/**
* Alias for the "avg" method.
*
* @param callable|string|null $callback
* @return mixed
* @param (callable(TValue): float|int)|string|null $callback
* @return float|int|null
*/
public function average($callback = null)
{
@@ -165,7 +184,7 @@ trait EnumeratesValues
/**
* Alias for the "contains" method.
*
* @param mixed $key
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param mixed $operator
* @param mixed $value
* @return bool
@@ -175,39 +194,11 @@ trait EnumeratesValues
return $this->contains(...func_get_args());
}
/**
* Determine if an item exists, using strict comparison.
*
* @param mixed $key
* @param mixed $value
* @return bool
*/
public function containsStrict($key, $value = null)
{
if (func_num_args() === 2) {
return $this->contains(function ($item) use ($key, $value) {
return data_get($item, $key) === $value;
});
}
if ($this->useAsCallable($key)) {
return ! is_null($this->first($key));
}
foreach ($this as $item) {
if ($item === $key) {
return true;
}
}
return false;
}
/**
* Dump the items and end the script.
*
* @param mixed ...$args
* @return void
* @return never
*/
public function dd(...$args)
{
@@ -235,7 +226,7 @@ trait EnumeratesValues
/**
* Execute a callback over each item.
*
* @param callable $callback
* @param callable(TValue, TKey): mixed $callback
* @return $this
*/
public function each(callable $callback)
@@ -252,7 +243,7 @@ trait EnumeratesValues
/**
* Execute a callback over each nested chunk of items.
*
* @param callable $callback
* @param callable(...mixed): mixed $callback
* @return static
*/
public function eachSpread(callable $callback)
@@ -267,7 +258,7 @@ trait EnumeratesValues
/**
* Determine if all items pass the given truth test.
*
* @param string|callable $key
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param mixed $operator
* @param mixed $value
* @return bool
@@ -292,16 +283,32 @@ trait EnumeratesValues
/**
* Get the first item by the given key value pair.
*
* @param string $key
* @param callable|string $key
* @param mixed $operator
* @param mixed $value
* @return mixed
* @return TValue|null
*/
public function firstWhere($key, $operator = null, $value = null)
{
return $this->first($this->operatorForWhere(...func_get_args()));
}
/**
* Get a single key's value from the first matching item in the collection.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function value($key, $default = null)
{
if ($value = $this->firstWhere($key)) {
return data_get($value, $key, $default);
}
return value($default);
}
/**
* Determine if the collection is not empty.
*
@@ -315,8 +322,10 @@ trait EnumeratesValues
/**
* Run a map over each nested chunk of items.
*
* @param callable $callback
* @return static
* @template TMapSpreadValue
*
* @param callable(mixed): TMapSpreadValue $callback
* @return static<TKey, TMapSpreadValue>
*/
public function mapSpread(callable $callback)
{
@@ -332,8 +341,11 @@ trait EnumeratesValues
*
* The callback should return an associative array with a single key/value pair.
*
* @param callable $callback
* @return static
* @template TMapToGroupsKey of array-key
* @template TMapToGroupsValue
*
* @param callable(TValue, TKey): array<TMapToGroupsKey, TMapToGroupsValue> $callback
* @return static<TMapToGroupsKey, static<int, TMapToGroupsValue>>
*/
public function mapToGroups(callable $callback)
{
@@ -345,8 +357,11 @@ trait EnumeratesValues
/**
* Map a collection and flatten the result by a single level.
*
* @param callable $callback
* @return static
* @template TFlatMapKey of array-key
* @template TFlatMapValue
*
* @param callable(TValue, TKey): (\Tightenco\Collect\Support\Collection<TFlatMapKey, TFlatMapValue>|array<TFlatMapKey, TFlatMapValue>) $callback
* @return static<TFlatMapKey, TFlatMapValue>
*/
public function flatMap(callable $callback)
{
@@ -356,48 +371,42 @@ trait EnumeratesValues
/**
* Map the values into a new class.
*
* @param string $class
* @return static
* @template TMapIntoValue
*
* @param class-string<TMapIntoValue> $class
* @return static<TKey, TMapIntoValue>
*/
public function mapInto($class)
{
return $this->map(function ($value, $key) use ($class) {
return new $class($value, $key);
});
return $this->map(fn ($value, $key) => new $class($value, $key));
}
/**
* Get the min value of a given key.
*
* @param callable|string|null $callback
* @param (callable(TValue):mixed)|string|null $callback
* @return mixed
*/
public function min($callback = null)
{
$callback = $this->valueRetriever($callback);
return $this->map(function ($value) use ($callback) {
return $callback($value);
})->filter(function ($value) {
return ! is_null($value);
})->reduce(function ($result, $value) {
return is_null($result) || $value < $result ? $value : $result;
});
return $this->map(fn ($value) => $callback($value))
->filter(fn ($value) => ! is_null($value))
->reduce(fn ($result, $value) => is_null($result) || $value < $result ? $value : $result);
}
/**
* Get the max value of a given key.
*
* @param callable|string|null $callback
* @param (callable(TValue):mixed)|string|null $callback
* @return mixed
*/
public function max($callback = null)
{
$callback = $this->valueRetriever($callback);
return $this->filter(function ($value) {
return ! is_null($value);
})->reduce(function ($result, $item) use ($callback) {
return $this->filter(fn ($value) => ! is_null($value))->reduce(function ($result, $item) use ($callback) {
$value = $callback($item);
return is_null($result) || $value > $result ? $value : $result;
@@ -421,10 +430,10 @@ trait EnumeratesValues
/**
* Partition the collection into two arrays using the given callback or key.
*
* @param callable|string $key
* @param mixed $operator
* @param mixed $value
* @return static
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param TValue|string|null $operator
* @param TValue|null $value
* @return static<int<0, 1>, static<TKey, TValue>>
*/
public function partition($key, $operator = null, $value = null)
{
@@ -449,7 +458,7 @@ trait EnumeratesValues
/**
* Get the sum of the given values.
*
* @param callable|string|null $callback
* @param (callable(TValue): mixed)|string|null $callback
* @return mixed
*/
public function sum($callback = null)
@@ -458,40 +467,17 @@ trait EnumeratesValues
? $this->identity()
: $this->valueRetriever($callback);
return $this->reduce(function ($result, $item) use ($callback) {
return $result + $callback($item);
}, 0);
}
/**
* Apply the callback if the value is truthy.
*
* @param bool|mixed $value
* @param callable|null $callback
* @param callable|null $default
* @return static|mixed
*/
public function when($value, callable $callback = null, callable $default = null)
{
if (! $callback) {
return new HigherOrderWhenProxy($this, $value);
}
if ($value) {
return $callback($this, $value);
} elseif ($default) {
return $default($this, $value);
}
return $this;
return $this->reduce(fn ($result, $item) => $result + $callback($item), 0);
}
/**
* Apply the callback if the collection is empty.
*
* @param callable $callback
* @param callable|null $default
* @return static|mixed
* @template TWhenEmptyReturnType
*
* @param (callable($this): TWhenEmptyReturnType) $callback
* @param (callable($this): TWhenEmptyReturnType)|null $default
* @return $this|TWhenEmptyReturnType
*/
public function whenEmpty(callable $callback, callable $default = null)
{
@@ -501,34 +487,25 @@ trait EnumeratesValues
/**
* Apply the callback if the collection is not empty.
*
* @param callable $callback
* @param callable|null $default
* @return static|mixed
* @template TWhenNotEmptyReturnType
*
* @param callable($this): TWhenNotEmptyReturnType $callback
* @param (callable($this): TWhenNotEmptyReturnType)|null $default
* @return $this|TWhenNotEmptyReturnType
*/
public function whenNotEmpty(callable $callback, callable $default = null)
{
return $this->when($this->isNotEmpty(), $callback, $default);
}
/**
* Apply the callback if the value is falsy.
*
* @param bool $value
* @param callable $callback
* @param callable|null $default
* @return static|mixed
*/
public function unless($value, callable $callback, callable $default = null)
{
return $this->when(! $value, $callback, $default);
}
/**
* Apply the callback unless the collection is empty.
*
* @param callable $callback
* @param callable|null $default
* @return static|mixed
* @template TUnlessEmptyReturnType
*
* @param callable($this): TUnlessEmptyReturnType $callback
* @param (callable($this): TUnlessEmptyReturnType)|null $default
* @return $this|TUnlessEmptyReturnType
*/
public function unlessEmpty(callable $callback, callable $default = null)
{
@@ -538,9 +515,11 @@ trait EnumeratesValues
/**
* Apply the callback unless the collection is not empty.
*
* @param callable $callback
* @param callable|null $default
* @return static|mixed
* @template TUnlessNotEmptyReturnType
*
* @param callable($this): TUnlessNotEmptyReturnType $callback
* @param (callable($this): TUnlessNotEmptyReturnType)|null $default
* @return $this|TUnlessNotEmptyReturnType
*/
public function unlessNotEmpty(callable $callback, callable $default = null)
{
@@ -550,7 +529,7 @@ trait EnumeratesValues
/**
* Filter items by the given key value pair.
*
* @param string $key
* @param callable|string $key
* @param mixed $operator
* @param mixed $value
* @return static
@@ -598,7 +577,7 @@ trait EnumeratesValues
* Filter items by the given key value pair.
*
* @param string $key
* @param mixed $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @param bool $strict
* @return static
*/
@@ -606,16 +585,14 @@ trait EnumeratesValues
{
$values = $this->getArrayableItems($values);
return $this->filter(function ($item) use ($key, $values, $strict) {
return in_array(data_get($item, $key), $values, $strict);
});
return $this->filter(fn ($item) => in_array(data_get($item, $key), $values, $strict));
}
/**
* Filter items by the given key value pair using strict comparison.
*
* @param string $key
* @param mixed $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @return static
*/
public function whereInStrict($key, $values)
@@ -627,7 +604,7 @@ trait EnumeratesValues
* Filter items such that the value of the given key is between the given values.
*
* @param string $key
* @param array $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @return static
*/
public function whereBetween($key, $values)
@@ -639,21 +616,21 @@ trait EnumeratesValues
* Filter items such that the value of the given key is not between the given values.
*
* @param string $key
* @param array $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @return static
*/
public function whereNotBetween($key, $values)
{
return $this->filter(function ($item) use ($key, $values) {
return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values);
});
return $this->filter(
fn ($item) => data_get($item, $key) < reset($values) || data_get($item, $key) > end($values)
);
}
/**
* Filter items by the given key value pair.
*
* @param string $key
* @param mixed $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @param bool $strict
* @return static
*/
@@ -661,16 +638,14 @@ trait EnumeratesValues
{
$values = $this->getArrayableItems($values);
return $this->reject(function ($item) use ($key, $values, $strict) {
return in_array(data_get($item, $key), $values, $strict);
});
return $this->reject(fn ($item) => in_array(data_get($item, $key), $values, $strict));
}
/**
* Filter items by the given key value pair using strict comparison.
*
* @param string $key
* @param mixed $values
* @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $values
* @return static
*/
public function whereNotInStrict($key, $values)
@@ -681,8 +656,10 @@ trait EnumeratesValues
/**
* Filter the items, removing any items that don't match the given type(s).
*
* @param string|string[] $type
* @return static
* @template TWhereInstanceOf
*
* @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type
* @return static<TKey, TWhereInstanceOf>
*/
public function whereInstanceOf($type)
{
@@ -704,8 +681,10 @@ trait EnumeratesValues
/**
* Pass the collection to the given callback and return the result.
*
* @param callable $callback
* @return mixed
* @template TPipeReturnType
*
* @param callable($this): TPipeReturnType $callback
* @return TPipeReturnType
*/
public function pipe(callable $callback)
{
@@ -715,7 +694,7 @@ trait EnumeratesValues
/**
* Pass the collection into a new class.
*
* @param string $class
* @param class-string $class
* @return mixed
*/
public function pipeInto($class)
@@ -726,38 +705,26 @@ trait EnumeratesValues
/**
* Pass the collection through a series of callable pipes and return the result.
*
* @param array<callable> $pipes
* @param array<callable> $callbacks
* @return mixed
*/
public function pipeThrough($pipes)
public function pipeThrough($callbacks)
{
return static::make($pipes)->reduce(
function ($carry, $pipe) {
return $pipe($carry);
},
return Collection::make($callbacks)->reduce(
fn ($carry, $callback) => $callback($carry),
$this,
);
}
/**
* Pass the collection to the given callback and then return it.
*
* @param callable $callback
* @return $this
*/
public function tap(callable $callback)
{
$callback(clone $this);
return $this;
}
/**
* Reduce the collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
* @template TReduceInitial
* @template TReduceReturnType
*
* @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
* @param TReduceInitial $initial
* @return TReduceReturnType
*/
public function reduce(callable $callback, $initial = null)
{
@@ -770,22 +737,6 @@ trait EnumeratesValues
return $result;
}
/**
* Reduce the collection to multiple aggregate values.
*
* @param callable $callback
* @param mixed ...$initial
* @return array
*
* @deprecated Use "reduceSpread" instead
*
* @throws \UnexpectedValueException
*/
public function reduceMany(callable $callback, ...$initial)
{
return $this->reduceSpread($callback, ...$initial);
}
/**
* Reduce the collection to multiple aggregate values.
*
@@ -804,7 +755,7 @@ trait EnumeratesValues
if (! is_array($result)) {
throw new UnexpectedValueException(sprintf(
"%s::reduceMany expects reducer to return an array, but got a '%s' instead.",
"%s::reduceSpread expects reducer to return an array, but got a '%s' instead.",
class_basename(static::class), gettype($result)
));
}
@@ -813,22 +764,10 @@ trait EnumeratesValues
return $result;
}
/**
* Reduce an associative collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduceWithKeys(callable $callback, $initial = null)
{
return $this->reduce($callback, $initial);
}
/**
* Create a collection of all elements that do not pass a given truth test.
*
* @param callable|mixed $callback
* @param (callable(TValue, TKey): bool)|bool|TValue $callback
* @return static
*/
public function reject($callback = true)
@@ -842,10 +781,45 @@ trait EnumeratesValues
});
}
/**
* Pass the collection to the given callback and then return it.
*
* @param callable($this): mixed $callback
* @return $this
*/
public function tap(callable $callback)
{
$callback($this);
return $this;
}
/**
* Return only unique items from the collection array.
*
* @param (callable(TValue, TKey): mixed)|string|null $key
* @param bool $strict
* @return static
*/
public function unique($key = null, $strict = false)
{
$callback = $this->valueRetriever($key);
$exists = [];
return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) {
if (in_array($id = $callback($item, $key), $exists, $strict)) {
return true;
}
$exists[] = $id;
});
}
/**
* Return only unique items from the collection array using strict comparison.
*
* @param string|callable|null $key
* @param (callable(TValue, TKey): mixed)|string|null $key
* @return static
*/
public function uniqueStrict($key = null)
@@ -856,7 +830,7 @@ trait EnumeratesValues
/**
* Collect the values into a collection.
*
* @return \Tightenco\Collect\Support\Collection
* @return \Tightenco\Collect\Support\Collection<TKey, TValue>
*/
public function collect()
{
@@ -866,22 +840,19 @@ trait EnumeratesValues
/**
* Get the collection of items as a plain array.
*
* @return array
* @return array<TKey, mixed>
*/
public function toArray()
{
return $this->map(function ($value) {
return $value instanceof Arrayable ? $value->toArray() : $value;
})->all();
return $this->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value)->all();
}
/**
* Convert the object into something JSON serializable.
*
* @return array
* @return array<TKey, mixed>
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
return array_map(function ($value) {
if ($value instanceof JsonSerializable) {
@@ -975,7 +946,7 @@ trait EnumeratesValues
* Results array of items from Collection or Arrayable.
*
* @param mixed $items
* @return array
* @return array<TKey, TValue>
*/
protected function getArrayableItems($items)
{
@@ -985,12 +956,14 @@ trait EnumeratesValues
return $items->all();
} elseif ($items instanceof Arrayable) {
return $items->toArray();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
} elseif ($items instanceof Jsonable) {
return json_decode($items->toJson(), true);
} elseif ($items instanceof JsonSerializable) {
return (array) $items->jsonSerialize();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
} elseif ($items instanceof UnitEnum) {
return [$items];
}
return (array) $items;
@@ -999,13 +972,17 @@ trait EnumeratesValues
/**
* Get an operator checker callback.
*
* @param string $key
* @param callable|string $key
* @param string|null $operator
* @param mixed $value
* @return \Closure
*/
protected function operatorForWhere($key, $operator = null, $value = null)
{
if ($this->useAsCallable($key)) {
return $key;
}
if (func_num_args() === 1) {
$value = true;
@@ -1041,6 +1018,7 @@ trait EnumeratesValues
case '>=': return $retrieved >= $value;
case '===': return $retrieved === $value;
case '!==': return $retrieved !== $value;
case '<=>': return $retrieved <=> $value;
}
};
}
@@ -1068,22 +1046,18 @@ trait EnumeratesValues
return $value;
}
return function ($item) use ($value) {
return data_get($item, $value);
};
return fn ($item) => data_get($item, $value);
}
/**
* Make a function to check an item's equality.
*
* @param mixed $value
* @return \Closure
* @return \Closure(mixed): bool
*/
protected function equality($value)
{
return function ($item) use ($value) {
return $item === $value;
};
return fn ($item) => $item === $value;
}
/**
@@ -1094,20 +1068,16 @@ trait EnumeratesValues
*/
protected function negate(Closure $callback)
{
return function (...$params) use ($callback) {
return ! $callback(...$params);
};
return fn (...$params) => ! $callback(...$params);
}
/**
* Make a function that returns what's passed to it.
*
* @return \Closure
* @return \Closure(TValue): TValue
*/
protected function identity()
{
return function ($value) {
return $value;
};
return fn ($value) => $value;
}
}
@@ -9,7 +9,6 @@ $aliases = [
Tightenco\Collect\Support\Collection::class => Illuminate\Support\Collection::class,
Tightenco\Collect\Support\Enumerable::class => Illuminate\Support\Enumerable::class,
Tightenco\Collect\Support\HigherOrderCollectionProxy::class => Illuminate\Support\HigherOrderCollectionProxy::class,
Tightenco\Collect\Support\HigherOrderWhenProxy::class => Illuminate\Support\HigherOrderWhenProxy::class,
Tightenco\Collect\Support\LazyCollection::class => Illuminate\Support\LazyCollection::class,
Tightenco\Collect\Support\Traits\EnumeratesValues::class => Illuminate\Support\Traits\EnumeratesValues::class,
];