CollectionInterface.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * This file is part of the ramsey/collection library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Collection;
  13. use Ramsey\Collection\Exception\CollectionMismatchException;
  14. use Ramsey\Collection\Exception\InvalidArgumentException;
  15. use Ramsey\Collection\Exception\InvalidPropertyOrMethod;
  16. use Ramsey\Collection\Exception\NoSuchElementException;
  17. use Ramsey\Collection\Exception\UnsupportedOperationException;
  18. /**
  19. * A collection represents a group of values, known as its elements.
  20. *
  21. * Some collections allow duplicate elements and others do not. Some are ordered
  22. * and others unordered.
  23. *
  24. * @template T
  25. * @extends ArrayInterface<T>
  26. */
  27. interface CollectionInterface extends ArrayInterface
  28. {
  29. /**
  30. * Ensures that this collection contains the specified element (optional
  31. * operation).
  32. *
  33. * Returns `true` if this collection changed as a result of the call.
  34. * (Returns `false` if this collection does not permit duplicates and
  35. * already contains the specified element.)
  36. *
  37. * Collections that support this operation may place limitations on what
  38. * elements may be added to this collection. In particular, some
  39. * collections will refuse to add `null` elements, and others will impose
  40. * restrictions on the type of elements that may be added. Collection
  41. * classes should clearly specify in their documentation any restrictions
  42. * on what elements may be added.
  43. *
  44. * If a collection refuses to add a particular element for any reason other
  45. * than that it already contains the element, it must throw an exception
  46. * (rather than returning `false`). This preserves the invariant that a
  47. * collection always contains the specified element after this call returns.
  48. *
  49. * @param T $element The element to add to the collection.
  50. *
  51. * @return bool `true` if this collection changed as a result of the call.
  52. *
  53. * @throws InvalidArgumentException if the collection refuses to add the
  54. * $element for any reason other than that it already contains the element.
  55. */
  56. public function add(mixed $element): bool;
  57. /**
  58. * Returns `true` if this collection contains the specified element.
  59. *
  60. * @param T $element The element to check whether the collection contains.
  61. * @param bool $strict Whether to perform a strict type check on the value.
  62. */
  63. public function contains(mixed $element, bool $strict = true): bool;
  64. /**
  65. * Returns the type associated with this collection.
  66. */
  67. public function getType(): string;
  68. /**
  69. * Removes a single instance of the specified element from this collection,
  70. * if it is present.
  71. *
  72. * @param T $element The element to remove from the collection.
  73. *
  74. * @return bool `true` if an element was removed as a result of this call.
  75. */
  76. public function remove(mixed $element): bool;
  77. /**
  78. * Returns the values from the given property, method, or array key.
  79. *
  80. * @param string $propertyOrMethod The name of the property, method, or
  81. * array key to evaluate and return.
  82. *
  83. * @return array<int, mixed>
  84. *
  85. * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist
  86. * on the elements in this collection.
  87. * @throws UnsupportedOperationException if unable to call column() on this
  88. * collection.
  89. */
  90. public function column(string $propertyOrMethod): array;
  91. /**
  92. * Returns the first item of the collection.
  93. *
  94. * @return T
  95. *
  96. * @throws NoSuchElementException if this collection is empty.
  97. */
  98. public function first(): mixed;
  99. /**
  100. * Returns the last item of the collection.
  101. *
  102. * @return T
  103. *
  104. * @throws NoSuchElementException if this collection is empty.
  105. */
  106. public function last(): mixed;
  107. /**
  108. * Sort the collection by a property, method, or array key with the given
  109. * sort order.
  110. *
  111. * If $propertyOrMethod is `null`, this will sort by comparing each element.
  112. *
  113. * This will always leave the original collection untouched and will return
  114. * a new one.
  115. *
  116. * @param string | null $propertyOrMethod The property, method, or array key
  117. * to sort by.
  118. * @param Sort $order The sort order for the resulting collection.
  119. *
  120. * @return CollectionInterface<T>
  121. *
  122. * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist
  123. * on the elements in this collection.
  124. * @throws UnsupportedOperationException if unable to call sort() on this
  125. * collection.
  126. */
  127. public function sort(?string $propertyOrMethod = null, Sort $order = Sort::Ascending): self;
  128. /**
  129. * Filter out items of the collection which don't match the criteria of
  130. * given callback.
  131. *
  132. * This will always leave the original collection untouched and will return
  133. * a new one.
  134. *
  135. * See the {@link http://php.net/manual/en/function.array-filter.php PHP array_filter() documentation}
  136. * for examples of how the `$callback` parameter works.
  137. *
  138. * @param callable(T): bool $callback A callable to use for filtering elements.
  139. *
  140. * @return CollectionInterface<T>
  141. */
  142. public function filter(callable $callback): self;
  143. /**
  144. * Create a new collection where the result of the given property, method,
  145. * or array key of each item in the collection equals the given value.
  146. *
  147. * This will always leave the original collection untouched and will return
  148. * a new one.
  149. *
  150. * @param string | null $propertyOrMethod The property, method, or array key
  151. * to evaluate. If `null`, the element itself is compared to $value.
  152. * @param mixed $value The value to match.
  153. *
  154. * @return CollectionInterface<T>
  155. *
  156. * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist
  157. * on the elements in this collection.
  158. * @throws UnsupportedOperationException if unable to call where() on this
  159. * collection.
  160. */
  161. public function where(?string $propertyOrMethod, mixed $value): self;
  162. /**
  163. * Apply a given callback method on each item of the collection.
  164. *
  165. * This will always leave the original collection untouched. The new
  166. * collection is created by mapping the callback to each item of the
  167. * original collection.
  168. *
  169. * See the {@link http://php.net/manual/en/function.array-map.php PHP array_map() documentation}
  170. * for examples of how the `$callback` parameter works.
  171. *
  172. * @param callable(T): TCallbackReturn $callback A callable to apply to each
  173. * item of the collection.
  174. *
  175. * @return CollectionInterface<TCallbackReturn>
  176. *
  177. * @template TCallbackReturn
  178. */
  179. public function map(callable $callback): self;
  180. /**
  181. * Apply a given callback method on each item of the collection
  182. * to reduce it to a single value.
  183. *
  184. * See the {@link http://php.net/manual/en/function.array-reduce.php PHP array_reduce() documentation}
  185. * for examples of how the `$callback` and `$initial` parameters work.
  186. *
  187. * @param callable(TCarry, T): TCarry $callback A callable to apply to each
  188. * item of the collection to reduce it to a single value.
  189. * @param TCarry $initial This is the initial value provided to the callback.
  190. *
  191. * @return TCarry
  192. *
  193. * @template TCarry
  194. */
  195. public function reduce(callable $callback, mixed $initial): mixed;
  196. /**
  197. * Create a new collection with divergent items between current and given
  198. * collection.
  199. *
  200. * @param CollectionInterface<T> $other The collection to check for divergent
  201. * items.
  202. *
  203. * @return CollectionInterface<T>
  204. *
  205. * @throws CollectionMismatchException if the compared collections are of
  206. * differing types.
  207. */
  208. public function diff(CollectionInterface $other): self;
  209. /**
  210. * Create a new collection with intersecting item between current and given
  211. * collection.
  212. *
  213. * @param CollectionInterface<T> $other The collection to check for
  214. * intersecting items.
  215. *
  216. * @return CollectionInterface<T>
  217. *
  218. * @throws CollectionMismatchException if the compared collections are of
  219. * differing types.
  220. */
  221. public function intersect(CollectionInterface $other): self;
  222. /**
  223. * Merge current items and items of given collections into a new one.
  224. *
  225. * @param CollectionInterface<T> ...$collections The collections to merge.
  226. *
  227. * @return CollectionInterface<T>
  228. *
  229. * @throws CollectionMismatchException if unable to merge any of the given
  230. * collections or items within the given collections due to type
  231. * mismatch errors.
  232. */
  233. public function merge(CollectionInterface ...$collections): self;
  234. }