Collection.php 8.7 KB

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