Collection.php 8.4 KB

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