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;
22 use Closure, Countable, IteratorAggregate, ArrayAccess;
25 * The missing (SPL) Collection/Array/OrderedMap interface.
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
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 preferrable.
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
42 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
43 * @author Jonathan Wage <jonwage@gmail.com>
44 * @author Roman Borschel <roman@code-factory.org>
46 interface Collection extends Countable, IteratorAggregate, ArrayAccess
49 * Adds an element at the end of the collection.
51 * @param mixed $element The element to add.
52 * @return boolean Always TRUE.
54 function add($element);
57 * Clears the collection, removing all elements.
62 * Checks whether an element is contained in the collection.
63 * This is an O(n) operation, where n is the size of the collection.
65 * @param mixed $element The element to search for.
66 * @return boolean TRUE if the collection contains the element, FALSE otherwise.
68 function contains($element);
71 * Checks whether the collection is empty (contains no elements).
73 * @return boolean TRUE if the collection is empty, FALSE otherwise.
78 * Removes the element at the specified index from the collection.
80 * @param string|integer $key The kex/index of the element to remove.
81 * @return mixed The removed element or NULL, if the collection did not contain the element.
83 function remove($key);
86 * Removes the specified element from the collection, if it is found.
88 * @param mixed $element The element to remove.
89 * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
91 function removeElement($element);
94 * Checks whether the collection contains an element with the specified key/index.
96 * @param string|integer $key The key/index to check for.
97 * @return boolean TRUE if the collection contains an element with the specified key/index,
100 function containsKey($key);
103 * Gets the element at the specified key/index.
105 * @param string|integer $key The key/index of the element to retrieve.
111 * Gets all keys/indices of the collection.
113 * @return array The keys/indices of the collection, in the order of the corresponding
114 * elements in the collection.
119 * Gets all values of the collection.
121 * @return array The values of all elements in the collection, in the order they
122 * appear in the collection.
124 function getValues();
127 * Sets an element in the collection at the specified key/index.
129 * @param string|integer $key The key/index of the element to set.
130 * @param mixed $value The element to set.
132 function set($key, $value);
135 * Gets a native PHP array representation of the collection.
142 * Sets the internal iterator to the first element in the collection and
143 * returns this element.
150 * Sets the internal iterator to the last element in the collection and
151 * returns this element.
158 * Gets the key/index of the element at the current iterator position.
164 * Gets the element of the collection at the current iterator position.
170 * Moves the internal iterator position to the next element.
176 * Tests for the existence of an element that satisfies the given predicate.
178 * @param Closure $p The predicate.
179 * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
181 function exists(Closure $p);
184 * Returns all the elements of this collection that satisfy the predicate p.
185 * The order of the elements is preserved.
187 * @param Closure $p The predicate used for filtering.
188 * @return Collection A collection with the results of the filter operation.
190 function filter(Closure $p);
193 * Applies the given predicate p to all elements of this collection,
194 * returning true, if the predicate yields true for all elements.
196 * @param Closure $p The predicate.
197 * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
199 function forAll(Closure $p);
202 * Applies the given function to each element in the collection and returns
203 * a new collection with the elements returned by the function.
205 * @param Closure $func
208 function map(Closure $func);
211 * Partitions this collection in two collections according to a predicate.
212 * Keys are preserved in the resulting collections.
214 * @param Closure $p The predicate on which to partition.
215 * @return array An array with two elements. The first element contains the collection
216 * of elements where the predicate returned TRUE, the second element
217 * contains the collection of elements where the predicate returned FALSE.
219 function partition(Closure $p);
222 * Gets the index/key of a given element. The comparison of two elements is strict,
223 * that means not only the value but also the type must match.
224 * For objects this means reference equality.
226 * @param mixed $element The element to search for.
227 * @return mixed The key/index of the element or FALSE if the element was not found.
229 function indexOf($element);
232 * Extract a slice of $length elements starting at position $offset from the Collection.
234 * If $length is null it returns all elements from $offset to the end of the Collection.
235 * Keys have to be preserved by this method. Calling this method will only return the
236 * selected slice and NOT change the elements contained in the collection slice is called on.
242 function slice($offset, $length = null);