Enumerable.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. <?php
  2. namespace Illuminate\Support;
  3. use CachingIterator;
  4. use Countable;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Contracts\Support\Jsonable;
  7. use IteratorAggregate;
  8. use JsonSerializable;
  9. use Traversable;
  10. /**
  11. * @template TKey of array-key
  12. *
  13. * @template-covariant TValue
  14. *
  15. * @extends \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
  16. * @extends \IteratorAggregate<TKey, TValue>
  17. */
  18. interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
  19. {
  20. /**
  21. * Create a new collection instance if the value isn't one already.
  22. *
  23. * @template TMakeKey of array-key
  24. * @template TMakeValue
  25. *
  26. * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|null $items
  27. * @return static<TMakeKey, TMakeValue>
  28. */
  29. public static function make($items = []);
  30. /**
  31. * Create a new instance by invoking the callback a given amount of times.
  32. *
  33. * @param int $number
  34. * @param callable|null $callback
  35. * @return static
  36. */
  37. public static function times($number, ?callable $callback = null);
  38. /**
  39. * Create a collection with the given range.
  40. *
  41. * @param int $from
  42. * @param int $to
  43. * @param int $step
  44. * @return static
  45. */
  46. public static function range($from, $to, $step = 1);
  47. /**
  48. * Wrap the given value in a collection if applicable.
  49. *
  50. * @template TWrapValue
  51. *
  52. * @param iterable<array-key, TWrapValue>|TWrapValue $value
  53. * @return static<array-key, TWrapValue>
  54. */
  55. public static function wrap($value);
  56. /**
  57. * Get the underlying items from the given collection if applicable.
  58. *
  59. * @template TUnwrapKey of array-key
  60. * @template TUnwrapValue
  61. *
  62. * @param array<TUnwrapKey, TUnwrapValue>|static<TUnwrapKey, TUnwrapValue> $value
  63. * @return array<TUnwrapKey, TUnwrapValue>
  64. */
  65. public static function unwrap($value);
  66. /**
  67. * Create a new instance with no items.
  68. *
  69. * @return static
  70. */
  71. public static function empty();
  72. /**
  73. * Get all items in the enumerable.
  74. *
  75. * @return array
  76. */
  77. public function all();
  78. /**
  79. * Alias for the "avg" method.
  80. *
  81. * @param (callable(TValue): float|int)|string|null $callback
  82. * @return float|int|null
  83. */
  84. public function average($callback = null);
  85. /**
  86. * Get the median of a given key.
  87. *
  88. * @param string|array<array-key, string>|null $key
  89. * @return float|int|null
  90. */
  91. public function median($key = null);
  92. /**
  93. * Get the mode of a given key.
  94. *
  95. * @param string|array<array-key, string>|null $key
  96. * @return array<int, float|int>|null
  97. */
  98. public function mode($key = null);
  99. /**
  100. * Collapse the items into a single enumerable.
  101. *
  102. * @return static<int, mixed>
  103. */
  104. public function collapse();
  105. /**
  106. * Alias for the "contains" method.
  107. *
  108. * @param (callable(TValue, TKey): bool)|TValue|string $key
  109. * @param mixed $operator
  110. * @param mixed $value
  111. * @return bool
  112. */
  113. public function some($key, $operator = null, $value = null);
  114. /**
  115. * Determine if an item exists, using strict comparison.
  116. *
  117. * @param (callable(TValue): bool)|TValue|array-key $key
  118. * @param TValue|null $value
  119. * @return bool
  120. */
  121. public function containsStrict($key, $value = null);
  122. /**
  123. * Get the average value of a given key.
  124. *
  125. * @param (callable(TValue): float|int)|string|null $callback
  126. * @return float|int|null
  127. */
  128. public function avg($callback = null);
  129. /**
  130. * Determine if an item exists in the enumerable.
  131. *
  132. * @param (callable(TValue, TKey): bool)|TValue|string $key
  133. * @param mixed $operator
  134. * @param mixed $value
  135. * @return bool
  136. */
  137. public function contains($key, $operator = null, $value = null);
  138. /**
  139. * Determine if an item is not contained in the collection.
  140. *
  141. * @param mixed $key
  142. * @param mixed $operator
  143. * @param mixed $value
  144. * @return bool
  145. */
  146. public function doesntContain($key, $operator = null, $value = null);
  147. /**
  148. * Cross join with the given lists, returning all possible permutations.
  149. *
  150. * @template TCrossJoinKey
  151. * @template TCrossJoinValue
  152. *
  153. * @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$lists
  154. * @return static<int, array<int, TValue|TCrossJoinValue>>
  155. */
  156. public function crossJoin(...$lists);
  157. /**
  158. * Dump the collection and end the script.
  159. *
  160. * @param mixed ...$args
  161. * @return never
  162. */
  163. public function dd(...$args);
  164. /**
  165. * Dump the collection.
  166. *
  167. * @param mixed ...$args
  168. * @return $this
  169. */
  170. public function dump(...$args);
  171. /**
  172. * Get the items that are not present in the given items.
  173. *
  174. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  175. * @return static
  176. */
  177. public function diff($items);
  178. /**
  179. * Get the items that are not present in the given items, using the callback.
  180. *
  181. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  182. * @param callable(TValue, TValue): int $callback
  183. * @return static
  184. */
  185. public function diffUsing($items, callable $callback);
  186. /**
  187. * Get the items whose keys and values are not present in the given items.
  188. *
  189. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  190. * @return static
  191. */
  192. public function diffAssoc($items);
  193. /**
  194. * Get the items whose keys and values are not present in the given items, using the callback.
  195. *
  196. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  197. * @param callable(TKey, TKey): int $callback
  198. * @return static
  199. */
  200. public function diffAssocUsing($items, callable $callback);
  201. /**
  202. * Get the items whose keys are not present in the given items.
  203. *
  204. * @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
  205. * @return static
  206. */
  207. public function diffKeys($items);
  208. /**
  209. * Get the items whose keys are not present in the given items, using the callback.
  210. *
  211. * @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
  212. * @param callable(TKey, TKey): int $callback
  213. * @return static
  214. */
  215. public function diffKeysUsing($items, callable $callback);
  216. /**
  217. * Retrieve duplicate items.
  218. *
  219. * @param (callable(TValue): bool)|string|null $callback
  220. * @param bool $strict
  221. * @return static
  222. */
  223. public function duplicates($callback = null, $strict = false);
  224. /**
  225. * Retrieve duplicate items using strict comparison.
  226. *
  227. * @param (callable(TValue): bool)|string|null $callback
  228. * @return static
  229. */
  230. public function duplicatesStrict($callback = null);
  231. /**
  232. * Execute a callback over each item.
  233. *
  234. * @param callable(TValue, TKey): mixed $callback
  235. * @return $this
  236. */
  237. public function each(callable $callback);
  238. /**
  239. * Execute a callback over each nested chunk of items.
  240. *
  241. * @param callable $callback
  242. * @return static
  243. */
  244. public function eachSpread(callable $callback);
  245. /**
  246. * Determine if all items pass the given truth test.
  247. *
  248. * @param (callable(TValue, TKey): bool)|TValue|string $key
  249. * @param mixed $operator
  250. * @param mixed $value
  251. * @return bool
  252. */
  253. public function every($key, $operator = null, $value = null);
  254. /**
  255. * Get all items except for those with the specified keys.
  256. *
  257. * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys
  258. * @return static
  259. */
  260. public function except($keys);
  261. /**
  262. * Run a filter over each of the items.
  263. *
  264. * @param (callable(TValue): bool)|null $callback
  265. * @return static
  266. */
  267. public function filter(?callable $callback = null);
  268. /**
  269. * Apply the callback if the given "value" is (or resolves to) truthy.
  270. *
  271. * @template TWhenReturnType as null
  272. *
  273. * @param bool $value
  274. * @param (callable($this): TWhenReturnType)|null $callback
  275. * @param (callable($this): TWhenReturnType)|null $default
  276. * @return $this|TWhenReturnType
  277. */
  278. public function when($value, ?callable $callback = null, ?callable $default = null);
  279. /**
  280. * Apply the callback if the collection is empty.
  281. *
  282. * @template TWhenEmptyReturnType
  283. *
  284. * @param (callable($this): TWhenEmptyReturnType) $callback
  285. * @param (callable($this): TWhenEmptyReturnType)|null $default
  286. * @return $this|TWhenEmptyReturnType
  287. */
  288. public function whenEmpty(callable $callback, ?callable $default = null);
  289. /**
  290. * Apply the callback if the collection is not empty.
  291. *
  292. * @template TWhenNotEmptyReturnType
  293. *
  294. * @param callable($this): TWhenNotEmptyReturnType $callback
  295. * @param (callable($this): TWhenNotEmptyReturnType)|null $default
  296. * @return $this|TWhenNotEmptyReturnType
  297. */
  298. public function whenNotEmpty(callable $callback, ?callable $default = null);
  299. /**
  300. * Apply the callback if the given "value" is (or resolves to) falsy.
  301. *
  302. * @template TUnlessReturnType
  303. *
  304. * @param bool $value
  305. * @param (callable($this): TUnlessReturnType) $callback
  306. * @param (callable($this): TUnlessReturnType)|null $default
  307. * @return $this|TUnlessReturnType
  308. */
  309. public function unless($value, callable $callback, ?callable $default = null);
  310. /**
  311. * Apply the callback unless the collection is empty.
  312. *
  313. * @template TUnlessEmptyReturnType
  314. *
  315. * @param callable($this): TUnlessEmptyReturnType $callback
  316. * @param (callable($this): TUnlessEmptyReturnType)|null $default
  317. * @return $this|TUnlessEmptyReturnType
  318. */
  319. public function unlessEmpty(callable $callback, ?callable $default = null);
  320. /**
  321. * Apply the callback unless the collection is not empty.
  322. *
  323. * @template TUnlessNotEmptyReturnType
  324. *
  325. * @param callable($this): TUnlessNotEmptyReturnType $callback
  326. * @param (callable($this): TUnlessNotEmptyReturnType)|null $default
  327. * @return $this|TUnlessNotEmptyReturnType
  328. */
  329. public function unlessNotEmpty(callable $callback, ?callable $default = null);
  330. /**
  331. * Filter items by the given key value pair.
  332. *
  333. * @param string $key
  334. * @param mixed $operator
  335. * @param mixed $value
  336. * @return static
  337. */
  338. public function where($key, $operator = null, $value = null);
  339. /**
  340. * Filter items where the value for the given key is null.
  341. *
  342. * @param string|null $key
  343. * @return static
  344. */
  345. public function whereNull($key = null);
  346. /**
  347. * Filter items where the value for the given key is not null.
  348. *
  349. * @param string|null $key
  350. * @return static
  351. */
  352. public function whereNotNull($key = null);
  353. /**
  354. * Filter items by the given key value pair using strict comparison.
  355. *
  356. * @param string $key
  357. * @param mixed $value
  358. * @return static
  359. */
  360. public function whereStrict($key, $value);
  361. /**
  362. * Filter items by the given key value pair.
  363. *
  364. * @param string $key
  365. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  366. * @param bool $strict
  367. * @return static
  368. */
  369. public function whereIn($key, $values, $strict = false);
  370. /**
  371. * Filter items by the given key value pair using strict comparison.
  372. *
  373. * @param string $key
  374. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  375. * @return static
  376. */
  377. public function whereInStrict($key, $values);
  378. /**
  379. * Filter items such that the value of the given key is between the given values.
  380. *
  381. * @param string $key
  382. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  383. * @return static
  384. */
  385. public function whereBetween($key, $values);
  386. /**
  387. * Filter items such that the value of the given key is not between the given values.
  388. *
  389. * @param string $key
  390. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  391. * @return static
  392. */
  393. public function whereNotBetween($key, $values);
  394. /**
  395. * Filter items by the given key value pair.
  396. *
  397. * @param string $key
  398. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  399. * @param bool $strict
  400. * @return static
  401. */
  402. public function whereNotIn($key, $values, $strict = false);
  403. /**
  404. * Filter items by the given key value pair using strict comparison.
  405. *
  406. * @param string $key
  407. * @param \Illuminate\Contracts\Support\Arrayable|iterable $values
  408. * @return static
  409. */
  410. public function whereNotInStrict($key, $values);
  411. /**
  412. * Filter the items, removing any items that don't match the given type(s).
  413. *
  414. * @template TWhereInstanceOf
  415. *
  416. * @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type
  417. * @return static<TKey, TWhereInstanceOf>
  418. */
  419. public function whereInstanceOf($type);
  420. /**
  421. * Get the first item from the enumerable passing the given truth test.
  422. *
  423. * @template TFirstDefault
  424. *
  425. * @param (callable(TValue,TKey): bool)|null $callback
  426. * @param TFirstDefault|(\Closure(): TFirstDefault) $default
  427. * @return TValue|TFirstDefault
  428. */
  429. public function first(?callable $callback = null, $default = null);
  430. /**
  431. * Get the first item by the given key value pair.
  432. *
  433. * @param string $key
  434. * @param mixed $operator
  435. * @param mixed $value
  436. * @return TValue|null
  437. */
  438. public function firstWhere($key, $operator = null, $value = null);
  439. /**
  440. * Get a flattened array of the items in the collection.
  441. *
  442. * @param int $depth
  443. * @return static
  444. */
  445. public function flatten($depth = INF);
  446. /**
  447. * Flip the values with their keys.
  448. *
  449. * @return static<TValue, TKey>
  450. */
  451. public function flip();
  452. /**
  453. * Get an item from the collection by key.
  454. *
  455. * @template TGetDefault
  456. *
  457. * @param TKey $key
  458. * @param TGetDefault|(\Closure(): TGetDefault) $default
  459. * @return TValue|TGetDefault
  460. */
  461. public function get($key, $default = null);
  462. /**
  463. * Group an associative array by a field or using a callback.
  464. *
  465. * @template TGroupKey of array-key
  466. *
  467. * @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
  468. * @param bool $preserveKeys
  469. * @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), ($groupBy is array ? mixed : TValue)>>
  470. */
  471. public function groupBy($groupBy, $preserveKeys = false);
  472. /**
  473. * Key an associative array by a field or using a callback.
  474. *
  475. * @template TNewKey of array-key
  476. *
  477. * @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
  478. * @return static<($keyBy is string ? array-key : ($keyBy is array ? array-key : TNewKey)), TValue>
  479. */
  480. public function keyBy($keyBy);
  481. /**
  482. * Determine if an item exists in the collection by key.
  483. *
  484. * @param TKey|array<array-key, TKey> $key
  485. * @return bool
  486. */
  487. public function has($key);
  488. /**
  489. * Determine if any of the keys exist in the collection.
  490. *
  491. * @param mixed $key
  492. * @return bool
  493. */
  494. public function hasAny($key);
  495. /**
  496. * Concatenate values of a given key as a string.
  497. *
  498. * @param (callable(TValue, TKey): mixed)|string $value
  499. * @param string|null $glue
  500. * @return string
  501. */
  502. public function implode($value, $glue = null);
  503. /**
  504. * Intersect the collection with the given items.
  505. *
  506. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  507. * @return static
  508. */
  509. public function intersect($items);
  510. /**
  511. * Intersect the collection with the given items, using the callback.
  512. *
  513. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  514. * @param callable(TValue, TValue): int $callback
  515. * @return static
  516. */
  517. public function intersectUsing($items, callable $callback);
  518. /**
  519. * Intersect the collection with the given items with additional index check.
  520. *
  521. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  522. * @return static
  523. */
  524. public function intersectAssoc($items);
  525. /**
  526. * Intersect the collection with the given items with additional index check, using the callback.
  527. *
  528. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
  529. * @param callable(TValue, TValue): int $callback
  530. * @return static
  531. */
  532. public function intersectAssocUsing($items, callable $callback);
  533. /**
  534. * Intersect the collection with the given items by key.
  535. *
  536. * @param \Illuminate\Contracts\Support\Arrayable<TKey, mixed>|iterable<TKey, mixed> $items
  537. * @return static
  538. */
  539. public function intersectByKeys($items);
  540. /**
  541. * Determine if the collection is empty or not.
  542. *
  543. * @return bool
  544. */
  545. public function isEmpty();
  546. /**
  547. * Determine if the collection is not empty.
  548. *
  549. * @return bool
  550. */
  551. public function isNotEmpty();
  552. /**
  553. * Determine if the collection contains a single item.
  554. *
  555. * @return bool
  556. */
  557. public function containsOneItem();
  558. /**
  559. * Join all items from the collection using a string. The final items can use a separate glue string.
  560. *
  561. * @param string $glue
  562. * @param string $finalGlue
  563. * @return string
  564. */
  565. public function join($glue, $finalGlue = '');
  566. /**
  567. * Get the keys of the collection items.
  568. *
  569. * @return static<int, TKey>
  570. */
  571. public function keys();
  572. /**
  573. * Get the last item from the collection.
  574. *
  575. * @template TLastDefault
  576. *
  577. * @param (callable(TValue, TKey): bool)|null $callback
  578. * @param TLastDefault|(\Closure(): TLastDefault) $default
  579. * @return TValue|TLastDefault
  580. */
  581. public function last(?callable $callback = null, $default = null);
  582. /**
  583. * Run a map over each of the items.
  584. *
  585. * @template TMapValue
  586. *
  587. * @param callable(TValue, TKey): TMapValue $callback
  588. * @return static<TKey, TMapValue>
  589. */
  590. public function map(callable $callback);
  591. /**
  592. * Run a map over each nested chunk of items.
  593. *
  594. * @param callable $callback
  595. * @return static
  596. */
  597. public function mapSpread(callable $callback);
  598. /**
  599. * Run a dictionary map over the items.
  600. *
  601. * The callback should return an associative array with a single key/value pair.
  602. *
  603. * @template TMapToDictionaryKey of array-key
  604. * @template TMapToDictionaryValue
  605. *
  606. * @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback
  607. * @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>>
  608. */
  609. public function mapToDictionary(callable $callback);
  610. /**
  611. * Run a grouping map over the items.
  612. *
  613. * The callback should return an associative array with a single key/value pair.
  614. *
  615. * @template TMapToGroupsKey of array-key
  616. * @template TMapToGroupsValue
  617. *
  618. * @param callable(TValue, TKey): array<TMapToGroupsKey, TMapToGroupsValue> $callback
  619. * @return static<TMapToGroupsKey, static<int, TMapToGroupsValue>>
  620. */
  621. public function mapToGroups(callable $callback);
  622. /**
  623. * Run an associative map over each of the items.
  624. *
  625. * The callback should return an associative array with a single key/value pair.
  626. *
  627. * @template TMapWithKeysKey of array-key
  628. * @template TMapWithKeysValue
  629. *
  630. * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback
  631. * @return static<TMapWithKeysKey, TMapWithKeysValue>
  632. */
  633. public function mapWithKeys(callable $callback);
  634. /**
  635. * Map a collection and flatten the result by a single level.
  636. *
  637. * @template TFlatMapKey of array-key
  638. * @template TFlatMapValue
  639. *
  640. * @param callable(TValue, TKey): (\Illuminate\Support\Collection<TFlatMapKey, TFlatMapValue>|array<TFlatMapKey, TFlatMapValue>) $callback
  641. * @return static<TFlatMapKey, TFlatMapValue>
  642. */
  643. public function flatMap(callable $callback);
  644. /**
  645. * Map the values into a new class.
  646. *
  647. * @template TMapIntoValue
  648. *
  649. * @param class-string<TMapIntoValue> $class
  650. * @return static<TKey, TMapIntoValue>
  651. */
  652. public function mapInto($class);
  653. /**
  654. * Merge the collection with the given items.
  655. *
  656. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  657. * @return static
  658. */
  659. public function merge($items);
  660. /**
  661. * Recursively merge the collection with the given items.
  662. *
  663. * @template TMergeRecursiveValue
  664. *
  665. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items
  666. * @return static<TKey, TValue|TMergeRecursiveValue>
  667. */
  668. public function mergeRecursive($items);
  669. /**
  670. * Create a collection by using this collection for keys and another for its values.
  671. *
  672. * @template TCombineValue
  673. *
  674. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TCombineValue>|iterable<array-key, TCombineValue> $values
  675. * @return static<TValue, TCombineValue>
  676. */
  677. public function combine($values);
  678. /**
  679. * Union the collection with the given items.
  680. *
  681. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  682. * @return static
  683. */
  684. public function union($items);
  685. /**
  686. * Get the min value of a given key.
  687. *
  688. * @param (callable(TValue):mixed)|string|null $callback
  689. * @return mixed
  690. */
  691. public function min($callback = null);
  692. /**
  693. * Get the max value of a given key.
  694. *
  695. * @param (callable(TValue):mixed)|string|null $callback
  696. * @return mixed
  697. */
  698. public function max($callback = null);
  699. /**
  700. * Create a new collection consisting of every n-th element.
  701. *
  702. * @param int $step
  703. * @param int $offset
  704. * @return static
  705. */
  706. public function nth($step, $offset = 0);
  707. /**
  708. * Get the items with the specified keys.
  709. *
  710. * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
  711. * @return static
  712. */
  713. public function only($keys);
  714. /**
  715. * "Paginate" the collection by slicing it into a smaller collection.
  716. *
  717. * @param int $page
  718. * @param int $perPage
  719. * @return static
  720. */
  721. public function forPage($page, $perPage);
  722. /**
  723. * Partition the collection into two arrays using the given callback or key.
  724. *
  725. * @param (callable(TValue, TKey): bool)|TValue|string $key
  726. * @param mixed $operator
  727. * @param mixed $value
  728. * @return static<int<0, 1>, static<TKey, TValue>>
  729. */
  730. public function partition($key, $operator = null, $value = null);
  731. /**
  732. * Push all of the given items onto the collection.
  733. *
  734. * @template TConcatKey of array-key
  735. * @template TConcatValue
  736. *
  737. * @param iterable<TConcatKey, TConcatValue> $source
  738. * @return static<TKey|TConcatKey, TValue|TConcatValue>
  739. */
  740. public function concat($source);
  741. /**
  742. * Get one or a specified number of items randomly from the collection.
  743. *
  744. * @param int|null $number
  745. * @return static<int, TValue>|TValue
  746. *
  747. * @throws \InvalidArgumentException
  748. */
  749. public function random($number = null);
  750. /**
  751. * Reduce the collection to a single value.
  752. *
  753. * @template TReduceInitial
  754. * @template TReduceReturnType
  755. *
  756. * @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
  757. * @param TReduceInitial $initial
  758. * @return TReduceInitial|TReduceReturnType
  759. */
  760. public function reduce(callable $callback, $initial = null);
  761. /**
  762. * Reduce the collection to multiple aggregate values.
  763. *
  764. * @param callable $callback
  765. * @param mixed ...$initial
  766. * @return array
  767. *
  768. * @throws \UnexpectedValueException
  769. */
  770. public function reduceSpread(callable $callback, ...$initial);
  771. /**
  772. * Replace the collection items with the given items.
  773. *
  774. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  775. * @return static
  776. */
  777. public function replace($items);
  778. /**
  779. * Recursively replace the collection items with the given items.
  780. *
  781. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
  782. * @return static
  783. */
  784. public function replaceRecursive($items);
  785. /**
  786. * Reverse items order.
  787. *
  788. * @return static
  789. */
  790. public function reverse();
  791. /**
  792. * Search the collection for a given value and return the corresponding key if successful.
  793. *
  794. * @param TValue|callable(TValue,TKey): bool $value
  795. * @param bool $strict
  796. * @return TKey|bool
  797. */
  798. public function search($value, $strict = false);
  799. /**
  800. * Get the item before the given item.
  801. *
  802. * @param TValue|(callable(TValue,TKey): bool) $value
  803. * @param bool $strict
  804. * @return TValue|null
  805. */
  806. public function before($value, $strict = false);
  807. /**
  808. * Get the item after the given item.
  809. *
  810. * @param TValue|(callable(TValue,TKey): bool) $value
  811. * @param bool $strict
  812. * @return TValue|null
  813. */
  814. public function after($value, $strict = false);
  815. /**
  816. * Shuffle the items in the collection.
  817. *
  818. * @return static
  819. */
  820. public function shuffle();
  821. /**
  822. * Create chunks representing a "sliding window" view of the items in the collection.
  823. *
  824. * @param int $size
  825. * @param int $step
  826. * @return static<int, static>
  827. */
  828. public function sliding($size = 2, $step = 1);
  829. /**
  830. * Skip the first {$count} items.
  831. *
  832. * @param int $count
  833. * @return static
  834. */
  835. public function skip($count);
  836. /**
  837. * Skip items in the collection until the given condition is met.
  838. *
  839. * @param TValue|callable(TValue,TKey): bool $value
  840. * @return static
  841. */
  842. public function skipUntil($value);
  843. /**
  844. * Skip items in the collection while the given condition is met.
  845. *
  846. * @param TValue|callable(TValue,TKey): bool $value
  847. * @return static
  848. */
  849. public function skipWhile($value);
  850. /**
  851. * Get a slice of items from the enumerable.
  852. *
  853. * @param int $offset
  854. * @param int|null $length
  855. * @return static
  856. */
  857. public function slice($offset, $length = null);
  858. /**
  859. * Split a collection into a certain number of groups.
  860. *
  861. * @param int $numberOfGroups
  862. * @return static<int, static>
  863. */
  864. public function split($numberOfGroups);
  865. /**
  866. * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
  867. *
  868. * @param (callable(TValue, TKey): bool)|string $key
  869. * @param mixed $operator
  870. * @param mixed $value
  871. * @return TValue
  872. *
  873. * @throws \Illuminate\Support\ItemNotFoundException
  874. * @throws \Illuminate\Support\MultipleItemsFoundException
  875. */
  876. public function sole($key = null, $operator = null, $value = null);
  877. /**
  878. * Get the first item in the collection but throw an exception if no matching items exist.
  879. *
  880. * @param (callable(TValue, TKey): bool)|string|null $key
  881. * @param mixed $operator
  882. * @param mixed $value
  883. * @return TValue
  884. *
  885. * @throws \Illuminate\Support\ItemNotFoundException
  886. */
  887. public function firstOrFail($key = null, $operator = null, $value = null);
  888. /**
  889. * Chunk the collection into chunks of the given size.
  890. *
  891. * @param int $size
  892. * @return static<int, static>
  893. */
  894. public function chunk($size);
  895. /**
  896. * Chunk the collection into chunks with a callback.
  897. *
  898. * @param callable(TValue, TKey, static<int, TValue>): bool $callback
  899. * @return static<int, static<int, TValue>>
  900. */
  901. public function chunkWhile(callable $callback);
  902. /**
  903. * Split a collection into a certain number of groups, and fill the first groups completely.
  904. *
  905. * @param int $numberOfGroups
  906. * @return static<int, static>
  907. */
  908. public function splitIn($numberOfGroups);
  909. /**
  910. * Sort through each item with a callback.
  911. *
  912. * @param (callable(TValue, TValue): int)|null|int $callback
  913. * @return static
  914. */
  915. public function sort($callback = null);
  916. /**
  917. * Sort items in descending order.
  918. *
  919. * @param int $options
  920. * @return static
  921. */
  922. public function sortDesc($options = SORT_REGULAR);
  923. /**
  924. * Sort the collection using the given callback.
  925. *
  926. * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
  927. * @param int $options
  928. * @param bool $descending
  929. * @return static
  930. */
  931. public function sortBy($callback, $options = SORT_REGULAR, $descending = false);
  932. /**
  933. * Sort the collection in descending order using the given callback.
  934. *
  935. * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
  936. * @param int $options
  937. * @return static
  938. */
  939. public function sortByDesc($callback, $options = SORT_REGULAR);
  940. /**
  941. * Sort the collection keys.
  942. *
  943. * @param int $options
  944. * @param bool $descending
  945. * @return static
  946. */
  947. public function sortKeys($options = SORT_REGULAR, $descending = false);
  948. /**
  949. * Sort the collection keys in descending order.
  950. *
  951. * @param int $options
  952. * @return static
  953. */
  954. public function sortKeysDesc($options = SORT_REGULAR);
  955. /**
  956. * Sort the collection keys using a callback.
  957. *
  958. * @param callable(TKey, TKey): int $callback
  959. * @return static
  960. */
  961. public function sortKeysUsing(callable $callback);
  962. /**
  963. * Get the sum of the given values.
  964. *
  965. * @param (callable(TValue): mixed)|string|null $callback
  966. * @return mixed
  967. */
  968. public function sum($callback = null);
  969. /**
  970. * Take the first or last {$limit} items.
  971. *
  972. * @param int $limit
  973. * @return static
  974. */
  975. public function take($limit);
  976. /**
  977. * Take items in the collection until the given condition is met.
  978. *
  979. * @param TValue|callable(TValue,TKey): bool $value
  980. * @return static
  981. */
  982. public function takeUntil($value);
  983. /**
  984. * Take items in the collection while the given condition is met.
  985. *
  986. * @param TValue|callable(TValue,TKey): bool $value
  987. * @return static
  988. */
  989. public function takeWhile($value);
  990. /**
  991. * Pass the collection to the given callback and then return it.
  992. *
  993. * @param callable(TValue): mixed $callback
  994. * @return $this
  995. */
  996. public function tap(callable $callback);
  997. /**
  998. * Pass the enumerable to the given callback and return the result.
  999. *
  1000. * @template TPipeReturnType
  1001. *
  1002. * @param callable($this): TPipeReturnType $callback
  1003. * @return TPipeReturnType
  1004. */
  1005. public function pipe(callable $callback);
  1006. /**
  1007. * Pass the collection into a new class.
  1008. *
  1009. * @template TPipeIntoValue
  1010. *
  1011. * @param class-string<TPipeIntoValue> $class
  1012. * @return TPipeIntoValue
  1013. */
  1014. public function pipeInto($class);
  1015. /**
  1016. * Pass the collection through a series of callable pipes and return the result.
  1017. *
  1018. * @param array<callable> $pipes
  1019. * @return mixed
  1020. */
  1021. public function pipeThrough($pipes);
  1022. /**
  1023. * Get the values of a given key.
  1024. *
  1025. * @param string|array<array-key, string> $value
  1026. * @param string|null $key
  1027. * @return static<array-key, mixed>
  1028. */
  1029. public function pluck($value, $key = null);
  1030. /**
  1031. * Create a collection of all elements that do not pass a given truth test.
  1032. *
  1033. * @param (callable(TValue, TKey): bool)|bool|TValue $callback
  1034. * @return static
  1035. */
  1036. public function reject($callback = true);
  1037. /**
  1038. * Convert a flatten "dot" notation array into an expanded array.
  1039. *
  1040. * @return static
  1041. */
  1042. public function undot();
  1043. /**
  1044. * Return only unique items from the collection array.
  1045. *
  1046. * @param (callable(TValue, TKey): mixed)|string|null $key
  1047. * @param bool $strict
  1048. * @return static
  1049. */
  1050. public function unique($key = null, $strict = false);
  1051. /**
  1052. * Return only unique items from the collection array using strict comparison.
  1053. *
  1054. * @param (callable(TValue, TKey): mixed)|string|null $key
  1055. * @return static
  1056. */
  1057. public function uniqueStrict($key = null);
  1058. /**
  1059. * Reset the keys on the underlying array.
  1060. *
  1061. * @return static<int, TValue>
  1062. */
  1063. public function values();
  1064. /**
  1065. * Pad collection to the specified length with a value.
  1066. *
  1067. * @template TPadValue
  1068. *
  1069. * @param int $size
  1070. * @param TPadValue $value
  1071. * @return static<int, TValue|TPadValue>
  1072. */
  1073. public function pad($size, $value);
  1074. /**
  1075. * Get the values iterator.
  1076. *
  1077. * @return \Traversable<TKey, TValue>
  1078. */
  1079. public function getIterator(): Traversable;
  1080. /**
  1081. * Count the number of items in the collection.
  1082. *
  1083. * @return int
  1084. */
  1085. public function count(): int;
  1086. /**
  1087. * Count the number of items in the collection by a field or using a callback.
  1088. *
  1089. * @param (callable(TValue, TKey): array-key)|string|null $countBy
  1090. * @return static<array-key, int>
  1091. */
  1092. public function countBy($countBy = null);
  1093. /**
  1094. * Zip the collection together with one or more arrays.
  1095. *
  1096. * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
  1097. * => [[1, 4], [2, 5], [3, 6]]
  1098. *
  1099. * @template TZipValue
  1100. *
  1101. * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
  1102. * @return static<int, static<int, TValue|TZipValue>>
  1103. */
  1104. public function zip($items);
  1105. /**
  1106. * Collect the values into a collection.
  1107. *
  1108. * @return \Illuminate\Support\Collection<TKey, TValue>
  1109. */
  1110. public function collect();
  1111. /**
  1112. * Get the collection of items as a plain array.
  1113. *
  1114. * @return array<TKey, mixed>
  1115. */
  1116. public function toArray();
  1117. /**
  1118. * Convert the object into something JSON serializable.
  1119. *
  1120. * @return mixed
  1121. */
  1122. public function jsonSerialize(): mixed;
  1123. /**
  1124. * Get the collection of items as JSON.
  1125. *
  1126. * @param int $options
  1127. * @return string
  1128. */
  1129. public function toJson($options = 0);
  1130. /**
  1131. * Get the collection of items as pretty print formatted JSON.
  1132. *
  1133. *
  1134. * @param int $options
  1135. * @return string
  1136. */
  1137. public function toPrettyJson(int $options = 0);
  1138. /**
  1139. * Get a CachingIterator instance.
  1140. *
  1141. * @param int $flags
  1142. * @return \CachingIterator
  1143. */
  1144. public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING);
  1145. /**
  1146. * Convert the collection to its string representation.
  1147. *
  1148. * @return string
  1149. */
  1150. public function __toString();
  1151. /**
  1152. * Indicate that the model's string representation should be escaped when __toString is invoked.
  1153. *
  1154. * @param bool $escape
  1155. * @return $this
  1156. */
  1157. public function escapeWhenCastingToString($escape = true);
  1158. /**
  1159. * Add a method to the list of proxied methods.
  1160. *
  1161. * @param string $method
  1162. * @return void
  1163. */
  1164. public static function proxy($method);
  1165. /**
  1166. * Dynamically access collection proxies.
  1167. *
  1168. * @param string $key
  1169. * @return mixed
  1170. *
  1171. * @throws \Exception
  1172. */
  1173. public function __get($key);
  1174. }