Collection.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use ArrayAccess;
  4. use Closure;
  5. use Countable;
  6. use IteratorAggregate;
  7. /**
  8. * The missing (SPL) Collection/Array/OrderedMap interface.
  9. *
  10. * A Collection resembles the nature of a regular PHP array. That is,
  11. * it is essentially an <b>ordered map</b> that can also be used
  12. * like a list.
  13. *
  14. * A Collection has an internal iterator just like a PHP array. In addition,
  15. * a Collection can be iterated with external iterators, which is preferable.
  16. * To use an external iterator simply use the foreach language construct to
  17. * iterate over the collection (which calls {@link getIterator()} internally) or
  18. * explicitly retrieve an iterator though {@link getIterator()} which can then be
  19. * used to iterate over the collection.
  20. * You can not rely on the internal iterator of the collection being at a certain
  21. * position unless you explicitly positioned it before. Prefer iteration with
  22. * external iterators.
  23. *
  24. * @phpstan-template TKey
  25. * @psalm-template TKey of array-key
  26. * @psalm-template T
  27. * @template-extends IteratorAggregate<TKey, T>
  28. * @template-extends ArrayAccess<TKey|null, T>
  29. */
  30. interface Collection extends Countable, IteratorAggregate, ArrayAccess
  31. {
  32. /**
  33. * Adds an element at the end of the collection.
  34. *
  35. * @param mixed $element The element to add.
  36. *
  37. * @return true Always TRUE.
  38. *
  39. * @psalm-param T $element
  40. */
  41. public function add($element);
  42. /**
  43. * Clears the collection, removing all elements.
  44. *
  45. * @return void
  46. */
  47. public function clear();
  48. /**
  49. * Checks whether an element is contained in the collection.
  50. * This is an O(n) operation, where n is the size of the collection.
  51. *
  52. * @param mixed $element The element to search for.
  53. *
  54. * @return bool TRUE if the collection contains the element, FALSE otherwise.
  55. *
  56. * @psalm-param T $element
  57. */
  58. public function contains($element);
  59. /**
  60. * Checks whether the collection is empty (contains no elements).
  61. *
  62. * @return bool TRUE if the collection is empty, FALSE otherwise.
  63. */
  64. public function isEmpty();
  65. /**
  66. * Removes the element at the specified index from the collection.
  67. *
  68. * @param string|int $key The key/index of the element to remove.
  69. *
  70. * @return mixed The removed element or NULL, if the collection did not contain the element.
  71. *
  72. * @psalm-param TKey $key
  73. * @psalm-return T|null
  74. */
  75. public function remove($key);
  76. /**
  77. * Removes the specified element from the collection, if it is found.
  78. *
  79. * @param mixed $element The element to remove.
  80. *
  81. * @return bool TRUE if this collection contained the specified element, FALSE otherwise.
  82. *
  83. * @psalm-param T $element
  84. */
  85. public function removeElement($element);
  86. /**
  87. * Checks whether the collection contains an element with the specified key/index.
  88. *
  89. * @param string|int $key The key/index to check for.
  90. *
  91. * @return bool TRUE if the collection contains an element with the specified key/index,
  92. * FALSE otherwise.
  93. *
  94. * @psalm-param TKey $key
  95. */
  96. public function containsKey($key);
  97. /**
  98. * Gets the element at the specified key/index.
  99. *
  100. * @param string|int $key The key/index of the element to retrieve.
  101. *
  102. * @return mixed
  103. *
  104. * @psalm-param TKey $key
  105. * @psalm-return T|null
  106. */
  107. public function get($key);
  108. /**
  109. * Gets all keys/indices of the collection.
  110. *
  111. * @return int[]|string[] The keys/indices of the collection, in the order of the corresponding
  112. * elements in the collection.
  113. *
  114. * @psalm-return TKey[]
  115. */
  116. public function getKeys();
  117. /**
  118. * Gets all values of the collection.
  119. *
  120. * @return array The values of all elements in the collection, in the order they
  121. * appear in the collection.
  122. *
  123. * @psalm-return T[]
  124. */
  125. public function getValues();
  126. /**
  127. * Sets an element in the collection at the specified key/index.
  128. *
  129. * @param string|int $key The key/index of the element to set.
  130. * @param mixed $value The element to set.
  131. *
  132. * @return void
  133. *
  134. * @psalm-param TKey $key
  135. * @psalm-param T $value
  136. */
  137. public function set($key, $value);
  138. /**
  139. * Gets a native PHP array representation of the collection.
  140. *
  141. * @return array
  142. *
  143. * @psalm-return array<TKey,T>
  144. */
  145. public function toArray();
  146. /**
  147. * Sets the internal iterator to the first element in the collection and returns this element.
  148. *
  149. * @return mixed
  150. *
  151. * @psalm-return T|false
  152. */
  153. public function first();
  154. /**
  155. * Sets the internal iterator to the last element in the collection and returns this element.
  156. *
  157. * @return mixed
  158. *
  159. * @psalm-return T|false
  160. */
  161. public function last();
  162. /**
  163. * Gets the key/index of the element at the current iterator position.
  164. *
  165. * @return int|string|null
  166. *
  167. * @psalm-return TKey|null
  168. */
  169. public function key();
  170. /**
  171. * Gets the element of the collection at the current iterator position.
  172. *
  173. * @return mixed
  174. *
  175. * @psalm-return T|false
  176. */
  177. public function current();
  178. /**
  179. * Moves the internal iterator position to the next element and returns this element.
  180. *
  181. * @return mixed
  182. *
  183. * @psalm-return T|false
  184. */
  185. public function next();
  186. /**
  187. * Tests for the existence of an element that satisfies the given predicate.
  188. *
  189. * @param Closure $p The predicate.
  190. *
  191. * @return bool TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  192. *
  193. * @psalm-param Closure(TKey=, T=):bool $p
  194. */
  195. public function exists(Closure $p);
  196. /**
  197. * Returns all the elements of this collection that satisfy the predicate p.
  198. * The order of the elements is preserved.
  199. *
  200. * @param Closure $p The predicate used for filtering.
  201. *
  202. * @return Collection A collection with the results of the filter operation.
  203. *
  204. * @psalm-param Closure(T=):bool $p
  205. * @psalm-return Collection<TKey, T>
  206. */
  207. public function filter(Closure $p);
  208. /**
  209. * Tests whether the given predicate p holds for all elements of this collection.
  210. *
  211. * @param Closure $p The predicate.
  212. *
  213. * @return bool TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  214. *
  215. * @psalm-param Closure(TKey=, T=):bool $p
  216. */
  217. public function forAll(Closure $p);
  218. /**
  219. * Applies the given function to each element in the collection and returns
  220. * a new collection with the elements returned by the function.
  221. *
  222. * @return Collection
  223. *
  224. * @psalm-template U
  225. * @psalm-param Closure(T=):U $func
  226. * @psalm-return Collection<TKey, U>
  227. */
  228. public function map(Closure $func);
  229. /**
  230. * Partitions this collection in two collections according to a predicate.
  231. * Keys are preserved in the resulting collections.
  232. *
  233. * @param Closure $p The predicate on which to partition.
  234. *
  235. * @return Collection[] An array with two elements. The first element contains the collection
  236. * of elements where the predicate returned TRUE, the second element
  237. * contains the collection of elements where the predicate returned FALSE.
  238. *
  239. * @psalm-param Closure(TKey=, T=):bool $p
  240. * @psalm-return array{0: Collection<TKey, T>, 1: Collection<TKey, T>}
  241. */
  242. public function partition(Closure $p);
  243. /**
  244. * Gets the index/key of a given element. The comparison of two elements is strict,
  245. * that means not only the value but also the type must match.
  246. * For objects this means reference equality.
  247. *
  248. * @param mixed $element The element to search for.
  249. *
  250. * @return int|string|bool The key/index of the element or FALSE if the element was not found.
  251. *
  252. * @psalm-param T $element
  253. * @psalm-return TKey|false
  254. */
  255. public function indexOf($element);
  256. /**
  257. * Extracts a slice of $length elements starting at position $offset from the Collection.
  258. *
  259. * If $length is null it returns all elements from $offset to the end of the Collection.
  260. * Keys have to be preserved by this method. Calling this method will only return the
  261. * selected slice and NOT change the elements contained in the collection slice is called on.
  262. *
  263. * @param int $offset The offset to start from.
  264. * @param int|null $length The maximum number of elements to return, or null for no limit.
  265. *
  266. * @return array
  267. *
  268. * @psalm-return array<TKey,T>
  269. */
  270. public function slice($offset, $length = null);
  271. }