2008-12-18 17:08:11 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Common\Collections;
|
2008-12-18 17:08:11 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
use \Countable;
|
|
|
|
use \IteratorAggregate;
|
|
|
|
use \ArrayAccess;
|
2009-01-29 20:00:44 +03:00
|
|
|
use \ArrayIterator;
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A Collection is a wrapper around a php array and just like a php array a
|
2009-01-12 16:34:41 +03:00
|
|
|
* collection instance can be a list, a set or a map, depending on how it is used.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class Collection implements Countable, IteratorAggregate, ArrayAccess
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* An array containing the entries of this collection.
|
|
|
|
* This is the wrapped php array.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_data = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param <type> $elements
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function __construct(array $elements = array())
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_data = $elements;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Unwraps the array contained in the Collection instance.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @return array The wrapped array.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function unwrap()
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
return $this->_data;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* returns the first entry in the collection
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function first()
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
return reset($this->_data);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the last record in the collection
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function last()
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
|
|
|
return end($this->_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the current key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return key($this->_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Removes an entry with a specific key from the collection.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @param mixed $key
|
2009-01-12 16:34:41 +03:00
|
|
|
* @return mixed
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function remove($key)
|
|
|
|
{
|
|
|
|
$removed = $this->_data[$key];
|
|
|
|
unset($this->_data[$key]);
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
/**
|
|
|
|
* Removes the specified element from the collection, if it is found.
|
|
|
|
*
|
|
|
|
* @param mixed $element
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function removeElement($element)
|
|
|
|
{
|
|
|
|
$key = array_search($element, $this->_data, true);
|
|
|
|
if ($key !== false) {
|
|
|
|
unset($this->_data[$key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see containsKey()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function __isset($key)
|
|
|
|
{
|
|
|
|
return $this->containsKey($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see remove()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function __unset($key)
|
|
|
|
{
|
|
|
|
return $this->remove($key);
|
|
|
|
}
|
|
|
|
|
2009-01-12 16:34:41 +03:00
|
|
|
/* ArrayAccess implementation */
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see containsKey()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return $this->containsKey($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see get()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return $this->get($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see add()
|
|
|
|
* @see set()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
if ( ! isset($offset)) {
|
|
|
|
return $this->add($value);
|
|
|
|
}
|
|
|
|
return $this->set($offset, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:56:43 +03:00
|
|
|
* @see remove()
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
return $this->remove($offset);
|
|
|
|
}
|
|
|
|
|
2009-01-12 16:34:41 +03:00
|
|
|
/* END ArrayAccess implementation */
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Checks whether the collection contains a specific key/index.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param mixed $key The key to check for.
|
2009-01-14 00:56:43 +03:00
|
|
|
* @return boolean TRUE if the given key/index exists, FALSE otherwise.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function containsKey($key)
|
|
|
|
{
|
|
|
|
return isset($this->_data[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Checks whether the given element is contained in the collection.
|
|
|
|
* Only element values are compared, not keys. 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.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param mixed $element
|
2009-01-14 00:56:43 +03:00
|
|
|
* @return boolean TRUE if the given element is contained in the collection,
|
|
|
|
* FALSE otherwise.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function contains($element)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
return in_array($element, $this->_data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the existance of an element that satisfies the given predicate.
|
|
|
|
*
|
|
|
|
* @param function $func
|
2009-01-29 20:00:44 +03:00
|
|
|
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
|
2009-01-12 16:34:41 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
public function exists(Closure $func) {
|
2009-01-12 16:34:41 +03:00
|
|
|
foreach ($this->_data as $key => $element)
|
|
|
|
if ($func($key, $element))
|
|
|
|
return true;
|
|
|
|
return false;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $otherColl
|
|
|
|
* @todo Impl
|
|
|
|
*/
|
|
|
|
public function containsAll($otherColl)
|
|
|
|
{
|
2009-01-29 20:00:44 +03:00
|
|
|
throw new DoctrineException("Not yet implemented.");
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Searches for a given element and, if found, returns the corresponding key/index
|
|
|
|
* of that 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.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param mixed $element The element to search for.
|
|
|
|
* @return mixed The key/index of the element or FALSE if the element was not found.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function search($element)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
return array_search($element, $this->_data, true);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Gets the element with the given key/index.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param mixed $key The key.
|
|
|
|
* @return mixed The element or NULL, if no element exists for the given key.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
if (isset($this->_data[$key])) {
|
|
|
|
return $this->_data[$key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Gets all keys/indexes.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getKeys()
|
|
|
|
{
|
|
|
|
return array_keys($this->_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Gets all elements.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function getElements()
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
|
|
|
return array_values($this->_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Returns the number of elements in the collection.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* Implementation of the Countable interface.
|
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @return integer The number of elements in the collection.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Adds/sets an element in the collection at the index / with the specified key.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* When the collection is a Map this is like put(key,value)/add(key,value).
|
|
|
|
* When the collection is a List this is like add(position,value).
|
|
|
|
*
|
|
|
|
* @param integer $key
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->_data[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Adds an element to the collection.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string $key
|
2009-01-12 16:34:41 +03:00
|
|
|
* @return boolean Always returns TRUE.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-12 16:34:41 +03:00
|
|
|
public function add($value)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_data[] = $value;
|
2008-12-18 17:08:11 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds all entities of the other collection to this collection.
|
|
|
|
*
|
|
|
|
* @param unknown_type $otherCollection
|
|
|
|
* @todo Impl
|
|
|
|
*/
|
|
|
|
public function addAll($otherCollection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the collection is empty.
|
2009-01-12 16:34:41 +03:00
|
|
|
* Note: This is preferrable over count() == 0.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @return boolean TRUE if the collection is empty, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
// Note: Little "trick". Empty arrays evaluate to FALSE. No need to count().
|
|
|
|
return ! (bool)$this->_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Gets an iterator that enables foreach() iteration over the elements in
|
|
|
|
* the collection.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @return ArrayIterator
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
$data = $this->_data;
|
|
|
|
return new ArrayIterator($data);
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
/**
|
2009-01-12 16:34:41 +03:00
|
|
|
* Applies the given function to each element in the collection and returns
|
|
|
|
* a new collection with the modified values.
|
2009-01-04 19:15:32 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param function $func
|
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
public function map(Closure $func)
|
2009-01-12 16:34:41 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
return new Collection(array_map($func, $this->_data));
|
2009-01-12 16:34:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the given function to each element in the collection and returns
|
|
|
|
* a new collection with the new values.
|
2009-01-04 19:15:32 +03:00
|
|
|
*
|
2009-01-12 16:34:41 +03:00
|
|
|
* @param function $func
|
2009-01-04 19:15:32 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
public function filter(Closure $func)
|
2009-01-12 16:34:41 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
return new Collection(array_filter($this->_data, $func));
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
return __CLASS__ . '@' . spl_object_hash($this);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the collection.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->_data = array();
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 19:15:32 +03:00
|
|
|
|