Collection.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Collections;
  20. use ArrayAccess;
  21. use Closure;
  22. use Countable;
  23. use IteratorAggregate;
  24. /**
  25. * The missing (SPL) Collection/Array/OrderedMap interface.
  26. *
  27. * A Collection resembles the nature of a regular PHP array. That is,
  28. * it is essentially an <b>ordered map</b> that can also be used
  29. * like a list.
  30. *
  31. * A Collection has an internal iterator just like a PHP array. In addition,
  32. * a Collection can be iterated with external iterators, which is preferable.
  33. * To use an external iterator simply use the foreach language construct to
  34. * iterate over the collection (which calls {@link getIterator()} internally) or
  35. * explicitly retrieve an iterator though {@link getIterator()} which can then be
  36. * used to iterate over the collection.
  37. * You can not rely on the internal iterator of the collection being at a certain
  38. * position unless you explicitly positioned it before. Prefer iteration with
  39. * external iterators.
  40. *
  41. * @since 2.0
  42. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  43. * @author Jonathan Wage <jonwage@gmail.com>
  44. * @author Roman Borschel <roman@code-factory.org>
  45. */
  46. interface Collection extends Countable, IteratorAggregate, ArrayAccess
  47. {
  48. /**
  49. * Adds an element at the end of the collection.
  50. *
  51. * @param mixed $element The element to add.
  52. *
  53. * @return boolean Always TRUE.
  54. */
  55. public function add($element);
  56. /**
  57. * Clears the collection, removing all elements.
  58. *
  59. * @return void
  60. */
  61. public function clear();
  62. /**
  63. * Checks whether an element is contained in the collection.
  64. * This is an O(n) operation, where n is the size of the collection.
  65. *
  66. * @param mixed $element The element to search for.
  67. *
  68. * @return boolean TRUE if the collection contains the element, FALSE otherwise.
  69. */
  70. public function contains($element);
  71. /**
  72. * Checks whether the collection is empty (contains no elements).
  73. *
  74. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  75. */
  76. public function isEmpty();
  77. /**
  78. * Removes the element at the specified index from the collection.
  79. *
  80. * @param string|integer $key The kex/index of the element to remove.
  81. *
  82. * @return mixed The removed element or NULL, if the collection did not contain the element.
  83. */
  84. public function remove($key);
  85. /**
  86. * Removes the specified element from the collection, if it is found.
  87. *
  88. * @param mixed $element The element to remove.
  89. *
  90. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  91. */
  92. public function removeElement($element);
  93. /**
  94. * Checks whether the collection contains an element with the specified key/index.
  95. *
  96. * @param string|integer $key The key/index to check for.
  97. *
  98. * @return boolean TRUE if the collection contains an element with the specified key/index,
  99. * FALSE otherwise.
  100. */
  101. public function containsKey($key);
  102. /**
  103. * Gets the element at the specified key/index.
  104. *
  105. * @param string|integer $key The key/index of the element to retrieve.
  106. *
  107. * @return mixed
  108. */
  109. public function get($key);
  110. /**
  111. * Gets all keys/indices of the collection.
  112. *
  113. * @return array The keys/indices of the collection, in the order of the corresponding
  114. * elements in the collection.
  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. public function getValues();
  124. /**
  125. * Sets an element in the collection at the specified key/index.
  126. *
  127. * @param string|integer $key The key/index of the element to set.
  128. * @param mixed $value The element to set.
  129. *
  130. * @return void
  131. */
  132. public function set($key, $value);
  133. /**
  134. * Gets a native PHP array representation of the collection.
  135. *
  136. * @return array
  137. */
  138. public function toArray();
  139. /**
  140. * Sets the internal iterator to the first element in the collection and returns this element.
  141. *
  142. * @return mixed
  143. */
  144. public function first();
  145. /**
  146. * Sets the internal iterator to the last element in the collection and returns this element.
  147. *
  148. * @return mixed
  149. */
  150. public function last();
  151. /**
  152. * Gets the key/index of the element at the current iterator position.
  153. *
  154. * @return int|string
  155. */
  156. public function key();
  157. /**
  158. * Gets the element of the collection at the current iterator position.
  159. *
  160. * @return mixed
  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. */
  168. public function next();
  169. /**
  170. * Tests for the existence of an element that satisfies the given predicate.
  171. *
  172. * @param Closure $p The predicate.
  173. *
  174. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  175. */
  176. public function exists(Closure $p);
  177. /**
  178. * Returns all the elements of this collection that satisfy the predicate p.
  179. * The order of the elements is preserved.
  180. *
  181. * @param Closure $p The predicate used for filtering.
  182. *
  183. * @return Collection A collection with the results of the filter operation.
  184. */
  185. public function filter(Closure $p);
  186. /**
  187. * Tests whether the given predicate p holds for all elements of this collection.
  188. *
  189. * @param Closure $p The predicate.
  190. *
  191. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  192. */
  193. public function forAll(Closure $p);
  194. /**
  195. * Applies the given function to each element in the collection and returns
  196. * a new collection with the elements returned by the function.
  197. *
  198. * @param Closure $func
  199. *
  200. * @return Collection
  201. */
  202. public function map(Closure $func);
  203. /**
  204. * Partitions this collection in two collections according to a predicate.
  205. * Keys are preserved in the resulting collections.
  206. *
  207. * @param Closure $p The predicate on which to partition.
  208. *
  209. * @return array An array with two elements. The first element contains the collection
  210. * of elements where the predicate returned TRUE, the second element
  211. * contains the collection of elements where the predicate returned FALSE.
  212. */
  213. public function partition(Closure $p);
  214. /**
  215. * Gets the index/key of a given element. The comparison of two elements is strict,
  216. * that means not only the value but also the type must match.
  217. * For objects this means reference equality.
  218. *
  219. * @param mixed $element The element to search for.
  220. *
  221. * @return int|string|bool The key/index of the element or FALSE if the element was not found.
  222. */
  223. public function indexOf($element);
  224. /**
  225. * Extracts a slice of $length elements starting at position $offset from the Collection.
  226. *
  227. * If $length is null it returns all elements from $offset to the end of the Collection.
  228. * Keys have to be preserved by this method. Calling this method will only return the
  229. * selected slice and NOT change the elements contained in the collection slice is called on.
  230. *
  231. * @param int $offset The offset to start from.
  232. * @param int|null $length The maximum number of elements to return, or null for no limit.
  233. *
  234. * @return array
  235. */
  236. public function slice($offset, $length = null);
  237. }