| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116 |
- <?php
- namespace Illuminate\Support\Traits;
- use CachingIterator;
- use Closure;
- use Exception;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Enumerable;
- use Illuminate\Support\HigherOrderCollectionProxy;
- use Illuminate\Support\HigherOrderWhenProxy;
- use JsonSerializable;
- use Symfony\Component\VarDumper\VarDumper;
- use Traversable;
- use UnexpectedValueException;
- use UnitEnum;
- /**
- * @property-read HigherOrderCollectionProxy $average
- * @property-read HigherOrderCollectionProxy $avg
- * @property-read HigherOrderCollectionProxy $contains
- * @property-read HigherOrderCollectionProxy $doesntContain
- * @property-read HigherOrderCollectionProxy $each
- * @property-read HigherOrderCollectionProxy $every
- * @property-read HigherOrderCollectionProxy $filter
- * @property-read HigherOrderCollectionProxy $first
- * @property-read HigherOrderCollectionProxy $flatMap
- * @property-read HigherOrderCollectionProxy $groupBy
- * @property-read HigherOrderCollectionProxy $keyBy
- * @property-read HigherOrderCollectionProxy $map
- * @property-read HigherOrderCollectionProxy $max
- * @property-read HigherOrderCollectionProxy $min
- * @property-read HigherOrderCollectionProxy $partition
- * @property-read HigherOrderCollectionProxy $reject
- * @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 $until
- */
- trait EnumeratesValues
- {
- /**
- * Indicates that the object's string representation should be escaped when __toString is invoked.
- *
- * @var bool
- */
- protected $escapeWhenCastingToString = false;
- /**
- * The methods that can be proxied.
- *
- * @var string[]
- */
- protected static $proxies = [
- 'average',
- 'avg',
- 'contains',
- 'doesntContain',
- 'each',
- 'every',
- 'filter',
- 'first',
- 'flatMap',
- 'groupBy',
- 'keyBy',
- 'map',
- 'max',
- 'min',
- 'partition',
- 'reject',
- 'skipUntil',
- 'skipWhile',
- 'some',
- 'sortBy',
- 'sortByDesc',
- 'sum',
- 'takeUntil',
- 'takeWhile',
- 'unique',
- 'until',
- ];
- /**
- * Create a new collection instance if the value isn't one already.
- *
- * @param mixed $items
- * @return static
- */
- public static function make($items = [])
- {
- return new static($items);
- }
- /**
- * Wrap the given value in a collection if applicable.
- *
- * @param mixed $value
- * @return static
- */
- public static function wrap($value)
- {
- return $value instanceof Enumerable
- ? new static($value)
- : new static(Arr::wrap($value));
- }
- /**
- * Get the underlying items from the given collection if applicable.
- *
- * @param array|static $value
- * @return array
- */
- public static function unwrap($value)
- {
- return $value instanceof Enumerable ? $value->all() : $value;
- }
- /**
- * Create a new instance with no items.
- *
- * @return static
- */
- public static function empty()
- {
- return new static([]);
- }
- /**
- * Create a new collection by invoking the callback a given amount of times.
- *
- * @param int $number
- * @param callable|null $callback
- * @return static
- */
- public static function times($number, callable $callback = null)
- {
- if ($number < 1) {
- return new static;
- }
- return static::range(1, $number)
- ->when($callback)
- ->map($callback);
- }
- /**
- * Alias for the "avg" method.
- *
- * @param callable|string|null $callback
- * @return mixed
- */
- public function average($callback = null)
- {
- return $this->avg($callback);
- }
- /**
- * Alias for the "contains" method.
- *
- * @param mixed $key
- * @param mixed $operator
- * @param mixed $value
- * @return bool
- */
- public function some($key, $operator = null, $value = null)
- {
- 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
- */
- public function dd(...$args)
- {
- $this->dump(...$args);
- exit(1);
- }
- /**
- * Dump the items.
- *
- * @return $this
- */
- public function dump()
- {
- (new Collection(func_get_args()))
- ->push($this->all())
- ->each(function ($item) {
- VarDumper::dump($item);
- });
- return $this;
- }
- /**
- * Execute a callback over each item.
- *
- * @param callable $callback
- * @return $this
- */
- public function each(callable $callback)
- {
- foreach ($this as $key => $item) {
- if ($callback($item, $key) === false) {
- break;
- }
- }
- return $this;
- }
- /**
- * Execute a callback over each nested chunk of items.
- *
- * @param callable $callback
- * @return static
- */
- public function eachSpread(callable $callback)
- {
- return $this->each(function ($chunk, $key) use ($callback) {
- $chunk[] = $key;
- return $callback(...$chunk);
- });
- }
- /**
- * Determine if all items pass the given truth test.
- *
- * @param string|callable $key
- * @param mixed $operator
- * @param mixed $value
- * @return bool
- */
- public function every($key, $operator = null, $value = null)
- {
- if (func_num_args() === 1) {
- $callback = $this->valueRetriever($key);
- foreach ($this as $k => $v) {
- if (! $callback($v, $k)) {
- return false;
- }
- }
- return true;
- }
- return $this->every($this->operatorForWhere(...func_get_args()));
- }
- /**
- * Get the first item by the given key value pair.
- *
- * @param string $key
- * @param mixed $operator
- * @param mixed $value
- * @return mixed
- */
- public function firstWhere($key, $operator = null, $value = null)
- {
- return $this->first($this->operatorForWhere(...func_get_args()));
- }
- /**
- * Determine if the collection is not empty.
- *
- * @return bool
- */
- public function isNotEmpty()
- {
- return ! $this->isEmpty();
- }
- /**
- * Run a map over each nested chunk of items.
- *
- * @param callable $callback
- * @return static
- */
- public function mapSpread(callable $callback)
- {
- return $this->map(function ($chunk, $key) use ($callback) {
- $chunk[] = $key;
- return $callback(...$chunk);
- });
- }
- /**
- * Run a grouping map over the items.
- *
- * The callback should return an associative array with a single key/value pair.
- *
- * @param callable $callback
- * @return static
- */
- public function mapToGroups(callable $callback)
- {
- $groups = $this->mapToDictionary($callback);
- return $groups->map([$this, 'make']);
- }
- /**
- * Map a collection and flatten the result by a single level.
- *
- * @param callable $callback
- * @return static
- */
- public function flatMap(callable $callback)
- {
- return $this->map($callback)->collapse();
- }
- /**
- * Map the values into a new class.
- *
- * @param string $class
- * @return static
- */
- public function mapInto($class)
- {
- return $this->map(function ($value, $key) use ($class) {
- return new $class($value, $key);
- });
- }
- /**
- * Get the min value of a given key.
- *
- * @param callable|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;
- });
- }
- /**
- * Get the max value of a given key.
- *
- * @param callable|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) {
- $value = $callback($item);
- return is_null($result) || $value > $result ? $value : $result;
- });
- }
- /**
- * "Paginate" the collection by slicing it into a smaller collection.
- *
- * @param int $page
- * @param int $perPage
- * @return static
- */
- public function forPage($page, $perPage)
- {
- $offset = max(0, ($page - 1) * $perPage);
- return $this->slice($offset, $perPage);
- }
- /**
- * Partition the collection into two arrays using the given callback or key.
- *
- * @param callable|string $key
- * @param mixed $operator
- * @param mixed $value
- * @return static
- */
- public function partition($key, $operator = null, $value = null)
- {
- $passed = [];
- $failed = [];
- $callback = func_num_args() === 1
- ? $this->valueRetriever($key)
- : $this->operatorForWhere(...func_get_args());
- foreach ($this as $key => $item) {
- if ($callback($item, $key)) {
- $passed[$key] = $item;
- } else {
- $failed[$key] = $item;
- }
- }
- return new static([new static($passed), new static($failed)]);
- }
- /**
- * Get the sum of the given values.
- *
- * @param callable|string|null $callback
- * @return mixed
- */
- public function sum($callback = null)
- {
- $callback = is_null($callback)
- ? $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;
- }
- /**
- * Apply the callback if the collection is empty.
- *
- * @param callable $callback
- * @param callable|null $default
- * @return static|mixed
- */
- public function whenEmpty(callable $callback, callable $default = null)
- {
- return $this->when($this->isEmpty(), $callback, $default);
- }
- /**
- * Apply the callback if the collection is not empty.
- *
- * @param callable $callback
- * @param callable|null $default
- * @return static|mixed
- */
- 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
- */
- public function unlessEmpty(callable $callback, callable $default = null)
- {
- return $this->whenNotEmpty($callback, $default);
- }
- /**
- * Apply the callback unless the collection is not empty.
- *
- * @param callable $callback
- * @param callable|null $default
- * @return static|mixed
- */
- public function unlessNotEmpty(callable $callback, callable $default = null)
- {
- return $this->whenEmpty($callback, $default);
- }
- /**
- * Filter items by the given key value pair.
- *
- * @param string $key
- * @param mixed $operator
- * @param mixed $value
- * @return static
- */
- public function where($key, $operator = null, $value = null)
- {
- return $this->filter($this->operatorForWhere(...func_get_args()));
- }
- /**
- * Filter items where the value for the given key is null.
- *
- * @param string|null $key
- * @return static
- */
- public function whereNull($key = null)
- {
- return $this->whereStrict($key, null);
- }
- /**
- * Filter items where the value for the given key is not null.
- *
- * @param string|null $key
- * @return static
- */
- public function whereNotNull($key = null)
- {
- return $this->where($key, '!==', null);
- }
- /**
- * Filter items by the given key value pair using strict comparison.
- *
- * @param string $key
- * @param mixed $value
- * @return static
- */
- public function whereStrict($key, $value)
- {
- return $this->where($key, '===', $value);
- }
- /**
- * Filter items by the given key value pair.
- *
- * @param string $key
- * @param mixed $values
- * @param bool $strict
- * @return static
- */
- public function whereIn($key, $values, $strict = false)
- {
- $values = $this->getArrayableItems($values);
- return $this->filter(function ($item) use ($key, $values, $strict) {
- return 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
- * @return static
- */
- public function whereInStrict($key, $values)
- {
- return $this->whereIn($key, $values, true);
- }
- /**
- * Filter items such that the value of the given key is between the given values.
- *
- * @param string $key
- * @param array $values
- * @return static
- */
- public function whereBetween($key, $values)
- {
- return $this->where($key, '>=', reset($values))->where($key, '<=', end($values));
- }
- /**
- * Filter items such that the value of the given key is not between the given values.
- *
- * @param string $key
- * @param array $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);
- });
- }
- /**
- * Filter items by the given key value pair.
- *
- * @param string $key
- * @param mixed $values
- * @param bool $strict
- * @return static
- */
- public function whereNotIn($key, $values, $strict = false)
- {
- $values = $this->getArrayableItems($values);
- return $this->reject(function ($item) use ($key, $values, $strict) {
- return 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
- * @return static
- */
- public function whereNotInStrict($key, $values)
- {
- return $this->whereNotIn($key, $values, true);
- }
- /**
- * Filter the items, removing any items that don't match the given type(s).
- *
- * @param string|string[] $type
- * @return static
- */
- public function whereInstanceOf($type)
- {
- return $this->filter(function ($value) use ($type) {
- if (is_array($type)) {
- foreach ($type as $classType) {
- if ($value instanceof $classType) {
- return true;
- }
- }
- return false;
- }
- return $value instanceof $type;
- });
- }
- /**
- * Pass the collection to the given callback and return the result.
- *
- * @param callable $callback
- * @return mixed
- */
- public function pipe(callable $callback)
- {
- return $callback($this);
- }
- /**
- * Pass the collection into a new class.
- *
- * @param string $class
- * @return mixed
- */
- public function pipeInto($class)
- {
- return new $class($this);
- }
- /**
- * Pass the collection through a series of callable pipes and return the result.
- *
- * @param array<callable> $pipes
- * @return mixed
- */
- public function pipeThrough($pipes)
- {
- return static::make($pipes)->reduce(
- function ($carry, $pipe) {
- return $pipe($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
- */
- public function reduce(callable $callback, $initial = null)
- {
- $result = $initial;
- foreach ($this as $key => $value) {
- $result = $callback($result, $value, $key);
- }
- 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.
- *
- * @param callable $callback
- * @param mixed ...$initial
- * @return array
- *
- * @throws \UnexpectedValueException
- */
- public function reduceSpread(callable $callback, ...$initial)
- {
- $result = $initial;
- foreach ($this as $key => $value) {
- $result = call_user_func_array($callback, array_merge($result, [$value, $key]));
- if (! is_array($result)) {
- throw new UnexpectedValueException(sprintf(
- "%s::reduceMany expects reducer to return an array, but got a '%s' instead.",
- class_basename(static::class), gettype($result)
- ));
- }
- }
- 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
- * @return static
- */
- public function reject($callback = true)
- {
- $useAsCallable = $this->useAsCallable($callback);
- return $this->filter(function ($value, $key) use ($callback, $useAsCallable) {
- return $useAsCallable
- ? ! $callback($value, $key)
- : $value != $callback;
- });
- }
- /**
- * Return only unique items from the collection array using strict comparison.
- *
- * @param string|callable|null $key
- * @return static
- */
- public function uniqueStrict($key = null)
- {
- return $this->unique($key, true);
- }
- /**
- * Collect the values into a collection.
- *
- * @return \Illuminate\Support\Collection
- */
- public function collect()
- {
- return new Collection($this->all());
- }
- /**
- * Get the collection of items as a plain array.
- *
- * @return array
- */
- public function toArray()
- {
- return $this->map(function ($value) {
- return $value instanceof Arrayable ? $value->toArray() : $value;
- })->all();
- }
- /**
- * Convert the object into something JSON serializable.
- *
- * @return array
- */
- #[\ReturnTypeWillChange]
- public function jsonSerialize()
- {
- 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;
- }, $this->all());
- }
- /**
- * Get the collection of items as JSON.
- *
- * @param int $options
- * @return string
- */
- public function toJson($options = 0)
- {
- return json_encode($this->jsonSerialize(), $options);
- }
- /**
- * Get a CachingIterator instance.
- *
- * @param int $flags
- * @return \CachingIterator
- */
- public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
- {
- return new CachingIterator($this->getIterator(), $flags);
- }
- /**
- * Convert the collection to its string representation.
- *
- * @return string
- */
- public function __toString()
- {
- return $this->escapeWhenCastingToString
- ? e($this->toJson())
- : $this->toJson();
- }
- /**
- * Indicate that the model's string representation should be escaped when __toString is invoked.
- *
- * @param bool $escape
- * @return $this
- */
- public function escapeWhenCastingToString($escape = true)
- {
- $this->escapeWhenCastingToString = $escape;
- return $this;
- }
- /**
- * Add a method to the list of proxied methods.
- *
- * @param string $method
- * @return void
- */
- public static function proxy($method)
- {
- static::$proxies[] = $method;
- }
- /**
- * Dynamically access collection proxies.
- *
- * @param string $key
- * @return mixed
- *
- * @throws \Exception
- */
- public function __get($key)
- {
- if (! in_array($key, static::$proxies)) {
- throw new Exception("Property [{$key}] does not exist on this collection instance.");
- }
- return new HigherOrderCollectionProxy($this, $key);
- }
- /**
- * Results array of items from Collection or Arrayable.
- *
- * @param mixed $items
- * @return array
- */
- protected function getArrayableItems($items)
- {
- if (is_array($items)) {
- return $items;
- } elseif ($items instanceof Enumerable) {
- return $items->all();
- } elseif ($items instanceof Arrayable) {
- return $items->toArray();
- } 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;
- }
- /**
- * Get an operator checker callback.
- *
- * @param string $key
- * @param string|null $operator
- * @param mixed $value
- * @return \Closure
- */
- protected function operatorForWhere($key, $operator = null, $value = null)
- {
- if (func_num_args() === 1) {
- $value = true;
- $operator = '=';
- }
- if (func_num_args() === 2) {
- $value = $operator;
- $operator = '=';
- }
- return function ($item) use ($key, $operator, $value) {
- $retrieved = data_get($item, $key);
- $strings = array_filter([$retrieved, $value], function ($value) {
- return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
- });
- if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {
- return in_array($operator, ['!=', '<>', '!==']);
- }
- switch ($operator) {
- default:
- case '=':
- case '==': return $retrieved == $value;
- case '!=':
- case '<>': return $retrieved != $value;
- case '<': return $retrieved < $value;
- case '>': return $retrieved > $value;
- case '<=': return $retrieved <= $value;
- case '>=': return $retrieved >= $value;
- case '===': return $retrieved === $value;
- case '!==': return $retrieved !== $value;
- }
- };
- }
- /**
- * Determine if the given value is callable, but not a string.
- *
- * @param mixed $value
- * @return bool
- */
- protected function useAsCallable($value)
- {
- return ! is_string($value) && is_callable($value);
- }
- /**
- * Get a value retrieving callback.
- *
- * @param callable|string|null $value
- * @return callable
- */
- protected function valueRetriever($value)
- {
- if ($this->useAsCallable($value)) {
- return $value;
- }
- return function ($item) use ($value) {
- return data_get($item, $value);
- };
- }
- /**
- * Make a function to check an item's equality.
- *
- * @param mixed $value
- * @return \Closure
- */
- protected function equality($value)
- {
- return function ($item) use ($value) {
- return $item === $value;
- };
- }
- /**
- * Make a function using another function, by negating its result.
- *
- * @param \Closure $callback
- * @return \Closure
- */
- protected function negate(Closure $callback)
- {
- return function (...$params) use ($callback) {
- return ! $callback(...$params);
- };
- }
- /**
- * Make a function that returns what's passed to it.
- *
- * @return \Closure
- */
- protected function identity()
- {
- return function ($value) {
- return $value;
- };
- }
- }
|