Collection.php 8.1KB

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