vendor/doctrine-common/lib/Doctrine/Common/Collections/ArrayCollection.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 
       
    20 namespace Doctrine\Common\Collections;
       
    21 
       
    22 use Closure, ArrayIterator;
       
    23 
       
    24 /**
       
    25  * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
       
    26  *
       
    27  * @since   2.0
       
    28  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
       
    29  * @author  Jonathan Wage <jonwage@gmail.com>
       
    30  * @author  Roman Borschel <roman@code-factory.org>
       
    31  */
       
    32 class ArrayCollection implements Collection
       
    33 {
       
    34     /**
       
    35      * An array containing the entries of this collection.
       
    36      *
       
    37      * @var array
       
    38      */
       
    39     private $_elements;
       
    40 
       
    41     /**
       
    42      * Initializes a new ArrayCollection.
       
    43      *
       
    44      * @param array $elements
       
    45      */
       
    46     public function __construct(array $elements = array())
       
    47     {
       
    48         $this->_elements = $elements;
       
    49     }
       
    50 
       
    51     /**
       
    52      * Gets the PHP array representation of this collection.
       
    53      *
       
    54      * @return array The PHP array representation of this collection.
       
    55      */
       
    56     public function toArray()
       
    57     {
       
    58         return $this->_elements;
       
    59     }
       
    60 
       
    61     /**
       
    62      * Sets the internal iterator to the first element in the collection and
       
    63      * returns this element.
       
    64      *
       
    65      * @return mixed
       
    66      */
       
    67     public function first()
       
    68     {
       
    69         return reset($this->_elements);
       
    70     }
       
    71 
       
    72     /**
       
    73      * Sets the internal iterator to the last element in the collection and
       
    74      * returns this element.
       
    75      *
       
    76      * @return mixed
       
    77      */
       
    78     public function last()
       
    79     {
       
    80         return end($this->_elements);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Gets the current key/index at the current internal iterator position.
       
    85      *
       
    86      * @return mixed
       
    87      */
       
    88     public function key()
       
    89     {
       
    90         return key($this->_elements);
       
    91     }
       
    92     
       
    93     /**
       
    94      * Moves the internal iterator position to the next element.
       
    95      *
       
    96      * @return mixed
       
    97      */
       
    98     public function next()
       
    99     {
       
   100         return next($this->_elements);
       
   101     }
       
   102     
       
   103     /**
       
   104      * Gets the element of the collection at the current internal iterator position.
       
   105      *
       
   106      * @return mixed
       
   107      */
       
   108     public function current()
       
   109     {
       
   110         return current($this->_elements);
       
   111     }
       
   112 
       
   113     /**
       
   114      * Removes an element with a specific key/index from the collection.
       
   115      *
       
   116      * @param mixed $key
       
   117      * @return mixed The removed element or NULL, if no element exists for the given key.
       
   118      */
       
   119     public function remove($key)
       
   120     {
       
   121         if (isset($this->_elements[$key])) {
       
   122             $removed = $this->_elements[$key];
       
   123             unset($this->_elements[$key]);
       
   124             
       
   125             return $removed;
       
   126         }
       
   127 
       
   128         return null;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Removes the specified element from the collection, if it is found.
       
   133      *
       
   134      * @param mixed $element The element to remove.
       
   135      * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
       
   136      */
       
   137     public function removeElement($element)
       
   138     {
       
   139         $key = array_search($element, $this->_elements, true);
       
   140         
       
   141         if ($key !== false) {
       
   142             unset($this->_elements[$key]);
       
   143             
       
   144             return true;
       
   145         }
       
   146         
       
   147         return false;
       
   148     }
       
   149 
       
   150     /**
       
   151      * ArrayAccess implementation of offsetExists()
       
   152      *
       
   153      * @see containsKey()
       
   154      */
       
   155     public function offsetExists($offset)
       
   156     {
       
   157         return $this->containsKey($offset);
       
   158     }
       
   159 
       
   160     /**
       
   161      * ArrayAccess implementation of offsetGet()
       
   162      *
       
   163      * @see get()
       
   164      */
       
   165     public function offsetGet($offset)
       
   166     {
       
   167         return $this->get($offset);
       
   168     }
       
   169 
       
   170     /**
       
   171      * ArrayAccess implementation of offsetGet()
       
   172      *
       
   173      * @see add()
       
   174      * @see set()
       
   175      */
       
   176     public function offsetSet($offset, $value)
       
   177     {
       
   178         if ( ! isset($offset)) {
       
   179             return $this->add($value);
       
   180         }
       
   181         return $this->set($offset, $value);
       
   182     }
       
   183 
       
   184     /**
       
   185      * ArrayAccess implementation of offsetUnset()
       
   186      *
       
   187      * @see remove()
       
   188      */
       
   189     public function offsetUnset($offset)
       
   190     {
       
   191         return $this->remove($offset);
       
   192     }
       
   193 
       
   194     /**
       
   195      * Checks whether the collection contains a specific key/index.
       
   196      *
       
   197      * @param mixed $key The key to check for.
       
   198      * @return boolean TRUE if the given key/index exists, FALSE otherwise.
       
   199      */
       
   200     public function containsKey($key)
       
   201     {
       
   202         return isset($this->_elements[$key]);
       
   203     }
       
   204 
       
   205     /**
       
   206      * Checks whether the given element is contained in the collection.
       
   207      * Only element values are compared, not keys. The comparison of two elements
       
   208      * is strict, that means not only the value but also the type must match.
       
   209      * For objects this means reference equality.
       
   210      *
       
   211      * @param mixed $element
       
   212      * @return boolean TRUE if the given element is contained in the collection,
       
   213      *          FALSE otherwise.
       
   214      */
       
   215     public function contains($element)
       
   216     {
       
   217         return in_array($element, $this->_elements, true);
       
   218     }
       
   219 
       
   220     /**
       
   221      * Tests for the existance of an element that satisfies the given predicate.
       
   222      *
       
   223      * @param Closure $p The predicate.
       
   224      * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
       
   225      */
       
   226     public function exists(Closure $p)
       
   227     {
       
   228         foreach ($this->_elements as $key => $element) {
       
   229             if ($p($key, $element)) {
       
   230                 return true;
       
   231             }
       
   232         }
       
   233         return false;
       
   234     }
       
   235 
       
   236     /**
       
   237      * Searches for a given element and, if found, returns the corresponding key/index
       
   238      * of that element. The comparison of two elements is strict, that means not
       
   239      * only the value but also the type must match.
       
   240      * For objects this means reference equality.
       
   241      *
       
   242      * @param mixed $element The element to search for.
       
   243      * @return mixed The key/index of the element or FALSE if the element was not found.
       
   244      */
       
   245     public function indexOf($element)
       
   246     {
       
   247         return array_search($element, $this->_elements, true);
       
   248     }
       
   249 
       
   250     /**
       
   251      * Gets the element with the given key/index.
       
   252      *
       
   253      * @param mixed $key The key.
       
   254      * @return mixed The element or NULL, if no element exists for the given key.
       
   255      */
       
   256     public function get($key)
       
   257     {
       
   258         if (isset($this->_elements[$key])) {
       
   259             return $this->_elements[$key];
       
   260         }
       
   261         return null;
       
   262     }
       
   263 
       
   264     /**
       
   265      * Gets all keys/indexes of the collection elements.
       
   266      *
       
   267      * @return array
       
   268      */
       
   269     public function getKeys()
       
   270     {
       
   271         return array_keys($this->_elements);
       
   272     }
       
   273 
       
   274     /**
       
   275      * Gets all elements.
       
   276      *
       
   277      * @return array
       
   278      */
       
   279     public function getValues()
       
   280     {
       
   281         return array_values($this->_elements);
       
   282     }
       
   283 
       
   284     /**
       
   285      * Returns the number of elements in the collection.
       
   286      *
       
   287      * Implementation of the Countable interface.
       
   288      *
       
   289      * @return integer The number of elements in the collection.
       
   290      */
       
   291     public function count()
       
   292     {
       
   293         return count($this->_elements);
       
   294     }
       
   295 
       
   296     /**
       
   297      * Adds/sets an element in the collection at the index / with the specified key.
       
   298      *
       
   299      * When the collection is a Map this is like put(key,value)/add(key,value).
       
   300      * When the collection is a List this is like add(position,value).
       
   301      *
       
   302      * @param mixed $key
       
   303      * @param mixed $value
       
   304      */
       
   305     public function set($key, $value)
       
   306     {
       
   307         $this->_elements[$key] = $value;
       
   308     }
       
   309 
       
   310     /**
       
   311      * Adds an element to the collection.
       
   312      *
       
   313      * @param mixed $value
       
   314      * @return boolean Always TRUE.
       
   315      */
       
   316     public function add($value)
       
   317     {
       
   318         $this->_elements[] = $value;
       
   319         return true;
       
   320     }
       
   321 
       
   322     /**
       
   323      * Checks whether the collection is empty.
       
   324      * 
       
   325      * Note: This is preferrable over count() == 0.
       
   326      *
       
   327      * @return boolean TRUE if the collection is empty, FALSE otherwise.
       
   328      */
       
   329     public function isEmpty()
       
   330     {
       
   331         return ! $this->_elements;
       
   332     }
       
   333 
       
   334     /**
       
   335      * Gets an iterator for iterating over the elements in the collection.
       
   336      *
       
   337      * @return ArrayIterator
       
   338      */
       
   339     public function getIterator()
       
   340     {
       
   341         return new ArrayIterator($this->_elements);
       
   342     }
       
   343 
       
   344     /**
       
   345      * Applies the given function to each element in the collection and returns
       
   346      * a new collection with the elements returned by the function.
       
   347      *
       
   348      * @param Closure $func
       
   349      * @return Collection
       
   350      */
       
   351     public function map(Closure $func)
       
   352     {
       
   353         return new static(array_map($func, $this->_elements));
       
   354     }
       
   355 
       
   356     /**
       
   357      * Returns all the elements of this collection that satisfy the predicate p.
       
   358      * The order of the elements is preserved.
       
   359      *
       
   360      * @param Closure $p The predicate used for filtering.
       
   361      * @return Collection A collection with the results of the filter operation.
       
   362      */
       
   363     public function filter(Closure $p)
       
   364     {
       
   365         return new static(array_filter($this->_elements, $p));
       
   366     }
       
   367 
       
   368     /**
       
   369      * Applies the given predicate p to all elements of this collection,
       
   370      * returning true, if the predicate yields true for all elements.
       
   371      *
       
   372      * @param Closure $p The predicate.
       
   373      * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
       
   374      */
       
   375     public function forAll(Closure $p)
       
   376     {
       
   377         foreach ($this->_elements as $key => $element) {
       
   378             if ( ! $p($key, $element)) {
       
   379                 return false;
       
   380             }
       
   381         }
       
   382         
       
   383         return true;
       
   384     }
       
   385 
       
   386     /**
       
   387      * Partitions this collection in two collections according to a predicate.
       
   388      * Keys are preserved in the resulting collections.
       
   389      *
       
   390      * @param Closure $p The predicate on which to partition.
       
   391      * @return array An array with two elements. The first element contains the collection
       
   392      *               of elements where the predicate returned TRUE, the second element
       
   393      *               contains the collection of elements where the predicate returned FALSE.
       
   394      */
       
   395     public function partition(Closure $p)
       
   396     {
       
   397         $coll1 = $coll2 = array();
       
   398         foreach ($this->_elements as $key => $element) {
       
   399             if ($p($key, $element)) {
       
   400                 $coll1[$key] = $element;
       
   401             } else {
       
   402                 $coll2[$key] = $element;
       
   403             }
       
   404         }
       
   405         return array(new static($coll1), new static($coll2));
       
   406     }
       
   407 
       
   408     /**
       
   409      * Returns a string representation of this object.
       
   410      *
       
   411      * @return string
       
   412      */
       
   413     public function __toString()
       
   414     {
       
   415         return __CLASS__ . '@' . spl_object_hash($this);
       
   416     }
       
   417 
       
   418     /**
       
   419      * Clears the collection.
       
   420      */
       
   421     public function clear()
       
   422     {
       
   423         $this->_elements = array();
       
   424     }
       
   425 
       
   426     /**
       
   427      * Extract a slice of $length elements starting at position $offset from the Collection.
       
   428      *
       
   429      * If $length is null it returns all elements from $offset to the end of the Collection.
       
   430      * Keys have to be preserved by this method. Calling this method will only return the
       
   431      * selected slice and NOT change the elements contained in the collection slice is called on.
       
   432      *
       
   433      * @param int $offset
       
   434      * @param int $length
       
   435      * @return array
       
   436      */
       
   437     public function slice($offset, $length = null)
       
   438     {
       
   439         return array_slice($this->_elements, $offset, $length, true);
       
   440     }
       
   441 }