Debian Codegraph @

lib/3rdParty/composer/illuminate/collections/LazyCollection.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use ArrayIterator;
6 use Closure;
7 use DateInterval;
8 use DateTimeImmutable;
9 use DateTimeInterface;
10 use Generator;
11 use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
12 use Illuminate\Support\Traits\EnumeratesValues;
13 use Illuminate\Support\Traits\Macroable;
14 use InvalidArgumentException;
15 use IteratorAggregate;
16 use stdClass;
17 use Traversable;
18
19 /**
20 * @template TKey of array-key
21 *
22 * @template-covariant TValue
23 *
24 * @implements \Illuminate\Support\Enumerable<TKey, TValue>
25 */
26 class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
27 {
28 /**
29 * @use \Illuminate\Support\Traits\EnumeratesValues<TKey, TValue>
30 */
31 use EnumeratesValues, Macroable;
32
33 /**
34 * The source from which to generate items.
35 *
36 * @var (Closure(): \Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue>
37 */
38 public $source;
39
40 /**
41 * Create a new lazy collection instance.
42 *
43 * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null $source
44 */
45 public function __construct($source = null)
46 {
47 if ($source instanceof Closure || $source instanceof self) {
48 $this->source = $source;
49 } elseif (is_null($source)) {
50 $this->source = static::empty();
51 } elseif ($source instanceof Generator) {
52 throw new InvalidArgumentException(
53 'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.'
54 );
55 } else {
56 $this->source = $this->getArrayableItems($source);
57 }
58 }
59
60 /**
61 * Create a new collection instance if the value isn't one already.
62 *
63 * @template TMakeKey of array-key
64 * @template TMakeValue
65 *
66 * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|(Closure(): \Generator<TMakeKey, TMakeValue, mixed, void>)|self<TMakeKey, TMakeValue>|array<TMakeKey, TMakeValue>|null $items
67 * @return static<TMakeKey, TMakeValue>
68 */
69 public static function make($items = [])
70 {
71 return new static($items);
72 }
73
74 /**
75 * Create a collection with the given range.
76 *
77 * @param int $from
78 * @param int $to
79 * @param int $step
80 * @return ($step is zero ? never : static<int, int>)
81 *
82 * @throws \InvalidArgumentException
83 */
84 public static function range($from, $to, $step = 1)
85 {
86 if ($step == 0) {
87 throw new InvalidArgumentException('Step value cannot be zero.');
88 }
89
90 return new static(function () use ($from, $to, $step) {
91 if ($from <= $to) {
92 for (; $from <= $to; $from += abs($step)) {
93 yield $from;
94 }
95 } else {
96 for (; $from >= $to; $from -= abs($step)) {
97 yield $from;
98 }
99 }
100 });
101 }
102
103 /**
104 * Get all items in the enumerable.
105 *
106 * @return array<TKey, TValue>
107 */
108 public function all()
109 {
110 if (is_array($this->source)) {
111 return $this->source;
112 }
113
114 return iterator_to_array($this->getIterator());
115 }
116
117 /**
118 * Eager load all items into a new lazy collection backed by an array.
119 *
120 * @return static<TKey, TValue>
121 */
122 public function eager()
123 {
124 return new static($this->all());
125 }
126
127 /**
128 * Cache values as they're enumerated.
129 *
130 * @return static<TKey, TValue>
131 */
132 public function remember()
133 {
134 $iterator = $this->getIterator();
135
136 $iteratorIndex = 0;
137
138 $cache = [];
139
140 return new static(function () use ($iterator, &$iteratorIndex, &$cache) {
141 for ($index = 0; true; $index++) {
142 if (array_key_exists($index, $cache)) {
143 yield $cache[$index][0] => $cache[$index][1];
144
145 continue;
146 }
147
148 if ($iteratorIndex < $index) {
149 $iterator->next();
150
151 $iteratorIndex++;
152 }
153
154 if (! $iterator->valid()) {
155 break;
156 }
157
158 $cache[$index] = [$iterator->key(), $iterator->current()];
159
160 yield $cache[$index][0] => $cache[$index][1];
161 }
162 });
163 }
164
165 /**
166 * Get the median of a given key.
167 *
168 * @param string|array<array-key, string>|null $key
169 * @return float|int|null
170 */
171 public function median($key = null)
172 {
173 return $this->collect()->median($key);
174 }
175
176 /**
177 * Get the mode of a given key.
178 *
179 * @param string|array<string>|null $key
180 * @return array<int, float|int>|null
181 */
182 public function mode($key = null)
183 {
184 return $this->collect()->mode($key);
185 }
186
187 /**
188 * Collapse the collection of items into a single array.
189 *
190 * @return static<int, mixed>
191 */
192 public function collapse()
193 {
194 return new static(function () {
195 foreach ($this as $values) {
196 if (is_array($values) || $values instanceof Enumerable) {
197 foreach ($values as $value) {
198 yield $value;
199 }
200 }
201 }
202 });
203 }
204
205 /**
206 * Collapse the collection of items into a single array while preserving its keys.
207 *
208 * @return static<mixed, mixed>
209 */
210 public function collapseWithKeys()
211 {
212 return new static(function () {
213 foreach ($this as $values) {
214 if (is_array($values) || $values instanceof Enumerable) {
215 foreach ($values as $key => $value) {
216 yield $key => $value;
217 }
218 }
219 }
220 });
221 }
222
223 /**
224 * Determine if an item exists in the enumerable.
225 *
226 * @param (callable(TValue, TKey): bool)|TValue|string $key
227 * @param mixed $operator
228 * @param mixed $value
229 * @return bool
230 */
231 public function contains($key, $operator = null, $value = null)
232 {
233 if (func_num_args() === 1 && $this->useAsCallable($key)) {
234 $placeholder = new stdClass;
235
236 /** @var callable $key */
237 return $this->first($key, $placeholder) !== $placeholder;
238 }
239
240 if (func_num_args() === 1) {
241 $needle = $key;
242
243 foreach ($this as $value) {
244 if ($value == $needle) {
245 return true;
246 }
247 }
248
249 return false;
250 }
251
252 return $this->contains($this->operatorForWhere(...func_get_args()));
253 }
254
255 /**
256 * Determine if an item exists, using strict comparison.
257 *
258 * @param (callable(TValue): bool)|TValue|array-key $key
259 * @param TValue|null $value
260 * @return bool
261 */
262 public function containsStrict($key, $value = null)
263 {
264 if (func_num_args() === 2) {
265 return $this->contains(fn ($item) => data_get($item, $key) === $value);
266 }
267
268 if ($this->useAsCallable($key)) {
269 return ! is_null($this->first($key));
270 }
271
272 foreach ($this as $item) {
273 if ($item === $key) {
274 return true;
275 }
276 }
277
278 return false;
279 }
280
281 /**
282 * Determine if an item is not contained in the enumerable.
283 *
284 * @param mixed $key
285 * @param mixed $operator
286 * @param mixed $value
287 * @return bool
288 */
289 public function doesntContain($key, $operator = null, $value = null)
290 {
291 return ! $this->contains(...func_get_args());
292 }
293
294 /**
295 * Determine if an item is not contained in the enumerable, using strict comparison.
296 *
297 * @param mixed $key
298 * @param mixed $operator
299 * @param mixed $value
300 * @return bool
301 */
302 public function doesntContainStrict($key, $operator = null, $value = null)
303 {
304 return ! $this->containsStrict(...func_get_args());
305 }
306
307 /**
308 * {@inheritDoc}
309 */
310 #[\Override]
311 public function crossJoin(...$arrays)
312 {
313 return $this->passthru(__FUNCTION__, func_get_args());
314 }
315
316 /**
317 * Count the number of items in the collection by a field or using a callback.
318 *
319 * @param (callable(TValue, TKey): (array-key|\UnitEnum))|string|null $countBy
320 * @return static<array-key, int>
321 */
322 public function countBy($countBy = null)
323 {
324 $countBy = is_null($countBy)
325 ? $this->identity()
326 : $this->valueRetriever($countBy);
327
328 return new static(function () use ($countBy) {
329 $counts = [];
330
331 foreach ($this as $key => $value) {
332 $group = enum_value($countBy($value, $key));
333
334 if (empty($counts[$group])) {
335 $counts[$group] = 0;
336 }
337
338 $counts[$group]++;
339 }
340
341 yield from $counts;
342 });
343 }
344
345 /**
346 * {@inheritDoc}
347 */
348 #[\Override]
349 public function diff($items)
350 {
351 return $this->passthru(__FUNCTION__, func_get_args());
352 }
353
354 /**
355 * {@inheritDoc}
356 */
357 #[\Override]
358 public function diffUsing($items, callable $callback)
359 {
360 return $this->passthru(__FUNCTION__, func_get_args());
361 }
362
363 /**
364 * {@inheritDoc}
365 */
366 #[\Override]
367 public function diffAssoc($items)
368 {
369 return $this->passthru(__FUNCTION__, func_get_args());
370 }
371
372 /**
373 * {@inheritDoc}
374 */
375 #[\Override]
376 public function diffAssocUsing($items, callable $callback)
377 {
378 return $this->passthru(__FUNCTION__, func_get_args());
379 }
380
381 /**
382 * {@inheritDoc}
383 */
384 #[\Override]
385 public function diffKeys($items)
386 {
387 return $this->passthru(__FUNCTION__, func_get_args());
388 }
389
390 /**
391 * {@inheritDoc}
392 */
393 #[\Override]
394 public function diffKeysUsing($items, callable $callback)
395 {
396 return $this->passthru(__FUNCTION__, func_get_args());
397 }
398
399 /**
400 * {@inheritDoc}
401 */
402 #[\Override]
403 public function duplicates($callback = null, $strict = false)
404 {
405 return $this->passthru(__FUNCTION__, func_get_args());
406 }
407
408 /**
409 * {@inheritDoc}
410 */
411 #[\Override]
412 public function duplicatesStrict($callback = null)
413 {
414 return $this->passthru(__FUNCTION__, func_get_args());
415 }
416
417 /**
418 * {@inheritDoc}
419 */
420 #[\Override]
421 public function except($keys)
422 {
423 return $this->passthru(__FUNCTION__, func_get_args());
424 }
425
426 /**
427 * Run a filter over each of the items.
428 *
429 * @param (callable(TValue, TKey): bool)|null $callback
430 * @return static
431 */
432 public function filter(?callable $callback = null)
433 {
434 if (is_null($callback)) {
435 $callback = fn ($value) => (bool) $value;
436 }
437
438 return new static(function () use ($callback) {
439 foreach ($this as $key => $value) {
440 if ($callback($value, $key)) {
441 yield $key => $value;
442 }
443 }
444 });
445 }
446
447 /**
448 * Get the first item from the enumerable passing the given truth test.
449 *
450 * @template TFirstDefault
451 *
452 * @param (callable(TValue): bool)|null $callback
453 * @param TFirstDefault|(\Closure(): TFirstDefault) $default
454 * @return TValue|TFirstDefault
455 */
456 public function first(?callable $callback = null, $default = null)
457 {
458 $iterator = $this->getIterator();
459
460 if (is_null($callback)) {
461 if (! $iterator->valid()) {
462 return value($default);
463 }
464
465 return $iterator->current();
466 }
467
468 foreach ($iterator as $key => $value) {
469 if ($callback($value, $key)) {
470 return $value;
471 }
472 }
473
474 return value($default);
475 }
476
477 /**
478 * Get a flattened list of the items in the collection.
479 *
480 * @param int $depth
481 * @return static<int, mixed>
482 */
483 public function flatten($depth = INF)
484 {
485 $instance = new static(function () use ($depth) {
486 foreach ($this as $item) {
487 if (! is_array($item) && ! $item instanceof Enumerable) {
488 yield $item;
489 } elseif ($depth === 1) {
490 yield from $item;
491 } else {
492 yield from (new static($item))->flatten($depth - 1);
493 }
494 }
495 });
496
497 return $instance->values();
498 }
499
500 /**
501 * Flip the items in the collection.
502 *
503 * @return static<TValue, TKey>
504 */
505 public function flip()
506 {
507 return new static(function () {
508 foreach ($this as $key => $value) {
509 yield $value => $key;
510 }
511 });
512 }
513
514 /**
515 * Get an item by key.
516 *
517 * @template TGetDefault
518 *
519 * @param TKey|null $key
520 * @param TGetDefault|(\Closure(): TGetDefault) $default
521 * @return TValue|TGetDefault
522 */
523 public function get($key, $default = null)
524 {
525 if (is_null($key)) {
526 return;
527 }
528
529 foreach ($this as $outerKey => $outerValue) {
530 if ($outerKey == $key) {
531 return $outerValue;
532 }
533 }
534
535 return value($default);
536 }
537
538 /**
539 * {@inheritDoc}
540 *
541 * @template TGroupKey of array-key|\UnitEnum|\Stringable
542 *
543 * @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
544 * @return static<
545 * ($groupBy is (array|string)
546 * ? array-key
547 * : (TGroupKey is \UnitEnum ? array-key : (TGroupKey is \Stringable ? string : TGroupKey))),
548 * static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>
549 * >
550 */
551 #[\Override]
552 public function groupBy($groupBy, $preserveKeys = false)
553 {
554 return $this->passthru(__FUNCTION__, func_get_args());
555 }
556
557 /**
558 * Key an associative array by a field or using a callback.
559 *
560 * @template TNewKey of array-key|\UnitEnum
561 *
562 * @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
563 * @return static<($keyBy is (array|string) ? array-key : (TNewKey is \UnitEnum ? array-key : TNewKey)), TValue>
564 */
565 public function keyBy($keyBy)
566 {
567 return new static(function () use ($keyBy) {
568 $keyBy = $this->valueRetriever($keyBy);
569
570 foreach ($this as $key => $item) {
571 $resolvedKey = $keyBy($item, $key);
572
573 if (is_object($resolvedKey)) {
574 $resolvedKey = (string) $resolvedKey;
575 }
576
577 yield $resolvedKey => $item;
578 }
579 });
580 }
581
582 /**
583 * Determine if an item exists in the collection by key.
584 *
585 * @param mixed $key
586 * @return bool
587 */
588 public function has($key)
589 {
590 $keys = array_flip(is_array($key) ? $key : func_get_args());
591 $count = count($keys);
592
593 foreach ($this as $key => $value) {
594 if (array_key_exists($key, $keys) && --$count == 0) {
595 return true;
596 }
597 }
598
599 return false;
600 }
601
602 /**
603 * Determine if any of the keys exist in the collection.
604 *
605 * @param mixed $key
606 * @return bool
607 */
608 public function hasAny($key)
609 {
610 $keys = array_flip(is_array($key) ? $key : func_get_args());
611
612 foreach ($this as $key => $value) {
613 if (array_key_exists($key, $keys)) {
614 return true;
615 }
616 }
617
618 return false;
619 }
620
621 /**
622 * Concatenate values of a given key as a string.
623 *
624 * @param (callable(TValue, TKey): mixed)|string $value
625 * @param string|null $glue
626 * @return string
627 */
628 public function implode($value, $glue = null)
629 {
630 return $this->collect()->implode(...func_get_args());
631 }
632
633 /**
634 * {@inheritDoc}
635 */
636 #[\Override]
637 public function intersect($items)
638 {
639 return $this->passthru(__FUNCTION__, func_get_args());
640 }
641
642 /**
643 * {@inheritDoc}
644 */
645 #[\Override]
646 public function intersectUsing($items, callable $callback)
647 {
648 return $this->passthru(__FUNCTION__, func_get_args());
649 }
650
651 /**
652 * {@inheritDoc}
653 */
654 #[\Override]
655 public function intersectAssoc($items)
656 {
657 return $this->passthru(__FUNCTION__, func_get_args());
658 }
659
660 /**
661 * {@inheritDoc}
662 */
663 #[\Override]
664 public function intersectAssocUsing($items, callable $callback)
665 {
666 return $this->passthru(__FUNCTION__, func_get_args());
667 }
668
669 /**
670 * {@inheritDoc}
671 */
672 #[\Override]
673 public function intersectByKeys($items)
674 {
675 return $this->passthru(__FUNCTION__, func_get_args());
676 }
677
678 /**
679 * Determine if the items are empty or not.
680 *
681 * @return bool
682 */
683 public function isEmpty()
684 {
685 return ! $this->getIterator()->valid();
686 }
687
688 /**
689 * Determine if the collection contains a single item.
690 *
691 * @param (callable(TValue, TKey): bool)|null $callback
692 * @return bool
693 *
694 * @deprecated 12.49.0 Use the `hasSole()` method instead.
695 */
696 public function containsOneItem(?callable $callback = null): bool
697 {
698 return $this->hasSole($callback);
699 }
700
701 /**
702 * Determine if the collection contains multiple items.
703 *
704 * @return bool
705 *
706 * @deprecated 12.50.0 Use the `hasMany()` method instead.
707 */
708 public function containsManyItems(): bool
709 {
710 return $this->hasMany();
711 }
712
713 /**
714 * Join all items from the collection using a string. The final items can use a separate glue string.
715 *
716 * @param string $glue
717 * @param string $finalGlue
718 * @return string
719 */
720 public function join($glue, $finalGlue = '')
721 {
722 return $this->collect()->join(...func_get_args());
723 }
724
725 /**
726 * Get the keys of the collection items.
727 *
728 * @return static<int, TKey>
729 */
730 public function keys()
731 {
732 return new static(function () {
733 foreach ($this as $key => $value) {
734 yield $key;
735 }
736 });
737 }
738
739 /**
740 * Get the last item from the collection.
741 *
742 * @template TLastDefault
743 *
744 * @param (callable(TValue, TKey): bool)|null $callback
745 * @param TLastDefault|(\Closure(): TLastDefault) $default
746 * @return TValue|TLastDefault
747 */
748 public function last(?callable $callback = null, $default = null)
749 {
750 $needle = $placeholder = new stdClass;
751
752 foreach ($this as $key => $value) {
753 if (is_null($callback) || $callback($value, $key)) {
754 $needle = $value;
755 }
756 }
757
758 return $needle === $placeholder ? value($default) : $needle;
759 }
760
761 /**
762 * Get the values of a given key.
763 *
764 * @param string|array<array-key, string> $value
765 * @param string|null $key
766 * @return static<array-key, mixed>
767 */
768 public function pluck($value, $key = null)
769 {
770 return new static(function () use ($value, $key) {
771 [$value, $key] = $this->explodePluckParameters($value, $key);
772
773 foreach ($this as $item) {
774 $itemValue = $value instanceof Closure
775 ? $value($item)
776 : data_get($item, $value);
777
778 if (is_null($key)) {
779 yield $itemValue;
780 } else {
781 $itemKey = $key instanceof Closure
782 ? $key($item)
783 : data_get($item, $key);
784
785 if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
786 $itemKey = (string) $itemKey;
787 }
788
789 yield $itemKey => $itemValue;
790 }
791 }
792 });
793 }
794
795 /**
796 * Run a map over each of the items.
797 *
798 * @template TMapValue
799 *
800 * @param callable(TValue, TKey): TMapValue $callback
801 * @return static<TKey, TMapValue>
802 */
803 public function map(callable $callback)
804 {
805 return new static(function () use ($callback) {
806 foreach ($this as $key => $value) {
807 yield $key => $callback($value, $key);
808 }
809 });
810 }
811
812 /**
813 * {@inheritDoc}
814 */
815 #[\Override]
816 public function mapToDictionary(callable $callback)
817 {
818 return $this->passthru(__FUNCTION__, func_get_args());
819 }
820
821 /**
822 * Run an associative map over each of the items.
823 *
824 * The callback should return an associative array with a single key/value pair.
825 *
826 * @template TMapWithKeysKey of array-key
827 * @template TMapWithKeysValue
828 *
829 * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback
830 * @return static<TMapWithKeysKey, TMapWithKeysValue>
831 */
832 public function mapWithKeys(callable $callback)
833 {
834 return new static(function () use ($callback) {
835 foreach ($this as $key => $value) {
836 yield from $callback($value, $key);
837 }
838 });
839 }
840
841 /**
842 * {@inheritDoc}
843 */
844 #[\Override]
845 public function merge($items)
846 {
847 return $this->passthru(__FUNCTION__, func_get_args());
848 }
849
850 /**
851 * {@inheritDoc}
852 */
853 #[\Override]
854 public function mergeRecursive($items)
855 {
856 return $this->passthru(__FUNCTION__, func_get_args());
857 }
858
859 /**
860 * Multiply the items in the collection by the multiplier.
861 *
862 * @param int $multiplier
863 * @return static
864 */
865 public function multiply(int $multiplier)
866 {
867 return $this->passthru(__FUNCTION__, func_get_args());
868 }
869
870 /**
871 * Create a collection by using this collection for keys and another for its values.
872 *
873 * @template TCombineValue
874 *
875 * @param \IteratorAggregate<array-key, TCombineValue>|array<array-key, TCombineValue>|(callable(): \Generator<array-key, TCombineValue>) $values
876 * @return static<TValue, TCombineValue>
877 */
878 public function combine($values)
879 {
880 return new static(function () use ($values) {
881 $values = $this->makeIterator($values);
882
883 $errorMessage = 'Both parameters should have an equal number of elements';
884
885 foreach ($this as $key) {
886 if (! $values->valid()) {
887 trigger_error($errorMessage, E_USER_WARNING);
888
889 break;
890 }
891
892 yield $key => $values->current();
893
894 $values->next();
895 }
896
897 if ($values->valid()) {
898 trigger_error($errorMessage, E_USER_WARNING);
899 }
900 });
901 }
902
903 /**
904 * {@inheritDoc}
905 */
906 #[\Override]
907 public function union($items)
908 {
909 return $this->passthru(__FUNCTION__, func_get_args());
910 }
911
912 /**
913 * Create a new collection consisting of every n-th element.
914 *
915 * @param int $step
916 * @param int $offset
917 * @return ($step is positive-int ? static : never)
918 *
919 * @throws \InvalidArgumentException
920 */
921 public function nth($step, $offset = 0)
922 {
923 if ($step < 1) {
924 throw new InvalidArgumentException('Step value must be at least 1.');
925 }
926
927 return new static(function () use ($step, $offset) {
928 $position = 0;
929
930 foreach ($this->slice($offset) as $item) {
931 if ($position % $step === 0) {
932 yield $item;
933 }
934
935 $position++;
936 }
937 });
938 }
939
940 /**
941 * Get the items with the specified keys.
942 *
943 * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
944 * @return static
945 */
946 public function only($keys)
947 {
948 if ($keys instanceof Enumerable) {
949 $keys = $keys->all();
950 } elseif (! is_null($keys)) {
951 $keys = is_array($keys) ? $keys : func_get_args();
952 }
953
954 return new static(function () use ($keys) {
955 if (is_null($keys)) {
956 yield from $this;
957 } else {
958 $keys = array_flip($keys);
959
960 foreach ($this as $key => $value) {
961 if (array_key_exists($key, $keys)) {
962 yield $key => $value;
963
964 unset($keys[$key]);
965
966 if (empty($keys)) {
967 break;
968 }
969 }
970 }
971 }
972 });
973 }
974
975 /**
976 * Select specific values from the items within the collection.
977 *
978 * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
979 * @return static
980 */
981 public function select($keys)
982 {
983 if ($keys instanceof Enumerable) {
984 $keys = $keys->all();
985 } elseif (! is_null($keys)) {
986 $keys = is_array($keys) ? $keys : func_get_args();
987 }
988
989 return new static(function () use ($keys) {
990 if (is_null($keys)) {
991 yield from $this;
992 } else {
993 foreach ($this as $item) {
994 $result = [];
995
996 foreach ($keys as $key) {
997 if (Arr::accessible($item) && Arr::exists($item, $key)) {
998 $result[$key] = $item[$key];
999 } elseif (is_object($item) && isset($item->{$key})) {
1000 $result[$key] = $item->{$key};
1001 }
1002 }
1003
1004 yield $result;
1005 }
1006 }
1007 });
1008 }
1009
1010 /**
1011 * Push all of the given items onto the collection.
1012 *
1013 * @template TConcatKey of array-key
1014 * @template TConcatValue
1015 *
1016 * @param iterable<TConcatKey, TConcatValue> $source
1017 * @return static<TKey|TConcatKey, TValue|TConcatValue>
1018 */
1019 public function concat($source)
1020 {
1021 return (new static(function () use ($source) {
1022 yield from $this;
1023 yield from $source;
1024 }))->values();
1025 }
1026
1027 /**
1028 * Get one or a specified number of items randomly from the collection.
1029 *
1030 * @param int|null $number
1031 * @param bool $preserveKeys
1032 * @return static<int, TValue>|TValue
1033 *
1034 * @throws \InvalidArgumentException
1035 */
1036 public function random($number = null, $preserveKeys = false)
1037 {
1038 $result = $this->collect()->random(...func_get_args());
1039
1040 return is_null($number) ? $result : new static($result);
1041 }
1042
1043 /**
1044 * Replace the collection items with the given items.
1045 *
1046 * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
1047 * @return static
1048 */
1049 public function replace($items)
1050 {
1051 return new static(function () use ($items) {
1052 $items = $this->getArrayableItems($items);
1053
1054 foreach ($this as $key => $value) {
1055 if (array_key_exists($key, $items)) {
1056 yield $key => $items[$key];
1057
1058 unset($items[$key]);
1059 } else {
1060 yield $key => $value;
1061 }
1062 }
1063
1064 foreach ($items as $key => $value) {
1065 yield $key => $value;
1066 }
1067 });
1068 }
1069
1070 /**
1071 * {@inheritDoc}
1072 */
1073 #[\Override]
1074 public function replaceRecursive($items)
1075 {
1076 return $this->passthru(__FUNCTION__, func_get_args());
1077 }
1078
1079 /**
1080 * {@inheritDoc}
1081 */
1082 #[\Override]
1083 public function reverse()
1084 {
1085 return $this->passthru(__FUNCTION__, func_get_args());
1086 }
1087
1088 /**
1089 * Search the collection for a given value and return the corresponding key if successful.
1090 *
1091 * @param TValue|(callable(TValue,TKey): bool) $value
1092 * @param bool $strict
1093 * @return TKey|false
1094 */
1095 public function search($value, $strict = false)
1096 {
1097 /** @var (callable(TValue,TKey): bool) $predicate */
1098 $predicate = $this->useAsCallable($value)
1099 ? $value
1100 : function ($item) use ($value, $strict) {
1101 return $strict ? $item === $value : $item == $value;
1102 };
1103
1104 foreach ($this as $key => $item) {
1105 if ($predicate($item, $key)) {
1106 return $key;
1107 }
1108 }
1109
1110 return false;
1111 }
1112
1113 /**
1114 * Get the item before the given item.
1115 *
1116 * @param TValue|(callable(TValue,TKey): bool) $value
1117 * @param bool $strict
1118 * @return TValue|null
1119 */
1120 public function before($value, $strict = false)
1121 {
1122 $previous = null;
1123
1124 /** @var (callable(TValue,TKey): bool) $predicate */
1125 $predicate = $this->useAsCallable($value)
1126 ? $value
1127 : function ($item) use ($value, $strict) {
1128 return $strict ? $item === $value : $item == $value;
1129 };
1130
1131 foreach ($this as $key => $item) {
1132 if ($predicate($item, $key)) {
1133 return $previous;
1134 }
1135
1136 $previous = $item;
1137 }
1138
1139 return null;
1140 }
1141
1142 /**
1143 * Get the item after the given item.
1144 *
1145 * @param TValue|(callable(TValue,TKey): bool) $value
1146 * @param bool $strict
1147 * @return TValue|null
1148 */
1149 public function after($value, $strict = false)
1150 {
1151 $found = false;
1152
1153 /** @var (callable(TValue,TKey): bool) $predicate */
1154 $predicate = $this->useAsCallable($value)
1155 ? $value
1156 : function ($item) use ($value, $strict) {
1157 return $strict ? $item === $value : $item == $value;
1158 };
1159
1160 foreach ($this as $key => $item) {
1161 if ($found) {
1162 return $item;
1163 }
1164
1165 if ($predicate($item, $key)) {
1166 $found = true;
1167 }
1168 }
1169
1170 return null;
1171 }
1172
1173 /**
1174 * {@inheritDoc}
1175 */
1176 #[\Override]
1177 public function shuffle()
1178 {
1179 return $this->passthru(__FUNCTION__, []);
1180 }
1181
1182 /**
1183 * Create chunks representing a "sliding window" view of the items in the collection.
1184 *
1185 * @param positive-int $size
1186 * @param positive-int $step
1187 * @return static<int, static>
1188 *
1189 * @throws \InvalidArgumentException
1190 */
1191 public function sliding($size = 2, $step = 1)
1192 {
1193 if ($size < 1) {
1194 throw new InvalidArgumentException('Size value must be at least 1.');
1195 } elseif ($step < 1) {
1196 throw new InvalidArgumentException('Step value must be at least 1.');
1197 }
1198
1199 return new static(function () use ($size, $step) {
1200 $iterator = $this->getIterator();
1201
1202 $chunk = [];
1203
1204 while ($iterator->valid()) {
1205 $chunk[$iterator->key()] = $iterator->current();
1206
1207 if (count($chunk) == $size) {
1208 yield (new static($chunk))->tap(function () use (&$chunk, $step) {
1209 $chunk = array_slice($chunk, $step, null, true);
1210 });
1211
1212 // If the $step between chunks is bigger than each chunk's $size
1213 // we will skip the extra items (which should never be in any
1214 // chunk) before we continue to the next chunk in the loop.
1215 if ($step > $size) {
1216 $skip = $step - $size;
1217
1218 for ($i = 0; $i < $skip && $iterator->valid(); $i++) {
1219 $iterator->next();
1220 }
1221 }
1222 }
1223
1224 $iterator->next();
1225 }
1226 });
1227 }
1228
1229 /**
1230 * Skip the first {$count} items.
1231 *
1232 * @param int $count
1233 * @return static
1234 */
1235 public function skip($count)
1236 {
1237 return new static(function () use ($count) {
1238 $iterator = $this->getIterator();
1239
1240 while ($iterator->valid() && $count--) {
1241 $iterator->next();
1242 }
1243
1244 while ($iterator->valid()) {
1245 yield $iterator->key() => $iterator->current();
1246
1247 $iterator->next();
1248 }
1249 });
1250 }
1251
1252 /**
1253 * Skip items in the collection until the given condition is met.
1254 *
1255 * @param TValue|callable(TValue,TKey): bool $value
1256 * @return static
1257 */
1258 public function skipUntil($value)
1259 {
1260 $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
1261
1262 return $this->skipWhile($this->negate($callback));
1263 }
1264
1265 /**
1266 * Skip items in the collection while the given condition is met.
1267 *
1268 * @param TValue|callable(TValue,TKey): bool $value
1269 * @return static
1270 */
1271 public function skipWhile($value)
1272 {
1273 $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
1274
1275 return new static(function () use ($callback) {
1276 $iterator = $this->getIterator();
1277
1278 while ($iterator->valid() && $callback($iterator->current(), $iterator->key())) {
1279 $iterator->next();
1280 }
1281
1282 while ($iterator->valid()) {
1283 yield $iterator->key() => $iterator->current();
1284
1285 $iterator->next();
1286 }
1287 });
1288 }
1289
1290 /**
1291 * {@inheritDoc}
1292 */
1293 #[\Override]
1294 public function slice($offset, $length = null)
1295 {
1296 if ($offset < 0 || $length < 0) {
1297 return $this->passthru(__FUNCTION__, func_get_args());
1298 }
1299
1300 $instance = $this->skip($offset);
1301
1302 return is_null($length) ? $instance : $instance->take($length);
1303 }
1304
1305 /**
1306 * {@inheritDoc}
1307 *
1308 * @throws \InvalidArgumentException
1309 */
1310 #[\Override]
1311 public function split($numberOfGroups)
1312 {
1313 if ($numberOfGroups < 1) {
1314 throw new InvalidArgumentException('Number of groups must be at least 1.');
1315 }
1316
1317 return $this->passthru(__FUNCTION__, func_get_args());
1318 }
1319
1320 /**
1321 * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
1322 *
1323 * @param (callable(TValue, TKey): bool)|string|null $key
1324 * @param mixed $operator
1325 * @param mixed $value
1326 * @return TValue
1327 *
1328 * @throws \Illuminate\Support\ItemNotFoundException
1329 * @throws \Illuminate\Support\MultipleItemsFoundException
1330 */
1331 public function sole($key = null, $operator = null, $value = null)
1332 {
1333 $filter = func_num_args() > 1
1334 ? $this->operatorForWhere(...func_get_args())
1335 : $key;
1336
1337 return $this
1338 ->unless($filter == null)
1339 ->filter($filter)
1340 ->take(2)
1341 ->collect()
1342 ->sole();
1343 }
1344
1345 /**
1346 * Determine if the collection contains a single item or a single item matching the given criteria.
1347 *
1348 * @param (callable(TValue, TKey): bool)|string|null $key
1349 * @param mixed $operator
1350 * @param mixed $value
1351 * @return bool
1352 */
1353 public function hasSole($key = null, $operator = null, $value = null): bool
1354 {
1355 $filter = func_num_args() > 1
1356 ? $this->operatorForWhere(...func_get_args())
1357 : $key;
1358
1359 return $this
1360 ->unless($filter == null)
1361 ->filter($filter)
1362 ->take(2)
1363 ->count() === 1;
1364 }
1365
1366 /**
1367 * Get the first item in the collection but throw an exception if no matching items exist.
1368 *
1369 * @param (callable(TValue, TKey): bool)|string|null $key
1370 * @param mixed $operator
1371 * @param mixed $value
1372 * @return TValue
1373 *
1374 * @throws \Illuminate\Support\ItemNotFoundException
1375 */
1376 public function firstOrFail($key = null, $operator = null, $value = null)
1377 {
1378 $filter = func_num_args() > 1
1379 ? $this->operatorForWhere(...func_get_args())
1380 : $key;
1381
1382 return $this
1383 ->unless($filter == null)
1384 ->filter($filter)
1385 ->take(1)
1386 ->collect()
1387 ->firstOrFail();
1388 }
1389
1390 /**
1391 * Chunk the collection into chunks of the given size.
1392 *
1393 * @param int $size
1394 * @param bool $preserveKeys
1395 * @return ($preserveKeys is true ? static<int, static> : static<int, static<int, TValue>>)
1396 */
1397 public function chunk($size, $preserveKeys = true)
1398 {
1399 if ($size <= 0) {
1400 return static::empty();
1401 }
1402
1403 $add = match ($preserveKeys) {
1404 true => fn (array &$chunk, Traversable $iterator) => $chunk[$iterator->key()] = $iterator->current(),
1405 false => fn (array &$chunk, Traversable $iterator) => $chunk[] = $iterator->current(),
1406 };
1407
1408 return new static(function () use ($size, $add) {
1409 $iterator = $this->getIterator();
1410
1411 while ($iterator->valid()) {
1412 $chunk = [];
1413
1414 while (true) {
1415 $add($chunk, $iterator);
1416
1417 if (count($chunk) < $size) {
1418 $iterator->next();
1419
1420 if (! $iterator->valid()) {
1421 break;
1422 }
1423 } else {
1424 break;
1425 }
1426 }
1427
1428 yield new static($chunk);
1429
1430 $iterator->next();
1431 }
1432 });
1433 }
1434
1435 /**
1436 * Split a collection into a certain number of groups, and fill the first groups completely.
1437 *
1438 * @param int $numberOfGroups
1439 * @return ($numberOfGroups is positive-int ? static<int, static> : never)
1440 *
1441 * @throws \InvalidArgumentException
1442 */
1443 public function splitIn($numberOfGroups)
1444 {
1445 if ($numberOfGroups < 1) {
1446 throw new InvalidArgumentException('Number of groups must be at least 1.');
1447 }
1448
1449 return $this->chunk((int) ceil($this->count() / $numberOfGroups));
1450 }
1451
1452 /**
1453 * Chunk the collection into chunks with a callback.
1454 *
1455 * @param callable(TValue, TKey, Collection<TKey, TValue>): bool $callback
1456 * @return static<int, static<TKey, TValue>>
1457 */
1458 public function chunkWhile(callable $callback)
1459 {
1460 return new static(function () use ($callback) {
1461 $iterator = $this->getIterator();
1462
1463 $chunk = new Collection;
1464
1465 if ($iterator->valid()) {
1466 $chunk[$iterator->key()] = $iterator->current();
1467
1468 $iterator->next();
1469 }
1470
1471 while ($iterator->valid()) {
1472 if (! $callback($iterator->current(), $iterator->key(), $chunk)) {
1473 yield new static($chunk);
1474
1475 $chunk = new Collection;
1476 }
1477
1478 $chunk[$iterator->key()] = $iterator->current();
1479
1480 $iterator->next();
1481 }
1482
1483 if ($chunk->isNotEmpty()) {
1484 yield new static($chunk);
1485 }
1486 });
1487 }
1488
1489 /**
1490 * {@inheritDoc}
1491 */
1492 #[\Override]
1493 public function sort($callback = null)
1494 {
1495 return $this->passthru(__FUNCTION__, func_get_args());
1496 }
1497
1498 /**
1499 * {@inheritDoc}
1500 */
1501 #[\Override]
1502 public function sortDesc($options = SORT_REGULAR)
1503 {
1504 return $this->passthru(__FUNCTION__, func_get_args());
1505 }
1506
1507 /**
1508 * {@inheritDoc}
1509 */
1510 #[\Override]
1511 public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
1512 {
1513 return $this->passthru(__FUNCTION__, func_get_args());
1514 }
1515
1516 /**
1517 * {@inheritDoc}
1518 */
1519 #[\Override]
1520 public function sortByDesc($callback, $options = SORT_REGULAR)
1521 {
1522 return $this->passthru(__FUNCTION__, func_get_args());
1523 }
1524
1525 /**
1526 * {@inheritDoc}
1527 */
1528 #[\Override]
1529 public function sortKeys($options = SORT_REGULAR, $descending = false)
1530 {
1531 return $this->passthru(__FUNCTION__, func_get_args());
1532 }
1533
1534 /**
1535 * {@inheritDoc}
1536 */
1537 #[\Override]
1538 public function sortKeysDesc($options = SORT_REGULAR)
1539 {
1540 return $this->passthru(__FUNCTION__, func_get_args());
1541 }
1542
1543 /**
1544 * {@inheritDoc}
1545 */
1546 #[\Override]
1547 public function sortKeysUsing(callable $callback)
1548 {
1549 return $this->passthru(__FUNCTION__, func_get_args());
1550 }
1551
1552 /**
1553 * Take the first or last {$limit} items.
1554 *
1555 * @param int $limit
1556 * @return static<TKey, TValue>
1557 */
1558 public function take($limit)
1559 {
1560 if ($limit < 0) {
1561 return new static(function () use ($limit) {
1562 $limit = abs($limit);
1563 $ringBuffer = [];
1564 $position = 0;
1565
1566 foreach ($this as $key => $value) {
1567 $ringBuffer[$position] = [$key, $value];
1568 $position = ($position + 1) % $limit;
1569 }
1570
1571 for ($i = 0, $end = min($limit, count($ringBuffer)); $i < $end; $i++) {
1572 $pointer = ($position + $i) % $limit;
1573 yield $ringBuffer[$pointer][0] => $ringBuffer[$pointer][1];
1574 }
1575 });
1576 }
1577
1578 return new static(function () use ($limit) {
1579 $iterator = $this->getIterator();
1580
1581 while ($limit--) {
1582 if (! $iterator->valid()) {
1583 break;
1584 }
1585
1586 yield $iterator->key() => $iterator->current();
1587
1588 if ($limit) {
1589 $iterator->next();
1590 }
1591 }
1592 });
1593 }
1594
1595 /**
1596 * Take items in the collection until the given condition is met.
1597 *
1598 * @param TValue|callable(TValue,TKey): bool $value
1599 * @return static<TKey, TValue>
1600 */
1601 public function takeUntil($value)
1602 {
1603 /** @var callable(TValue, TKey): bool $callback */
1604 $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
1605
1606 return new static(function () use ($callback) {
1607 foreach ($this as $key => $item) {
1608 if ($callback($item, $key)) {
1609 break;
1610 }
1611
1612 yield $key => $item;
1613 }
1614 });
1615 }
1616
1617 /**
1618 * Take items in the collection until a given point in time, with an optional callback on timeout.
1619 *
1620 * @param \DateTimeInterface $timeout
1621 * @param callable(TValue|null, TKey|null): mixed|null $callback
1622 * @return static<TKey, TValue>
1623 */
1624 public function takeUntilTimeout(DateTimeInterface $timeout, ?callable $callback = null)
1625 {
1626 $timeout = $timeout->getTimestamp();
1627
1628 return new static(function () use ($timeout, $callback) {
1629 if ($this->now() >= $timeout) {
1630 if ($callback) {
1631 $callback(null, null);
1632 }
1633
1634 return;
1635 }
1636
1637 foreach ($this as $key => $value) {
1638 yield $key => $value;
1639
1640 if ($this->now() >= $timeout) {
1641 if ($callback) {
1642 $callback($value, $key);
1643 }
1644
1645 break;
1646 }
1647 }
1648 });
1649 }
1650
1651 /**
1652 * Take items in the collection while the given condition is met.
1653 *
1654 * @param TValue|callable(TValue,TKey): bool $value
1655 * @return static<TKey, TValue>
1656 */
1657 public function takeWhile($value)
1658 {
1659 /** @var callable(TValue, TKey): bool $callback */
1660 $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
1661
1662 return $this->takeUntil(fn ($item, $key) => ! $callback($item, $key));
1663 }
1664
1665 /**
1666 * Pass each item in the collection to the given callback, lazily.
1667 *
1668 * @param callable(TValue, TKey): mixed $callback
1669 * @return static<TKey, TValue>
1670 */
1671 public function tapEach(callable $callback)
1672 {
1673 return new static(function () use ($callback) {
1674 foreach ($this as $key => $value) {
1675 $callback($value, $key);
1676
1677 yield $key => $value;
1678 }
1679 });
1680 }
1681
1682 /**
1683 * Throttle the values, releasing them at most once per the given seconds.
1684 *
1685 * @return static<TKey, TValue>
1686 */
1687 public function throttle(float $seconds)
1688 {
1689 return new static(function () use ($seconds) {
1690 $microseconds = $seconds * 1_000_000;
1691
1692 foreach ($this as $key => $value) {
1693 $fetchedAt = $this->preciseNow();
1694
1695 yield $key => $value;
1696
1697 $sleep = $microseconds - ($this->preciseNow() - $fetchedAt);
1698
1699 $this->usleep((int) $sleep);
1700 }
1701 });
1702 }
1703
1704 /**
1705 * Flatten a multi-dimensional associative array with dots.
1706 *
1707 * @param int $depth
1708 * @return static
1709 */
1710 public function dot($depth = INF)
1711 {
1712 return $this->passthru(__FUNCTION__, [$depth]);
1713 }
1714
1715 /**
1716 * {@inheritDoc}
1717 */
1718 #[\Override]
1719 public function undot()
1720 {
1721 return $this->passthru(__FUNCTION__, []);
1722 }
1723
1724 /**
1725 * Return only unique items from the collection array.
1726 *
1727 * @param (callable(TValue, TKey): mixed)|string|null $key
1728 * @param bool $strict
1729 * @return static<TKey, TValue>
1730 */
1731 public function unique($key = null, $strict = false)
1732 {
1733 $callback = $this->valueRetriever($key);
1734
1735 return new static(function () use ($callback, $strict) {
1736 $exists = [];
1737
1738 foreach ($this as $key => $item) {
1739 if (! in_array($id = $callback($item, $key), $exists, $strict)) {
1740 yield $key => $item;
1741
1742 $exists[] = $id;
1743 }
1744 }
1745 });
1746 }
1747
1748 /**
1749 * Reset the keys on the underlying array.
1750 *
1751 * @return static<int, TValue>
1752 */
1753 public function values()
1754 {
1755 return new static(function () {
1756 foreach ($this as $item) {
1757 yield $item;
1758 }
1759 });
1760 }
1761
1762 /**
1763 * Run the given callback every time the interval has passed.
1764 *
1765 * @return static<TKey, TValue>
1766 */
1767 public function withHeartbeat(DateInterval|int $interval, callable $callback)
1768 {
1769 $seconds = is_int($interval) ? $interval : $this->intervalSeconds($interval);
1770
1771 return new static(function () use ($seconds, $callback) {
1772 $start = $this->now();
1773
1774 foreach ($this as $key => $value) {
1775 $now = $this->now();
1776
1777 if (($now - $start) >= $seconds) {
1778 $callback();
1779
1780 $start = $now;
1781 }
1782
1783 yield $key => $value;
1784 }
1785 });
1786 }
1787
1788 /**
1789 * Get the total seconds from the given interval.
1790 */
1791 protected function intervalSeconds(DateInterval $interval): int
1792 {
1793 $start = new DateTimeImmutable();
1794
1795 return $start->add($interval)->getTimestamp() - $start->getTimestamp();
1796 }
1797
1798 /**
1799 * Zip the collection together with one or more arrays.
1800 *
1801 * e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]);
1802 * => [[1, 4], [2, 5], [3, 6]]
1803 *
1804 * @template TZipValue
1805 *
1806 * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
1807 * @return static<int, static<int, TValue|TZipValue>>
1808 */
1809 public function zip($items)
1810 {
1811 $iterables = func_get_args();
1812
1813 return new static(function () use ($iterables) {
1814 $iterators = (new Collection($iterables))
1815 ->map(fn ($iterable) => $this->makeIterator($iterable))
1816 ->prepend($this->getIterator());
1817
1818 while ($iterators->contains->valid()) {
1819 yield new static($iterators->map->current());
1820
1821 $iterators->each->next();
1822 }
1823 });
1824 }
1825
1826 /**
1827 * {@inheritDoc}
1828 */
1829 #[\Override]
1830 public function pad($size, $value)
1831 {
1832 if ($size < 0) {
1833 return $this->passthru(__FUNCTION__, func_get_args());
1834 }
1835
1836 return new static(function () use ($size, $value) {
1837 $yielded = 0;
1838
1839 foreach ($this as $index => $item) {
1840 yield $index => $item;
1841
1842 $yielded++;
1843 }
1844
1845 while ($yielded++ < $size) {
1846 yield $value;
1847 }
1848 });
1849 }
1850
1851 /**
1852 * Get the values iterator.
1853 *
1854 * @return \Traversable<TKey, TValue>
1855 */
1856 public function getIterator(): Traversable
1857 {
1858 return $this->makeIterator($this->source);
1859 }
1860
1861 /**
1862 * Count the number of items in the collection.
1863 *
1864 * @return int
1865 */
1866 public function count(): int
1867 {
1868 if (is_array($this->source)) {
1869 return count($this->source);
1870 }
1871
1872 return iterator_count($this->getIterator());
1873 }
1874
1875 /**
1876 * Make an iterator from the given source.
1877 *
1878 * @template TIteratorKey of array-key
1879 * @template TIteratorValue
1880 *
1881 * @param \IteratorAggregate<TIteratorKey, TIteratorValue>|array<TIteratorKey, TIteratorValue>|(callable(): \Generator<TIteratorKey, TIteratorValue>) $source
1882 * @return \Traversable<TIteratorKey, TIteratorValue>
1883 */
1884 protected function makeIterator($source)
1885 {
1886 if ($source instanceof IteratorAggregate) {
1887 return $source->getIterator();
1888 }
1889
1890 if (is_array($source)) {
1891 return new ArrayIterator($source);
1892 }
1893
1894 if (is_callable($source)) {
1895 $maybeTraversable = $source();
1896
1897 return $maybeTraversable instanceof Traversable
1898 ? $maybeTraversable
1899 : new ArrayIterator(Arr::wrap($maybeTraversable));
1900 }
1901
1902 return new ArrayIterator((array) $source);
1903 }
1904
1905 /**
1906 * Explode the "value" and "key" arguments passed to "pluck".
1907 *
1908 * @param string|string[] $value
1909 * @param string|string[]|null $key
1910 * @return array{string[],string[]|null}
1911 */
1912 protected function explodePluckParameters($value, $key)
1913 {
1914 $value = is_string($value) ? explode('.', $value) : $value;
1915
1916 $key = is_null($key) || is_array($key) || $key instanceof Closure ? $key : explode('.', $key);
1917
1918 return [$value, $key];
1919 }
1920
1921 /**
1922 * Pass this lazy collection through a method on the collection class.
1923 *
1924 * @param string $method
1925 * @param array<mixed> $params
1926 * @return static
1927 */
1928 protected function passthru($method, array $params)
1929 {
1930 return new static(function () use ($method, $params) {
1931 yield from $this->collect()->$method(...$params);
1932 });
1933 }
1934
1935 /**
1936 * Get the current time.
1937 *
1938 * @return int
1939 */
1940 protected function now()
1941 {
1942 return class_exists(Carbon::class)
1943 ? Carbon::now()->timestamp
1944 : time();
1945 }
1946
1947 /**
1948 * Get the precise current time.
1949 *
1950 * @return float
1951 */
1952 protected function preciseNow()
1953 {
1954 return class_exists(Carbon::class)
1955 ? Carbon::now()->getPreciseTimestamp()
1956 : microtime(true) * 1_000_000;
1957 }
1958
1959 /**
1960 * Sleep for the given amount of microseconds.
1961 *
1962 * @return void
1963 */
1964 protected function usleep(int $microseconds)
1965 {
1966 if ($microseconds <= 0) {
1967 return;
1968 }
1969
1970 class_exists(Sleep::class)
1971 ? Sleep::usleep($microseconds)
1972 : usleep($microseconds);
1973 }
1974 }