Enumerable.php 36 KB

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