Collection.php 4.31 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Common\Collections;

/**
 * The missing (SPL) Collection/Array interface.
7
 * 
8 9 10 11
 * A Collection resembles the nature of a regular PHP array. That is,
 * it is essentially an ordered map that can syntactically also be used
 * like a list.
 * 
12 13 14 15 16 17 18 19 20 21 22
 * A Collection has an internal iterator just like a PHP array. In addition
 * a Collection can be iterated with external iterators, which is preferrable.
 * To use an external iterator simply use the foreach language construct to
 * iterator over the collection (which canns getIterator() internally) or
 * explicitly retrieve an iterator though getIterator() which can then be
 * 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.
 * 
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess
{
    /**
     * Adds an element to the collection.
     *
     * @param mixed $element The element to add.
     * @return boolean Always TRUE.
     */
    function add($element);
    
    /**
     * Clears the collection.
     */
    function clear();
    
    /**
     * Checks whether an element is contained in the collection.
     * This is an O(n) operation.
     *
     * @param mixed $element The element to check for.
     * @return boolean TRUE if the collection contains the element, FALSE otherwise.
     */
    function contains($element);
    
    /**
     * Checks whether the collection is empty.
     *
     * @return boolean TRUE if the collection is empty, FALSE otherwise.
     */
    function isEmpty();
    
    /**
     * Removes the element with the specified key/index from the collection.
     * 
     * @param string|integer $key The key/index of the element to remove.
     * @return mixed The removed element or NULL, if the collection did not contain the element.
     */
    function remove($key);
    
    /**
     * 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);
    
    /**
     * 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);
    
    /**
     * Gets an element with a specified key / at a specified index.
     * 
     * @param string|integer $key The key/index of the element to retrieve.
     * @return mixed
     */
    function get($key);
    
    /**
     * 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();
    
    /**
     * 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();
    
    /**
     * 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);
    
    /**
     * Gets a plain PHP array representation of the collection.
     * 
     * @return array
     */
    function toArray();
    
    /**
122 123 124
     * Sets the internal iterator to the first element in the collection and
     * returns this element.
     *
125 126 127 128 129
     * @return mixed
     */
    function first();
    
    /**
130 131 132
     * Sets the internal iterator to the last element in the collection and
     * returns this element.
     *
133 134
     * @return mixed
     */
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    function last();
    
    /**
     * Gets the key/index of the element at the current iterator position.
     */
    function key();
    
    /**
     * Gets the element of the collection at the current iterator position.
     */
    function current();
    
    /**
     * Moves the internal iterator position to the next element.
     */
    function next();
151
}