Collection.php 8.6 KB

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