ReadableCollection.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use Closure;
  4. use Countable;
  5. use IteratorAggregate;
  6. /**
  7. * @psalm-template TKey of array-key
  8. * @template-covariant T
  9. * @template-extends IteratorAggregate<TKey, T>
  10. */
  11. interface ReadableCollection extends Countable, IteratorAggregate
  12. {
  13. /**
  14. * Checks whether an element is contained in the collection.
  15. * This is an O(n) operation, where n is the size of the collection.
  16. *
  17. * @param mixed $element The element to search for.
  18. * @psalm-param TMaybeContained $element
  19. *
  20. * @return bool TRUE if the collection contains the element, FALSE otherwise.
  21. * @psalm-return (TMaybeContained is T ? bool : false)
  22. *
  23. * @template TMaybeContained
  24. */
  25. public function contains($element);
  26. /**
  27. * Checks whether the collection is empty (contains no elements).
  28. *
  29. * @return bool TRUE if the collection is empty, FALSE otherwise.
  30. */
  31. public function isEmpty();
  32. /**
  33. * Checks whether the collection contains an element with the specified key/index.
  34. *
  35. * @param string|int $key The key/index to check for.
  36. * @psalm-param TKey $key
  37. *
  38. * @return bool TRUE if the collection contains an element with the specified key/index,
  39. * FALSE otherwise.
  40. */
  41. public function containsKey($key);
  42. /**
  43. * Gets the element at the specified key/index.
  44. *
  45. * @param string|int $key The key/index of the element to retrieve.
  46. * @psalm-param TKey $key
  47. *
  48. * @return mixed
  49. * @psalm-return T|null
  50. */
  51. public function get($key);
  52. /**
  53. * Gets all keys/indices of the collection.
  54. *
  55. * @return int[]|string[] The keys/indices of the collection, in the order of the corresponding
  56. * elements in the collection.
  57. * @psalm-return list<TKey>
  58. */
  59. public function getKeys();
  60. /**
  61. * Gets all values of the collection.
  62. *
  63. * @return mixed[] The values of all elements in the collection, in the
  64. * order they appear in the collection.
  65. * @psalm-return list<T>
  66. */
  67. public function getValues();
  68. /**
  69. * Gets a native PHP array representation of the collection.
  70. *
  71. * @return mixed[]
  72. * @psalm-return array<TKey,T>
  73. */
  74. public function toArray();
  75. /**
  76. * Sets the internal iterator to the first element in the collection and returns this element.
  77. *
  78. * @return mixed
  79. * @psalm-return T|false
  80. */
  81. public function first();
  82. /**
  83. * Sets the internal iterator to the last element in the collection and returns this element.
  84. *
  85. * @return mixed
  86. * @psalm-return T|false
  87. */
  88. public function last();
  89. /**
  90. * Gets the key/index of the element at the current iterator position.
  91. *
  92. * @return int|string|null
  93. * @psalm-return TKey|null
  94. */
  95. public function key();
  96. /**
  97. * Gets the element of the collection at the current iterator position.
  98. *
  99. * @return mixed
  100. * @psalm-return T|false
  101. */
  102. public function current();
  103. /**
  104. * Moves the internal iterator position to the next element and returns this element.
  105. *
  106. * @return mixed
  107. * @psalm-return T|false
  108. */
  109. public function next();
  110. /**
  111. * Extracts a slice of $length elements starting at position $offset from the Collection.
  112. *
  113. * If $length is null it returns all elements from $offset to the end of the Collection.
  114. * Keys have to be preserved by this method. Calling this method will only return the
  115. * selected slice and NOT change the elements contained in the collection slice is called on.
  116. *
  117. * @param int $offset The offset to start from.
  118. * @param int|null $length The maximum number of elements to return, or null for no limit.
  119. *
  120. * @return mixed[]
  121. * @psalm-return array<TKey,T>
  122. */
  123. public function slice($offset, $length = null);
  124. /**
  125. * Tests for the existence of an element that satisfies the given predicate.
  126. *
  127. * @param Closure $p The predicate.
  128. * @psalm-param Closure(TKey, T):bool $p
  129. *
  130. * @return bool TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  131. */
  132. public function exists(Closure $p);
  133. /**
  134. * Returns all the elements of this collection that satisfy the predicate p.
  135. * The order of the elements is preserved.
  136. *
  137. * @param Closure $p The predicate used for filtering.
  138. * @psalm-param Closure(T):bool $p
  139. *
  140. * @return ReadableCollection<mixed> A collection with the results of the filter operation.
  141. * @psalm-return ReadableCollection<TKey, T>
  142. */
  143. public function filter(Closure $p);
  144. /**
  145. * Applies the given function to each element in the collection and returns
  146. * a new collection with the elements returned by the function.
  147. *
  148. * @psalm-param Closure(T):U $func
  149. *
  150. * @return Collection<mixed>
  151. * @psalm-return Collection<TKey, U>
  152. *
  153. * @psalm-template U
  154. */
  155. public function map(Closure $func);
  156. /**
  157. * Partitions this collection in two collections according to a predicate.
  158. * Keys are preserved in the resulting collections.
  159. *
  160. * @param Closure $p The predicate on which to partition.
  161. * @psalm-param Closure(TKey, T):bool $p
  162. *
  163. * @return ReadableCollection<mixed>[] An array with two elements. The first element contains the collection
  164. * of elements where the predicate returned TRUE, the second element
  165. * contains the collection of elements where the predicate returned FALSE.
  166. * @psalm-return array{0: ReadableCollection<TKey, T>, 1: ReadableCollection<TKey, T>}
  167. */
  168. public function partition(Closure $p);
  169. /**
  170. * Tests whether the given predicate p holds for all elements of this collection.
  171. *
  172. * @param Closure $p The predicate.
  173. * @psalm-param Closure(TKey, T):bool $p
  174. *
  175. * @return bool TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  176. */
  177. public function forAll(Closure $p);
  178. /**
  179. * Gets the index/key of a given element. The comparison of two elements is strict,
  180. * that means not only the value but also the type must match.
  181. * For objects this means reference equality.
  182. *
  183. * @param mixed $element The element to search for.
  184. * @psalm-param TMaybeContained $element
  185. *
  186. * @return int|string|bool The key/index of the element or FALSE if the element was not found.
  187. * @psalm-return (TMaybeContained is T ? TKey|false : false)
  188. *
  189. * @template TMaybeContained
  190. */
  191. public function indexOf($element);
  192. }