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.
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>.
20 namespace Doctrine\Common\Collections;
25 use IteratorAggregate;
28 * The missing (SPL) Collection/Array/OrderedMap interface.
30 * A Collection resembles the nature of a regular PHP array. That is,
31 * it is essentially an <b>ordered map</b> that can also be used
34 * A Collection has an internal iterator just like a PHP array. In addition,
35 * a Collection can be iterated with external iterators, which is preferable.
36 * To use an external iterator simply use the foreach language construct to
37 * iterate over the collection (which calls {@link getIterator()} internally) or
38 * explicitly retrieve an iterator though {@link getIterator()} which can then be
39 * used to iterate over the collection.
40 * You can not rely on the internal iterator of the collection being at a certain
41 * position unless you explicitly positioned it before. Prefer iteration with
45 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
46 * @author Jonathan Wage <jonwage@gmail.com>
47 * @author Roman Borschel <roman@code-factory.org>
49 interface Collection extends Countable, IteratorAggregate, ArrayAccess
52 * Adds an element at the end of the collection.
54 * @param mixed $element The element to add.
56 * @return boolean Always TRUE.
58 public function add($element);
61 * Clears the collection, removing all elements.
65 public function clear();
68 * Checks whether an element is contained in the collection.
69 * This is an O(n) operation, where n is the size of the collection.
71 * @param mixed $element The element to search for.
73 * @return boolean TRUE if the collection contains the element, FALSE otherwise.
75 public function contains($element);
78 * Checks whether the collection is empty (contains no elements).
80 * @return boolean TRUE if the collection is empty, FALSE otherwise.
82 public function isEmpty();
85 * Removes the element at the specified index from the collection.
87 * @param string|integer $key The kex/index of the element to remove.
89 * @return mixed The removed element or NULL, if the collection did not contain the element.
91 public function remove($key);
94 * Removes the specified element from the collection, if it is found.
96 * @param mixed $element The element to remove.
98 * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
100 public function removeElement($element);
103 * Checks whether the collection contains an element with the specified key/index.
105 * @param string|integer $key The key/index to check for.
107 * @return boolean TRUE if the collection contains an element with the specified key/index,
110 public function containsKey($key);
113 * Gets the element at the specified key/index.
115 * @param string|integer $key The key/index of the element to retrieve.
119 public function get($key);
122 * Gets all keys/indices of the collection.
124 * @return array The keys/indices of the collection, in the order of the corresponding
125 * elements in the collection.
127 public function getKeys();
130 * Gets all values of the collection.
132 * @return array The values of all elements in the collection, in the order they
133 * appear in the collection.
135 public function getValues();
138 * Sets an element in the collection at the specified key/index.
140 * @param string|integer $key The key/index of the element to set.
141 * @param mixed $value The element to set.
145 public function set($key, $value);
148 * Gets a native PHP array representation of the collection.
152 public function toArray();
155 * Sets the internal iterator to the first element in the collection and returns this element.
159 public function first();
162 * Sets the internal iterator to the last element in the collection and returns this element.
166 public function last();
169 * Gets the key/index of the element at the current iterator position.
173 public function key();
176 * Gets the element of the collection at the current iterator position.
180 public function current();
183 * Moves the internal iterator position to the next element and returns this element.
187 public function next();
190 * Tests for the existence of an element that satisfies the given predicate.
192 * @param Closure $p The predicate.
194 * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
196 public function exists(Closure $p);
199 * Returns all the elements of this collection that satisfy the predicate p.
200 * The order of the elements is preserved.
202 * @param Closure $p The predicate used for filtering.
204 * @return Collection A collection with the results of the filter operation.
206 public function filter(Closure $p);
209 * Tests whether the given predicate p holds for all elements of this collection.
211 * @param Closure $p The predicate.
213 * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
215 public function forAll(Closure $p);
218 * Applies the given function to each element in the collection and returns
219 * a new collection with the elements returned by the function.
221 * @param Closure $func
225 public function map(Closure $func);
228 * Partitions this collection in two collections according to a predicate.
229 * Keys are preserved in the resulting collections.
231 * @param Closure $p The predicate on which to partition.
233 * @return array An array with two elements. The first element contains the collection
234 * of elements where the predicate returned TRUE, the second element
235 * contains the collection of elements where the predicate returned FALSE.
237 public function partition(Closure $p);
240 * Gets the index/key of a given element. The comparison of two elements is strict,
241 * that means not only the value but also the type must match.
242 * For objects this means reference equality.
244 * @param mixed $element The element to search for.
246 * @return int|string|bool The key/index of the element or FALSE if the element was not found.
248 public function indexOf($element);
251 * Extracts a slice of $length elements starting at position $offset from the Collection.
253 * If $length is null it returns all elements from $offset to the end of the Collection.
254 * Keys have to be preserved by this method. Calling this method will only return the
255 * selected slice and NOT change the elements contained in the collection slice is called on.
257 * @param int $offset The offset to start from.
258 * @param int|null $length The maximum number of elements to return, or null for no limit.
262 public function slice($offset, $length = null);