Enumerable.php 33 KB

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