LazyCollection.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayIterator;
  4. use Closure;
  5. use DateTimeInterface;
  6. use Generator;
  7. use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
  8. use Illuminate\Support\Traits\EnumeratesValues;
  9. use Illuminate\Support\Traits\Macroable;
  10. use InvalidArgumentException;
  11. use IteratorAggregate;
  12. use stdClass;
  13. use Traversable;
  14. /**
  15. * @template TKey of array-key
  16. *
  17. * @template-covariant TValue
  18. *
  19. * @implements \Illuminate\Support\Enumerable<TKey, TValue>
  20. */
  21. class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable
  22. {
  23. /**
  24. * @use \Illuminate\Support\Traits\EnumeratesValues<TKey, TValue>
  25. */
  26. use EnumeratesValues, Macroable;
  27. /**
  28. * The source from which to generate items.
  29. *
  30. * @var (Closure(): \Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue>
  31. */
  32. public $source;
  33. /**
  34. * Create a new lazy collection instance.
  35. *
  36. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null $source
  37. * @return void
  38. */
  39. public function __construct($source = null)
  40. {
  41. if ($source instanceof Closure || $source instanceof self) {
  42. $this->source = $source;
  43. } elseif (is_null($source)) {
  44. $this->source = static::empty();
  45. } elseif ($source instanceof Generator) {
  46. throw new InvalidArgumentException(
  47. 'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.'
  48. );
  49. } else {
  50. $this->source = $this->getArrayableItems($source);
  51. }
  52. }
  53. /**
  54. * Create a new collection instance if the value isn't one already.
  55. *
  56. * @template TMakeKey of array-key
  57. * @template TMakeValue
  58. *
  59. * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|(Closure(): \Generator<TMakeKey, TMakeValue, mixed, void>)|self<TMakeKey, TMakeValue>|array<TMakeKey, TMakeValue>|null $items
  60. * @return static<TMakeKey, TMakeValue>
  61. */
  62. public static function make($items = [])
  63. {
  64. return new static($items);
  65. }
  66. /**
  67. * Create a collection with the given range.
  68. *
  69. * @param int $from
  70. * @param int $to
  71. * @return static<int, int>
  72. */
  73. public static function range($from, $to)
  74. {
  75. return new static(function () use ($from, $to) {
  76. if ($from <= $to) {
  77. for (; $from <= $to; $from++) {
  78. yield $from;
  79. }
  80. } else {
  81. for (; $from >= $to; $from--) {
  82. yield $from;
  83. }
  84. }
  85. });
  86. }
  87. /**
  88. * Get all items in the enumerable.
  89. *
  90. * @return array<TKey, TValue>
  91. */
  92. public function all()
  93. {
  94. if (is_array($this->source)) {
  95. return $this->source;
  96. }
  97. return iterator_to_array($this->getIterator());
  98. }
  99. /**
  100. * Eager load all items into a new lazy collection backed by an array.
  101. *
  102. * @return static
  103. */
  104. public function eager()
  105. {
  106. return new static($this->all());
  107. }
  108. /**
  109. * Cache values as they're enumerated.
  110. *
  111. * @return static
  112. */
  113. public function remember()
  114. {
  115. $iterator = $this->getIterator();
  116. $iteratorIndex = 0;
  117. $cache = [];
  118. return new static(function () use ($iterator, &$iteratorIndex, &$cache) {
  119. for ($index = 0; true; $index++) {
  120. if (array_key_exists($index, $cache)) {
  121. yield $cache[$index][0] => $cache[$index][1];
  122. continue;
  123. }
  124. if ($iteratorIndex < $index) {
  125. $iterator->next();
  126. $iteratorIndex++;
  127. }
  128. if (! $iterator->valid()) {
  129. break;
  130. }
  131. $cache[$index] = [$iterator->key(), $iterator->current()];
  132. yield $cache[$index][0] => $cache[$index][1];
  133. }
  134. });
  135. }
  136. /**
  137. * Get the average value of a given key.
  138. *
  139. * @param (callable(TValue): float|int)|string|null $callback
  140. * @return float|int|null
  141. */
  142. public function avg($callback = null)
  143. {
  144. return $this->collect()->avg($callback);
  145. }
  146. /**
  147. * Get the median of a given key.
  148. *
  149. * @param string|array<array-key, string>|null $key
  150. * @return float|int|null
  151. */
  152. public function median($key = null)
  153. {
  154. return $this->collect()->median($key);
  155. }
  156. /**
  157. * Get the mode of a given key.
  158. *
  159. * @param string|array<string>|null $key
  160. * @return array<int, float|int>|null
  161. */
  162. public function mode($key = null)
  163. {
  164. return $this->collect()->mode($key);
  165. }
  166. /**
  167. * Collapse the collection of items into a single array.
  168. *
  169. * @return static<int, mixed>
  170. */
  171. public function collapse()
  172. {
  173. return new static(function () {
  174. foreach ($this as $values) {
  175. if (is_array($values) || $values instanceof Enumerable) {
  176. foreach ($values as $value) {
  177. yield $value;
  178. }
  179. }
  180. }
  181. });
  182. }
  183. /**
  184. * Determine if an item exists in the enumerable.
  185. *
  186. * @param (callable(TValue, TKey): bool)|TValue|string $key
  187. * @param mixed $operator
  188. * @param mixed $value
  189. * @return bool
  190. */
  191. public function contains($key, $operator = null, $value = null)
  192. {
  193. if (func_num_args() === 1 && $this->useAsCallable($key)) {
  194. $placeholder = new stdClass;
  195. /** @var callable $key */
  196. return $this->first($key, $placeholder) !== $placeholder;
  197. }
  198. if (func_num_args() === 1) {
  199. $needle = $key;
  200. foreach ($this as $value) {
  201. if ($value == $needle) {
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. return $this->contains($this->operatorForWhere(...func_get_args()));
  208. }
  209. /**
  210. * Determine if an item exists, using strict comparison.
  211. *
  212. * @param (callable(TValue): bool)|TValue|array-key $key
  213. * @param TValue|null $value
  214. * @return bool
  215. */
  216. public function containsStrict($key, $value = null)
  217. {
  218. if (func_num_args() === 2) {
  219. return $this->contains(fn ($item) => data_get($item, $key) === $value);
  220. }
  221. if ($this->useAsCallable($key)) {
  222. return ! is_null($this->first($key));
  223. }
  224. foreach ($this as $item) {
  225. if ($item === $key) {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. /**
  232. * Determine if an item is not contained in the enumerable.
  233. *
  234. * @param mixed $key
  235. * @param mixed $operator
  236. * @param mixed $value
  237. * @return bool
  238. */
  239. public function doesntContain($key, $operator = null, $value = null)
  240. {
  241. return ! $this->contains(...func_get_args());
  242. }
  243. /**
  244. * Cross join the given iterables, returning all possible permutations.
  245. *
  246. * @template TCrossJoinKey
  247. * @template TCrossJoinValue
  248. *
  249. * @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$arrays
  250. * @return static<int, array<int, TValue|TCrossJoinValue>>
  251. */
  252. public function crossJoin(...$arrays)
  253. {
  254. return $this->passthru('crossJoin', func_get_args());
  255. }
  256. /**
  257. * Count the number of items in the collection by a field or using a callback.
  258. *
  259. * @param (callable(TValue, TKey): array-key)|string|null $countBy
  260. * @return static<array-key, int>
  261. */
  262. public function countBy($countBy = null)
  263. {
  264. $countBy = is_null($countBy)
  265. ? $this->identity()
  266. : $this->valueRetriever($countBy);
  267. return new static(function () use ($countBy) {
  268. $counts = [];
  269. foreach ($this as $key => $value) {
  270. $group = $countBy($value, $key);
  271. if (empty($counts[$group])) {
  272. $counts[$group] = 0;
  273. }
  274. $counts[$group]++;
  275. }
  276. yield from $counts;
  277. });
  278. }
  279. /**
  280. * Get the items that are not present in the given items.
  281. *
  282. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  283. * @return static
  284. */
  285. public function diff($items)
  286. {
  287. return $this->passthru('diff', func_get_args());
  288. }
  289. /**
  290. * Get the items that are not present in the given items, using the callback.
  291. *
  292. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  293. * @param callable(TValue, TValue): int $callback
  294. * @return static
  295. */
  296. public function diffUsing($items, callable $callback)
  297. {
  298. return $this->passthru('diffUsing', func_get_args());
  299. }
  300. /**
  301. * Get the items whose keys and values are not present in the given items.
  302. *
  303. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  304. * @return static
  305. */
  306. public function diffAssoc($items)
  307. {
  308. return $this->passthru('diffAssoc', func_get_args());
  309. }
  310. /**
  311. * Get the items whose keys and values are not present in the given items, using the callback.
  312. *
  313. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  314. * @param callable(TKey, TKey): int $callback
  315. * @return static
  316. */
  317. public function diffAssocUsing($items, callable $callback)
  318. {
  319. return $this->passthru('diffAssocUsing', func_get_args());
  320. }
  321. /**
  322. * Get the items whose keys are not present in the given items.
  323. *
  324. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  325. * @return static
  326. */
  327. public function diffKeys($items)
  328. {
  329. return $this->passthru('diffKeys', func_get_args());
  330. }
  331. /**
  332. * Get the items whose keys are not present in the given items, using the callback.
  333. *
  334. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  335. * @param callable(TKey, TKey): int $callback
  336. * @return static
  337. */
  338. public function diffKeysUsing($items, callable $callback)
  339. {
  340. return $this->passthru('diffKeysUsing', func_get_args());
  341. }
  342. /**
  343. * Retrieve duplicate items.
  344. *
  345. * @param (callable(TValue): bool)|string|null $callback
  346. * @param bool $strict
  347. * @return static
  348. */
  349. public function duplicates($callback = null, $strict = false)
  350. {
  351. return $this->passthru('duplicates', func_get_args());
  352. }
  353. /**
  354. * Retrieve duplicate items using strict comparison.
  355. *
  356. * @param (callable(TValue): bool)|string|null $callback
  357. * @return static
  358. */
  359. public function duplicatesStrict($callback = null)
  360. {
  361. return $this->passthru('duplicatesStrict', func_get_args());
  362. }
  363. /**
  364. * Get all items except for those with the specified keys.
  365. *
  366. * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys
  367. * @return static
  368. */
  369. public function except($keys)
  370. {
  371. return $this->passthru('except', func_get_args());
  372. }
  373. /**
  374. * Run a filter over each of the items.
  375. *
  376. * @param (callable(TValue, TKey): bool)|null $callback
  377. * @return static
  378. */
  379. public function filter(?callable $callback = null)
  380. {
  381. if (is_null($callback)) {
  382. $callback = fn ($value) => (bool) $value;
  383. }
  384. return new static(function () use ($callback) {
  385. foreach ($this as $key => $value) {
  386. if ($callback($value, $key)) {
  387. yield $key => $value;
  388. }
  389. }
  390. });
  391. }
  392. /**
  393. * Get the first item from the enumerable passing the given truth test.
  394. *
  395. * @template TFirstDefault
  396. *
  397. * @param (callable(TValue): bool)|null $callback
  398. * @param TFirstDefault|(\Closure(): TFirstDefault) $default
  399. * @return TValue|TFirstDefault
  400. */
  401. public function first(?callable $callback = null, $default = null)
  402. {
  403. $iterator = $this->getIterator();
  404. if (is_null($callback)) {
  405. if (! $iterator->valid()) {
  406. return value($default);
  407. }
  408. return $iterator->current();
  409. }
  410. foreach ($iterator as $key => $value) {
  411. if ($callback($value, $key)) {
  412. return $value;
  413. }
  414. }
  415. return value($default);
  416. }
  417. /**
  418. * Get a flattened list of the items in the collection.
  419. *
  420. * @param int $depth
  421. * @return static<int, mixed>
  422. */
  423. public function flatten($depth = INF)
  424. {
  425. $instance = new static(function () use ($depth) {
  426. foreach ($this as $item) {
  427. if (! is_array($item) && ! $item instanceof Enumerable) {
  428. yield $item;
  429. } elseif ($depth === 1) {
  430. yield from $item;
  431. } else {
  432. yield from (new static($item))->flatten($depth - 1);
  433. }
  434. }
  435. });
  436. return $instance->values();
  437. }
  438. /**
  439. * Flip the items in the collection.
  440. *
  441. * @return static<TValue, TKey>
  442. */
  443. public function flip()
  444. {
  445. return new static(function () {
  446. foreach ($this as $key => $value) {
  447. yield $value => $key;
  448. }
  449. });
  450. }
  451. /**
  452. * Get an item by key.
  453. *
  454. * @template TGetDefault
  455. *
  456. * @param TKey|null $key
  457. * @param TGetDefault|(\Closure(): TGetDefault) $default
  458. * @return TValue|TGetDefault
  459. */
  460. public function get($key, $default = null)
  461. {
  462. if (is_null($key)) {
  463. return;
  464. }
  465. foreach ($this as $outerKey => $outerValue) {
  466. if ($outerKey == $key) {
  467. return $outerValue;
  468. }
  469. }
  470. return value($default);
  471. }
  472. /**
  473. * Group an associative array by a field or using a callback.
  474. *
  475. * @param (callable(TValue, TKey): array-key)|array|string $groupBy
  476. * @param bool $preserveKeys
  477. * @return static<array-key, static<array-key, TValue>>
  478. */
  479. public function groupBy($groupBy, $preserveKeys = false)
  480. {
  481. return $this->passthru('groupBy', func_get_args());
  482. }
  483. /**
  484. * Key an associative array by a field or using a callback.
  485. *
  486. * @param (callable(TValue, TKey): array-key)|array|string $keyBy
  487. * @return static<array-key, TValue>
  488. */
  489. public function keyBy($keyBy)
  490. {
  491. return new static(function () use ($keyBy) {
  492. $keyBy = $this->valueRetriever($keyBy);
  493. foreach ($this as $key => $item) {
  494. $resolvedKey = $keyBy($item, $key);
  495. if (is_object($resolvedKey)) {
  496. $resolvedKey = (string) $resolvedKey;
  497. }
  498. yield $resolvedKey => $item;
  499. }
  500. });
  501. }
  502. /**
  503. * Determine if an item exists in the collection by key.
  504. *
  505. * @param mixed $key
  506. * @return bool
  507. */
  508. public function has($key)
  509. {
  510. $keys = array_flip(is_array($key) ? $key : func_get_args());
  511. $count = count($keys);
  512. foreach ($this as $key => $value) {
  513. if (array_key_exists($key, $keys) && --$count == 0) {
  514. return true;
  515. }
  516. }
  517. return false;
  518. }
  519. /**
  520. * Determine if any of the keys exist in the collection.
  521. *
  522. * @param mixed $key
  523. * @return bool
  524. */
  525. public function hasAny($key)
  526. {
  527. $keys = array_flip(is_array($key) ? $key : func_get_args());
  528. foreach ($this as $key => $value) {
  529. if (array_key_exists($key, $keys)) {
  530. return true;
  531. }
  532. }
  533. return false;
  534. }
  535. /**
  536. * Concatenate values of a given key as a string.
  537. *
  538. * @param callable|string $value
  539. * @param string|null $glue
  540. * @return string
  541. */
  542. public function implode($value, $glue = null)
  543. {
  544. return $this->collect()->implode(...func_get_args());
  545. }
  546. /**
  547. * Intersect the collection with the given items.
  548. *
  549. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  550. * @return static
  551. */
  552. public function intersect($items)
  553. {
  554. return $this->passthru('intersect', func_get_args());
  555. }
  556. /**
  557. * Intersect the collection with the given items, using the callback.
  558. *
  559. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  560. * @param callable(TValue, TValue): int $callback
  561. * @return static
  562. */
  563. public function intersectUsing()
  564. {
  565. return $this->passthru('intersectUsing', func_get_args());
  566. }
  567. /**
  568. * Intersect the collection with the given items with additional index check.
  569. *
  570. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  571. * @return static
  572. */
  573. public function intersectAssoc($items)
  574. {
  575. return $this->passthru('intersectAssoc', func_get_args());
  576. }
  577. /**
  578. * Intersect the collection with the given items with additional index check, using the callback.
  579. *
  580. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  581. * @param callable(TValue, TValue): int $callback
  582. * @return static
  583. */
  584. public function intersectAssocUsing($items, callable $callback)
  585. {
  586. return $this->passthru('intersectAssocUsing', func_get_args());
  587. }
  588. /**
  589. * Intersect the collection with the given items by key.
  590. *
  591. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  592. * @return static
  593. */
  594. public function intersectByKeys($items)
  595. {
  596. return $this->passthru('intersectByKeys', func_get_args());
  597. }
  598. /**
  599. * Determine if the items are empty or not.
  600. *
  601. * @return bool
  602. */
  603. public function isEmpty()
  604. {
  605. return ! $this->getIterator()->valid();
  606. }
  607. /**
  608. * Determine if the collection contains a single item.
  609. *
  610. * @return bool
  611. */
  612. public function containsOneItem()
  613. {
  614. return $this->take(2)->count() === 1;
  615. }
  616. /**
  617. * Join all items from the collection using a string. The final items can use a separate glue string.
  618. *
  619. * @param string $glue
  620. * @param string $finalGlue
  621. * @return string
  622. */
  623. public function join($glue, $finalGlue = '')
  624. {
  625. return $this->collect()->join(...func_get_args());
  626. }
  627. /**
  628. * Get the keys of the collection items.
  629. *
  630. * @return static<int, TKey>
  631. */
  632. public function keys()
  633. {
  634. return new static(function () {
  635. foreach ($this as $key => $value) {
  636. yield $key;
  637. }
  638. });
  639. }
  640. /**
  641. * Get the last item from the collection.
  642. *
  643. * @template TLastDefault
  644. *
  645. * @param (callable(TValue, TKey): bool)|null $callback
  646. * @param TLastDefault|(\Closure(): TLastDefault) $default
  647. * @return TValue|TLastDefault
  648. */
  649. public function last(?callable $callback = null, $default = null)
  650. {
  651. $needle = $placeholder = new stdClass;
  652. foreach ($this as $key => $value) {
  653. if (is_null($callback) || $callback($value, $key)) {
  654. $needle = $value;
  655. }
  656. }
  657. return $needle === $placeholder ? value($default) : $needle;
  658. }
  659. /**
  660. * Get the values of a given key.
  661. *
  662. * @param string|array<array-key, string> $value
  663. * @param string|null $key
  664. * @return static<int, mixed>
  665. */
  666. public function pluck($value, $key = null)
  667. {
  668. return new static(function () use ($value, $key) {
  669. [$value, $key] = $this->explodePluckParameters($value, $key);
  670. foreach ($this as $item) {
  671. $itemValue = data_get($item, $value);
  672. if (is_null($key)) {
  673. yield $itemValue;
  674. } else {
  675. $itemKey = data_get($item, $key);
  676. if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
  677. $itemKey = (string) $itemKey;
  678. }
  679. yield $itemKey => $itemValue;
  680. }
  681. }
  682. });
  683. }
  684. /**
  685. * Run a map over each of the items.
  686. *
  687. * @template TMapValue
  688. *
  689. * @param callable(TValue, TKey): TMapValue $callback
  690. * @return static<TKey, TMapValue>
  691. */
  692. public function map(callable $callback)
  693. {
  694. return new static(function () use ($callback) {
  695. foreach ($this as $key => $value) {
  696. yield $key => $callback($value, $key);
  697. }
  698. });
  699. }
  700. /**
  701. * Run a dictionary map over the items.
  702. *
  703. * The callback should return an associative array with a single key/value pair.
  704. *
  705. * @template TMapToDictionaryKey of array-key
  706. * @template TMapToDictionaryValue
  707. *
  708. * @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback
  709. * @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>>
  710. */
  711. public function mapToDictionary(callable $callback)
  712. {
  713. return $this->passthru('mapToDictionary', func_get_args());
  714. }
  715. /**
  716. * Run an associative map over each of the items.
  717. *
  718. * The callback should return an associative array with a single key/value pair.
  719. *
  720. * @template TMapWithKeysKey of array-key
  721. * @template TMapWithKeysValue
  722. *
  723. * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback
  724. * @return static<TMapWithKeysKey, TMapWithKeysValue>
  725. */
  726. public function mapWithKeys(callable $callback)
  727. {
  728. return new static(function () use ($callback) {
  729. foreach ($this as $key => $value) {
  730. yield from $callback($value, $key);
  731. }
  732. });
  733. }
  734. /**
  735. * Merge the collection with the given items.
  736. *
  737. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  738. * @return static
  739. */
  740. public function merge($items)
  741. {
  742. return $this->passthru('merge', func_get_args());
  743. }
  744. /**
  745. * Recursively merge the collection with the given items.
  746. *
  747. * @template TMergeRecursiveValue
  748. *
  749. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items
  750. * @return static<TKey, TValue|TMergeRecursiveValue>
  751. */
  752. public function mergeRecursive($items)
  753. {
  754. return $this->passthru('mergeRecursive', func_get_args());
  755. }
  756. /**
  757. * Create a collection by using this collection for keys and another for its values.
  758. *
  759. * @template TCombineValue
  760. *
  761. * @param \IteratorAggregate<array-key, TCombineValue>|array<array-key, TCombineValue>|(callable(): \Generator<array-key, TCombineValue>) $values
  762. * @return static<TValue, TCombineValue>
  763. */
  764. public function combine($values)
  765. {
  766. return new static(function () use ($values) {
  767. $values = $this->makeIterator($values);
  768. $errorMessage = 'Both parameters should have an equal number of elements';
  769. foreach ($this as $key) {
  770. if (! $values->valid()) {
  771. trigger_error($errorMessage, E_USER_WARNING);
  772. break;
  773. }
  774. yield $key => $values->current();
  775. $values->next();
  776. }
  777. if ($values->valid()) {
  778. trigger_error($errorMessage, E_USER_WARNING);
  779. }
  780. });
  781. }
  782. /**
  783. * Union the collection with the given items.
  784. *
  785. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  786. * @return static
  787. */
  788. public function union($items)
  789. {
  790. return $this->passthru('union', func_get_args());
  791. }
  792. /**
  793. * Create a new collection consisting of every n-th element.
  794. *
  795. * @param int $step
  796. * @param int $offset
  797. * @return static
  798. */
  799. public function nth($step, $offset = 0)
  800. {
  801. return new static(function () use ($step, $offset) {
  802. $position = 0;
  803. foreach ($this->slice($offset) as $item) {
  804. if ($position % $step === 0) {
  805. yield $item;
  806. }
  807. $position++;
  808. }
  809. });
  810. }
  811. /**
  812. * Get the items with the specified keys.
  813. *
  814. * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
  815. * @return static
  816. */
  817. public function only($keys)
  818. {
  819. if ($keys instanceof Enumerable) {
  820. $keys = $keys->all();
  821. } elseif (! is_null($keys)) {
  822. $keys = is_array($keys) ? $keys : func_get_args();
  823. }
  824. return new static(function () use ($keys) {
  825. if (is_null($keys)) {
  826. yield from $this;
  827. } else {
  828. $keys = array_flip($keys);
  829. foreach ($this as $key => $value) {
  830. if (array_key_exists($key, $keys)) {
  831. yield $key => $value;
  832. unset($keys[$key]);
  833. if (empty($keys)) {
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. });
  840. }
  841. /**
  842. * Select specific values from the items within the collection.
  843. *
  844. * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
  845. * @return static
  846. */
  847. public function select($keys)
  848. {
  849. if ($keys instanceof Enumerable) {
  850. $keys = $keys->all();
  851. } elseif (! is_null($keys)) {
  852. $keys = is_array($keys) ? $keys : func_get_args();
  853. }
  854. return new static(function () use ($keys) {
  855. if (is_null($keys)) {
  856. yield from $this;
  857. } else {
  858. foreach ($this as $item) {
  859. $result = [];
  860. foreach ($keys as $key) {
  861. if (Arr::accessible($item) && Arr::exists($item, $key)) {
  862. $result[$key] = $item[$key];
  863. } elseif (is_object($item) && isset($item->{$key})) {
  864. $result[$key] = $item->{$key};
  865. }
  866. }
  867. yield $result;
  868. }
  869. }
  870. });
  871. }
  872. /**
  873. * Push all of the given items onto the collection.
  874. *
  875. * @template TConcatKey of array-key
  876. * @template TConcatValue
  877. *
  878. * @param iterable<TConcatKey, TConcatValue> $source
  879. * @return static<TKey|TConcatKey, TValue|TConcatValue>
  880. */
  881. public function concat($source)
  882. {
  883. return (new static(function () use ($source) {
  884. yield from $this;
  885. yield from $source;
  886. }))->values();
  887. }
  888. /**
  889. * Get one or a specified number of items randomly from the collection.
  890. *
  891. * @param int|null $number
  892. * @return static<int, TValue>|TValue
  893. *
  894. * @throws \InvalidArgumentException
  895. */
  896. public function random($number = null)
  897. {
  898. $result = $this->collect()->random(...func_get_args());
  899. return is_null($number) ? $result : new static($result);
  900. }
  901. /**
  902. * Replace the collection items with the given items.
  903. *
  904. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  905. * @return static
  906. */
  907. public function replace($items)
  908. {
  909. return new static(function () use ($items) {
  910. $items = $this->getArrayableItems($items);
  911. foreach ($this as $key => $value) {
  912. if (array_key_exists($key, $items)) {
  913. yield $key => $items[$key];
  914. unset($items[$key]);
  915. } else {
  916. yield $key => $value;
  917. }
  918. }
  919. foreach ($items as $key => $value) {
  920. yield $key => $value;
  921. }
  922. });
  923. }
  924. /**
  925. * Recursively replace the collection items with the given items.
  926. *
  927. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  928. * @return static
  929. */
  930. public function replaceRecursive($items)
  931. {
  932. return $this->passthru('replaceRecursive', func_get_args());
  933. }
  934. /**
  935. * Reverse items order.
  936. *
  937. * @return static
  938. */
  939. public function reverse()
  940. {
  941. return $this->passthru('reverse', func_get_args());
  942. }
  943. /**
  944. * Search the collection for a given value and return the corresponding key if successful.
  945. *
  946. * @param TValue|(callable(TValue,TKey): bool) $value
  947. * @param bool $strict
  948. * @return TKey|false
  949. */
  950. public function search($value, $strict = false)
  951. {
  952. /** @var (callable(TValue,TKey): bool) $predicate */
  953. $predicate = $this->useAsCallable($value)
  954. ? $value
  955. : function ($item) use ($value, $strict) {
  956. return $strict ? $item === $value : $item == $value;
  957. };
  958. foreach ($this as $key => $item) {
  959. if ($predicate($item, $key)) {
  960. return $key;
  961. }
  962. }
  963. return false;
  964. }
  965. /**
  966. * Shuffle the items in the collection.
  967. *
  968. * @param int|null $seed
  969. * @return static
  970. */
  971. public function shuffle($seed = null)
  972. {
  973. return $this->passthru('shuffle', func_get_args());
  974. }
  975. /**
  976. * Create chunks representing a "sliding window" view of the items in the collection.
  977. *
  978. * @param int $size
  979. * @param int $step
  980. * @return static<int, static>
  981. */
  982. public function sliding($size = 2, $step = 1)
  983. {
  984. return new static(function () use ($size, $step) {
  985. $iterator = $this->getIterator();
  986. $chunk = [];
  987. while ($iterator->valid()) {
  988. $chunk[$iterator->key()] = $iterator->current();
  989. if (count($chunk) == $size) {
  990. yield (new static($chunk))->tap(function () use (&$chunk, $step) {
  991. $chunk = array_slice($chunk, $step, null, true);
  992. });
  993. // If the $step between chunks is bigger than each chunk's $size
  994. // we will skip the extra items (which should never be in any
  995. // chunk) before we continue to the next chunk in the loop.
  996. if ($step > $size) {
  997. $skip = $step - $size;
  998. for ($i = 0; $i < $skip && $iterator->valid(); $i++) {
  999. $iterator->next();
  1000. }
  1001. }
  1002. }
  1003. $iterator->next();
  1004. }
  1005. });
  1006. }
  1007. /**
  1008. * Skip the first {$count} items.
  1009. *
  1010. * @param int $count
  1011. * @return static
  1012. */
  1013. public function skip($count)
  1014. {
  1015. return new static(function () use ($count) {
  1016. $iterator = $this->getIterator();
  1017. while ($iterator->valid() && $count--) {
  1018. $iterator->next();
  1019. }
  1020. while ($iterator->valid()) {
  1021. yield $iterator->key() => $iterator->current();
  1022. $iterator->next();
  1023. }
  1024. });
  1025. }
  1026. /**
  1027. * Skip items in the collection until the given condition is met.
  1028. *
  1029. * @param TValue|callable(TValue,TKey): bool $value
  1030. * @return static
  1031. */
  1032. public function skipUntil($value)
  1033. {
  1034. $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
  1035. return $this->skipWhile($this->negate($callback));
  1036. }
  1037. /**
  1038. * Skip items in the collection while the given condition is met.
  1039. *
  1040. * @param TValue|callable(TValue,TKey): bool $value
  1041. * @return static
  1042. */
  1043. public function skipWhile($value)
  1044. {
  1045. $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
  1046. return new static(function () use ($callback) {
  1047. $iterator = $this->getIterator();
  1048. while ($iterator->valid() && $callback($iterator->current(), $iterator->key())) {
  1049. $iterator->next();
  1050. }
  1051. while ($iterator->valid()) {
  1052. yield $iterator->key() => $iterator->current();
  1053. $iterator->next();
  1054. }
  1055. });
  1056. }
  1057. /**
  1058. * Get a slice of items from the enumerable.
  1059. *
  1060. * @param int $offset
  1061. * @param int|null $length
  1062. * @return static
  1063. */
  1064. public function slice($offset, $length = null)
  1065. {
  1066. if ($offset < 0 || $length < 0) {
  1067. return $this->passthru('slice', func_get_args());
  1068. }
  1069. $instance = $this->skip($offset);
  1070. return is_null($length) ? $instance : $instance->take($length);
  1071. }
  1072. /**
  1073. * Split a collection into a certain number of groups.
  1074. *
  1075. * @param int $numberOfGroups
  1076. * @return static<int, static>
  1077. */
  1078. public function split($numberOfGroups)
  1079. {
  1080. return $this->passthru('split', func_get_args());
  1081. }
  1082. /**
  1083. * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
  1084. *
  1085. * @param (callable(TValue, TKey): bool)|string $key
  1086. * @param mixed $operator
  1087. * @param mixed $value
  1088. * @return TValue
  1089. *
  1090. * @throws \Illuminate\Support\ItemNotFoundException
  1091. * @throws \Illuminate\Support\MultipleItemsFoundException
  1092. */
  1093. public function sole($key = null, $operator = null, $value = null)
  1094. {
  1095. $filter = func_num_args() > 1
  1096. ? $this->operatorForWhere(...func_get_args())
  1097. : $key;
  1098. return $this
  1099. ->unless($filter == null)
  1100. ->filter($filter)
  1101. ->take(2)
  1102. ->collect()
  1103. ->sole();
  1104. }
  1105. /**
  1106. * Get the first item in the collection but throw an exception if no matching items exist.
  1107. *
  1108. * @param (callable(TValue, TKey): bool)|string $key
  1109. * @param mixed $operator
  1110. * @param mixed $value
  1111. * @return TValue
  1112. *
  1113. * @throws \Illuminate\Support\ItemNotFoundException
  1114. */
  1115. public function firstOrFail($key = null, $operator = null, $value = null)
  1116. {
  1117. $filter = func_num_args() > 1
  1118. ? $this->operatorForWhere(...func_get_args())
  1119. : $key;
  1120. return $this
  1121. ->unless($filter == null)
  1122. ->filter($filter)
  1123. ->take(1)
  1124. ->collect()
  1125. ->firstOrFail();
  1126. }
  1127. /**
  1128. * Chunk the collection into chunks of the given size.
  1129. *
  1130. * @param int $size
  1131. * @return static<int, static>
  1132. */
  1133. public function chunk($size)
  1134. {
  1135. if ($size <= 0) {
  1136. return static::empty();
  1137. }
  1138. return new static(function () use ($size) {
  1139. $iterator = $this->getIterator();
  1140. while ($iterator->valid()) {
  1141. $chunk = [];
  1142. while (true) {
  1143. $chunk[$iterator->key()] = $iterator->current();
  1144. if (count($chunk) < $size) {
  1145. $iterator->next();
  1146. if (! $iterator->valid()) {
  1147. break;
  1148. }
  1149. } else {
  1150. break;
  1151. }
  1152. }
  1153. yield new static($chunk);
  1154. $iterator->next();
  1155. }
  1156. });
  1157. }
  1158. /**
  1159. * Split a collection into a certain number of groups, and fill the first groups completely.
  1160. *
  1161. * @param int $numberOfGroups
  1162. * @return static<int, static>
  1163. */
  1164. public function splitIn($numberOfGroups)
  1165. {
  1166. return $this->chunk(ceil($this->count() / $numberOfGroups));
  1167. }
  1168. /**
  1169. * Chunk the collection into chunks with a callback.
  1170. *
  1171. * @param callable(TValue, TKey, Collection<TKey, TValue>): bool $callback
  1172. * @return static<int, static<int, TValue>>
  1173. */
  1174. public function chunkWhile(callable $callback)
  1175. {
  1176. return new static(function () use ($callback) {
  1177. $iterator = $this->getIterator();
  1178. $chunk = new Collection;
  1179. if ($iterator->valid()) {
  1180. $chunk[$iterator->key()] = $iterator->current();
  1181. $iterator->next();
  1182. }
  1183. while ($iterator->valid()) {
  1184. if (! $callback($iterator->current(), $iterator->key(), $chunk)) {
  1185. yield new static($chunk);
  1186. $chunk = new Collection;
  1187. }
  1188. $chunk[$iterator->key()] = $iterator->current();
  1189. $iterator->next();
  1190. }
  1191. if ($chunk->isNotEmpty()) {
  1192. yield new static($chunk);
  1193. }
  1194. });
  1195. }
  1196. /**
  1197. * Sort through each item with a callback.
  1198. *
  1199. * @param (callable(TValue, TValue): int)|null|int $callback
  1200. * @return static
  1201. */
  1202. public function sort($callback = null)
  1203. {
  1204. return $this->passthru('sort', func_get_args());
  1205. }
  1206. /**
  1207. * Sort items in descending order.
  1208. *
  1209. * @param int $options
  1210. * @return static
  1211. */
  1212. public function sortDesc($options = SORT_REGULAR)
  1213. {
  1214. return $this->passthru('sortDesc', func_get_args());
  1215. }
  1216. /**
  1217. * Sort the collection using the given callback.
  1218. *
  1219. * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
  1220. * @param int $options
  1221. * @param bool $descending
  1222. * @return static
  1223. */
  1224. public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
  1225. {
  1226. return $this->passthru('sortBy', func_get_args());
  1227. }
  1228. /**
  1229. * Sort the collection in descending order using the given callback.
  1230. *
  1231. * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
  1232. * @param int $options
  1233. * @return static
  1234. */
  1235. public function sortByDesc($callback, $options = SORT_REGULAR)
  1236. {
  1237. return $this->passthru('sortByDesc', func_get_args());
  1238. }
  1239. /**
  1240. * Sort the collection keys.
  1241. *
  1242. * @param int $options
  1243. * @param bool $descending
  1244. * @return static
  1245. */
  1246. public function sortKeys($options = SORT_REGULAR, $descending = false)
  1247. {
  1248. return $this->passthru('sortKeys', func_get_args());
  1249. }
  1250. /**
  1251. * Sort the collection keys in descending order.
  1252. *
  1253. * @param int $options
  1254. * @return static
  1255. */
  1256. public function sortKeysDesc($options = SORT_REGULAR)
  1257. {
  1258. return $this->passthru('sortKeysDesc', func_get_args());
  1259. }
  1260. /**
  1261. * Sort the collection keys using a callback.
  1262. *
  1263. * @param callable(TKey, TKey): int $callback
  1264. * @return static
  1265. */
  1266. public function sortKeysUsing(callable $callback)
  1267. {
  1268. return $this->passthru('sortKeysUsing', func_get_args());
  1269. }
  1270. /**
  1271. * Take the first or last {$limit} items.
  1272. *
  1273. * @param int $limit
  1274. * @return static
  1275. */
  1276. public function take($limit)
  1277. {
  1278. if ($limit < 0) {
  1279. return new static(function () use ($limit) {
  1280. $limit = abs($limit);
  1281. $ringBuffer = [];
  1282. $position = 0;
  1283. foreach ($this as $key => $value) {
  1284. $ringBuffer[$position] = [$key, $value];
  1285. $position = ($position + 1) % $limit;
  1286. }
  1287. for ($i = 0, $end = min($limit, count($ringBuffer)); $i < $end; $i++) {
  1288. $pointer = ($position + $i) % $limit;
  1289. yield $ringBuffer[$pointer][0] => $ringBuffer[$pointer][1];
  1290. }
  1291. });
  1292. }
  1293. return new static(function () use ($limit) {
  1294. $iterator = $this->getIterator();
  1295. while ($limit--) {
  1296. if (! $iterator->valid()) {
  1297. break;
  1298. }
  1299. yield $iterator->key() => $iterator->current();
  1300. if ($limit) {
  1301. $iterator->next();
  1302. }
  1303. }
  1304. });
  1305. }
  1306. /**
  1307. * Take items in the collection until the given condition is met.
  1308. *
  1309. * @param TValue|callable(TValue,TKey): bool $value
  1310. * @return static
  1311. */
  1312. public function takeUntil($value)
  1313. {
  1314. /** @var callable(TValue, TKey): bool $callback */
  1315. $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
  1316. return new static(function () use ($callback) {
  1317. foreach ($this as $key => $item) {
  1318. if ($callback($item, $key)) {
  1319. break;
  1320. }
  1321. yield $key => $item;
  1322. }
  1323. });
  1324. }
  1325. /**
  1326. * Take items in the collection until a given point in time.
  1327. *
  1328. * @param \DateTimeInterface $timeout
  1329. * @return static
  1330. */
  1331. public function takeUntilTimeout(DateTimeInterface $timeout)
  1332. {
  1333. $timeout = $timeout->getTimestamp();
  1334. return new static(function () use ($timeout) {
  1335. if ($this->now() >= $timeout) {
  1336. return;
  1337. }
  1338. foreach ($this as $key => $value) {
  1339. yield $key => $value;
  1340. if ($this->now() >= $timeout) {
  1341. break;
  1342. }
  1343. }
  1344. });
  1345. }
  1346. /**
  1347. * Take items in the collection while the given condition is met.
  1348. *
  1349. * @param TValue|callable(TValue,TKey): bool $value
  1350. * @return static
  1351. */
  1352. public function takeWhile($value)
  1353. {
  1354. /** @var callable(TValue, TKey): bool $callback */
  1355. $callback = $this->useAsCallable($value) ? $value : $this->equality($value);
  1356. return $this->takeUntil(fn ($item, $key) => ! $callback($item, $key));
  1357. }
  1358. /**
  1359. * Pass each item in the collection to the given callback, lazily.
  1360. *
  1361. * @param callable(TValue, TKey): mixed $callback
  1362. * @return static
  1363. */
  1364. public function tapEach(callable $callback)
  1365. {
  1366. return new static(function () use ($callback) {
  1367. foreach ($this as $key => $value) {
  1368. $callback($value, $key);
  1369. yield $key => $value;
  1370. }
  1371. });
  1372. }
  1373. /**
  1374. * Flatten a multi-dimensional associative array with dots.
  1375. *
  1376. * @return static
  1377. */
  1378. public function dot()
  1379. {
  1380. return $this->passthru('dot', []);
  1381. }
  1382. /**
  1383. * Convert a flatten "dot" notation array into an expanded array.
  1384. *
  1385. * @return static
  1386. */
  1387. public function undot()
  1388. {
  1389. return $this->passthru('undot', []);
  1390. }
  1391. /**
  1392. * Return only unique items from the collection array.
  1393. *
  1394. * @param (callable(TValue, TKey): mixed)|string|null $key
  1395. * @param bool $strict
  1396. * @return static
  1397. */
  1398. public function unique($key = null, $strict = false)
  1399. {
  1400. $callback = $this->valueRetriever($key);
  1401. return new static(function () use ($callback, $strict) {
  1402. $exists = [];
  1403. foreach ($this as $key => $item) {
  1404. if (! in_array($id = $callback($item, $key), $exists, $strict)) {
  1405. yield $key => $item;
  1406. $exists[] = $id;
  1407. }
  1408. }
  1409. });
  1410. }
  1411. /**
  1412. * Reset the keys on the underlying array.
  1413. *
  1414. * @return static<int, TValue>
  1415. */
  1416. public function values()
  1417. {
  1418. return new static(function () {
  1419. foreach ($this as $item) {
  1420. yield $item;
  1421. }
  1422. });
  1423. }
  1424. /**
  1425. * Zip the collection together with one or more arrays.
  1426. *
  1427. * e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]);
  1428. * => [[1, 4], [2, 5], [3, 6]]
  1429. *
  1430. * @template TZipValue
  1431. *
  1432. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
  1433. * @return static<int, static<int, TValue|TZipValue>>
  1434. */
  1435. public function zip($items)
  1436. {
  1437. $iterables = func_get_args();
  1438. return new static(function () use ($iterables) {
  1439. $iterators = Collection::make($iterables)->map(function ($iterable) {
  1440. return $this->makeIterator($iterable);
  1441. })->prepend($this->getIterator());
  1442. while ($iterators->contains->valid()) {
  1443. yield new static($iterators->map->current());
  1444. $iterators->each->next();
  1445. }
  1446. });
  1447. }
  1448. /**
  1449. * Pad collection to the specified length with a value.
  1450. *
  1451. * @template TPadValue
  1452. *
  1453. * @param int $size
  1454. * @param TPadValue $value
  1455. * @return static<int, TValue|TPadValue>
  1456. */
  1457. public function pad($size, $value)
  1458. {
  1459. if ($size < 0) {
  1460. return $this->passthru('pad', func_get_args());
  1461. }
  1462. return new static(function () use ($size, $value) {
  1463. $yielded = 0;
  1464. foreach ($this as $index => $item) {
  1465. yield $index => $item;
  1466. $yielded++;
  1467. }
  1468. while ($yielded++ < $size) {
  1469. yield $value;
  1470. }
  1471. });
  1472. }
  1473. /**
  1474. * Get the values iterator.
  1475. *
  1476. * @return \Traversable<TKey, TValue>
  1477. */
  1478. public function getIterator(): Traversable
  1479. {
  1480. return $this->makeIterator($this->source);
  1481. }
  1482. /**
  1483. * Count the number of items in the collection.
  1484. *
  1485. * @return int
  1486. */
  1487. public function count(): int
  1488. {
  1489. if (is_array($this->source)) {
  1490. return count($this->source);
  1491. }
  1492. return iterator_count($this->getIterator());
  1493. }
  1494. /**
  1495. * Make an iterator from the given source.
  1496. *
  1497. * @template TIteratorKey of array-key
  1498. * @template TIteratorValue
  1499. *
  1500. * @param \IteratorAggregate<TIteratorKey, TIteratorValue>|array<TIteratorKey, TIteratorValue>|(callable(): \Generator<TIteratorKey, TIteratorValue>) $source
  1501. * @return \Traversable<TIteratorKey, TIteratorValue>
  1502. */
  1503. protected function makeIterator($source)
  1504. {
  1505. if ($source instanceof IteratorAggregate) {
  1506. return $source->getIterator();
  1507. }
  1508. if (is_array($source)) {
  1509. return new ArrayIterator($source);
  1510. }
  1511. if (is_callable($source)) {
  1512. $maybeTraversable = $source();
  1513. return $maybeTraversable instanceof Traversable
  1514. ? $maybeTraversable
  1515. : new ArrayIterator(Arr::wrap($maybeTraversable));
  1516. }
  1517. return new ArrayIterator((array) $source);
  1518. }
  1519. /**
  1520. * Explode the "value" and "key" arguments passed to "pluck".
  1521. *
  1522. * @param string|string[] $value
  1523. * @param string|string[]|null $key
  1524. * @return array{string[],string[]|null}
  1525. */
  1526. protected function explodePluckParameters($value, $key)
  1527. {
  1528. $value = is_string($value) ? explode('.', $value) : $value;
  1529. $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
  1530. return [$value, $key];
  1531. }
  1532. /**
  1533. * Pass this lazy collection through a method on the collection class.
  1534. *
  1535. * @param string $method
  1536. * @param array<mixed> $params
  1537. * @return static
  1538. */
  1539. protected function passthru($method, array $params)
  1540. {
  1541. return new static(function () use ($method, $params) {
  1542. yield from $this->collect()->$method(...$params);
  1543. });
  1544. }
  1545. /**
  1546. * Get the current time.
  1547. *
  1548. * @return int
  1549. */
  1550. protected function now()
  1551. {
  1552. return class_exists(Carbon::class)
  1553. ? Carbon::now()->timestamp
  1554. : time();
  1555. }
  1556. }