2009-07-29 16:00:08 +04:00
|
|
|
<?php
|
2009-08-11 01:36:57 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-07-29 16:00:08 +04:00
|
|
|
|
|
|
|
namespace Doctrine\Common\Collections;
|
|
|
|
|
2010-03-29 17:50:57 +04:00
|
|
|
use Closure, Countable, IteratorAggregate, ArrayAccess;
|
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2010-01-22 18:10:13 +03:00
|
|
|
* The missing (SPL) Collection/Array/OrderedMap interface.
|
2009-08-03 17:25:56 +04:00
|
|
|
*
|
2009-07-29 16:00:08 +04:00
|
|
|
* A Collection resembles the nature of a regular PHP array. That is,
|
2010-03-29 17:50:57 +04:00
|
|
|
* it is essentially an <b>ordered map</b> that can also be used
|
2009-07-29 16:00:08 +04:00
|
|
|
* like a list.
|
|
|
|
*
|
2010-03-29 17:50:57 +04:00
|
|
|
* A Collection has an internal iterator just like a PHP array. In addition,
|
2009-08-03 17:25:56 +04:00
|
|
|
* a Collection can be iterated with external iterators, which is preferrable.
|
|
|
|
* To use an external iterator simply use the foreach language construct to
|
2010-03-29 17:50:57 +04:00
|
|
|
* iterate over the collection (which calls {@link getIterator()} internally) or
|
|
|
|
* explicitly retrieve an iterator though {@link getIterator()} which can then be
|
2009-08-03 17:25:56 +04:00
|
|
|
* used to iterate over the collection.
|
|
|
|
* You can not rely on the internal iterator of the collection being at a certain
|
|
|
|
* position unless you explicitly positioned it before. Prefer iteration with
|
|
|
|
* external iterators.
|
|
|
|
*
|
2009-08-11 01:36:57 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-07-29 16:00:08 +04:00
|
|
|
*/
|
2010-03-29 17:50:57 +04:00
|
|
|
interface Collection extends Countable, IteratorAggregate, ArrayAccess
|
2009-07-29 16:00:08 +04:00
|
|
|
{
|
|
|
|
/**
|
2010-03-29 17:50:57 +04:00
|
|
|
* Adds an element at the end of the collection.
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
|
|
|
* @param mixed $element The element to add.
|
|
|
|
* @return boolean Always TRUE.
|
|
|
|
*/
|
|
|
|
function add($element);
|
|
|
|
|
|
|
|
/**
|
2010-01-22 18:10:13 +03:00
|
|
|
* Clears the collection, removing all elements.
|
2009-07-29 16:00:08 +04:00
|
|
|
*/
|
|
|
|
function clear();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Checks whether an element is contained in the collection.
|
2010-03-29 17:50:57 +04:00
|
|
|
* This is an O(n) operation, where n is the size of the collection.
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
2010-03-29 17:50:57 +04:00
|
|
|
* @param mixed $element The element to search for.
|
2009-07-29 16:00:08 +04:00
|
|
|
* @return boolean TRUE if the collection contains the element, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function contains($element);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2010-03-29 17:50:57 +04:00
|
|
|
* Checks whether the collection is empty (contains no elements).
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
|
|
|
* @return boolean TRUE if the collection is empty, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function isEmpty();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2010-03-29 17:50:57 +04:00
|
|
|
* Removes the element at the specified index from the collection.
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
2010-03-29 17:50:57 +04:00
|
|
|
* @param string|integer $key The kex/index of the element to remove.
|
2009-07-29 16:00:08 +04:00
|
|
|
* @return mixed The removed element or NULL, if the collection did not contain the element.
|
|
|
|
*/
|
|
|
|
function remove($key);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Removes an element from the collection.
|
|
|
|
*
|
|
|
|
* @param mixed $element The element to remove.
|
|
|
|
* @return mixed The removed element or NULL, if the collection did not contain the element.
|
|
|
|
*/
|
|
|
|
function removeElement($element);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Checks whether the collection contains an element with the specified key/index.
|
|
|
|
*
|
|
|
|
* @param string|integer $key The key/index to check for.
|
|
|
|
* @return boolean TRUE if the collection contains an element with the specified key/index,
|
|
|
|
* FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function containsKey($key);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2010-03-29 17:50:57 +04:00
|
|
|
* Gets the element at the specified key/index.
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
|
|
|
* @param string|integer $key The key/index of the element to retrieve.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function get($key);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Gets all keys/indices of the collection.
|
|
|
|
*
|
|
|
|
* @return array The keys/indices of the collection, in the order of the corresponding
|
|
|
|
* elements in the collection.
|
|
|
|
*/
|
|
|
|
function getKeys();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Gets all values of the collection.
|
|
|
|
*
|
|
|
|
* @return array The values of all elements in the collection, in the order they
|
|
|
|
* appear in the collection.
|
|
|
|
*/
|
|
|
|
function getValues();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
|
|
|
* Sets an element in the collection at the specified key/index.
|
|
|
|
*
|
|
|
|
* @param string|integer $key The key/index of the element to set.
|
|
|
|
* @param mixed $value The element to set.
|
|
|
|
*/
|
|
|
|
function set($key, $value);
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2010-03-29 17:50:57 +04:00
|
|
|
* Gets a native PHP array representation of the collection.
|
2009-07-29 16:00:08 +04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function toArray();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2009-08-03 17:25:56 +04:00
|
|
|
* Sets the internal iterator to the first element in the collection and
|
|
|
|
* returns this element.
|
|
|
|
*
|
2009-07-29 16:00:08 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function first();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-07-29 16:00:08 +04:00
|
|
|
/**
|
2009-08-03 17:25:56 +04:00
|
|
|
* Sets the internal iterator to the last element in the collection and
|
|
|
|
* returns this element.
|
|
|
|
*
|
2009-07-29 16:00:08 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2009-08-03 17:25:56 +04:00
|
|
|
function last();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
/**
|
|
|
|
* Gets the key/index of the element at the current iterator position.
|
2009-08-11 01:36:57 +04:00
|
|
|
*
|
2009-08-03 17:25:56 +04:00
|
|
|
*/
|
|
|
|
function key();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
/**
|
|
|
|
* Gets the element of the collection at the current iterator position.
|
2009-08-11 01:36:57 +04:00
|
|
|
*
|
2009-08-03 17:25:56 +04:00
|
|
|
*/
|
|
|
|
function current();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
/**
|
|
|
|
* Moves the internal iterator position to the next element.
|
2009-08-11 01:36:57 +04:00
|
|
|
*
|
2009-08-03 17:25:56 +04:00
|
|
|
*/
|
|
|
|
function next();
|
2010-03-29 17:50:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the existence of an element that satisfies the given predicate.
|
|
|
|
*
|
|
|
|
* @param Closure $p The predicate.
|
|
|
|
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function exists(Closure $p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the elements of this collection that satisfy the predicate p.
|
|
|
|
* The order of the elements is preserved.
|
|
|
|
*
|
|
|
|
* @param Closure $p The predicate used for filtering.
|
|
|
|
* @return Collection A collection with the results of the filter operation.
|
|
|
|
*/
|
|
|
|
function filter(Closure $p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the given predicate p to all elements of this collection,
|
|
|
|
* returning true, if the predicate yields true for all elements.
|
|
|
|
*
|
|
|
|
* @param Closure $p The predicate.
|
|
|
|
* @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function forAll(Closure $p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the given function to each element in the collection and returns
|
|
|
|
* a new collection with the elements returned by the function.
|
|
|
|
*
|
|
|
|
* @param Closure $func
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
function map(Closure $func);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partitions this collection in two collections according to a predicate.
|
|
|
|
* Keys are preserved in the resulting collections.
|
|
|
|
*
|
|
|
|
* @param Closure $p The predicate on which to partition.
|
|
|
|
* @return array An array with two elements. The first element contains the collection
|
|
|
|
* of elements where the predicate returned TRUE, the second element
|
|
|
|
* contains the collection of elements where the predicate returned FALSE.
|
|
|
|
*/
|
|
|
|
function partition(Closure $p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index/key of a given element. The comparison of two elements is strict,
|
|
|
|
* that means not only the value but also the type must match.
|
|
|
|
* For objects this means reference equality.
|
|
|
|
*
|
|
|
|
* @param mixed $element The element to search for.
|
|
|
|
* @return mixed The key/index of the element or FALSE if the element was not found.
|
|
|
|
*/
|
|
|
|
function indexOf($element);
|
2009-07-29 16:00:08 +04:00
|
|
|
}
|