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;
24 use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
27 * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
29 * Warning: Using (un-)serialize() on a collection is not a supported use-case
30 * and may break when we change the internals in the future. If you need to
31 * serialize a collection use {@link toArray()} and reconstruct the collection
35 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
36 * @author Jonathan Wage <jonwage@gmail.com>
37 * @author Roman Borschel <roman@code-factory.org>
39 class ArrayCollection implements Collection, Selectable
42 * An array containing the entries of this collection.
49 * Initializes a new ArrayCollection.
51 * @param array $elements
53 public function __construct(array $elements = array())
55 $this->elements = $elements;
59 * Creates a new instance from the specified elements.
61 * This method is provided for derived classes to specify how a new
62 * instance should be created when constructor semantics have changed.
64 * @param array $elements Elements.
68 protected function createFrom(array $elements)
70 return new static($elements);
76 public function toArray()
78 return $this->elements;
84 public function first()
86 return reset($this->elements);
92 public function last()
94 return end($this->elements);
100 public function key()
102 return key($this->elements);
108 public function next()
110 return next($this->elements);
116 public function current()
118 return current($this->elements);
124 public function remove($key)
126 if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
130 $removed = $this->elements[$key];
131 unset($this->elements[$key]);
139 public function removeElement($element)
141 $key = array_search($element, $this->elements, true);
143 if ($key === false) {
147 unset($this->elements[$key]);
153 * Required by interface ArrayAccess.
157 public function offsetExists($offset)
159 return $this->containsKey($offset);
163 * Required by interface ArrayAccess.
167 public function offsetGet($offset)
169 return $this->get($offset);
173 * Required by interface ArrayAccess.
177 public function offsetSet($offset, $value)
179 if ( ! isset($offset)) {
180 return $this->add($value);
183 $this->set($offset, $value);
187 * Required by interface ArrayAccess.
191 public function offsetUnset($offset)
193 return $this->remove($offset);
199 public function containsKey($key)
201 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
207 public function contains($element)
209 return in_array($element, $this->elements, true);
215 public function exists(Closure $p)
217 foreach ($this->elements as $key => $element) {
218 if ($p($key, $element)) {
229 public function indexOf($element)
231 return array_search($element, $this->elements, true);
237 public function get($key)
239 return isset($this->elements[$key]) ? $this->elements[$key] : null;
245 public function getKeys()
247 return array_keys($this->elements);
253 public function getValues()
255 return array_values($this->elements);
261 public function count()
263 return count($this->elements);
269 public function set($key, $value)
271 $this->elements[$key] = $value;
277 public function add($element)
279 $this->elements[] = $element;
287 public function isEmpty()
289 return empty($this->elements);
293 * Required by interface IteratorAggregate.
297 public function getIterator()
299 return new ArrayIterator($this->elements);
305 public function map(Closure $func)
307 return $this->createFrom(array_map($func, $this->elements));
313 public function filter(Closure $p)
315 return $this->createFrom(array_filter($this->elements, $p));
321 public function forAll(Closure $p)
323 foreach ($this->elements as $key => $element) {
324 if ( ! $p($key, $element)) {
335 public function partition(Closure $p)
337 $matches = $noMatches = array();
339 foreach ($this->elements as $key => $element) {
340 if ($p($key, $element)) {
341 $matches[$key] = $element;
343 $noMatches[$key] = $element;
347 return array($this->createFrom($matches), $this->createFrom($noMatches));
351 * Returns a string representation of this object.
355 public function __toString()
357 return __CLASS__ . '@' . spl_object_hash($this);
363 public function clear()
365 $this->elements = array();
371 public function slice($offset, $length = null)
373 return array_slice($this->elements, $offset, $length, true);
379 public function matching(Criteria $criteria)
381 $expr = $criteria->getWhereExpression();
382 $filtered = $this->elements;
385 $visitor = new ClosureExpressionVisitor();
386 $filter = $visitor->dispatch($expr);
387 $filtered = array_filter($filtered, $filter);
390 if ($orderings = $criteria->getOrderings()) {
392 foreach (array_reverse($orderings) as $field => $ordering) {
393 $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
396 uasort($filtered, $next);
399 $offset = $criteria->getFirstResult();
400 $length = $criteria->getMaxResults();
402 if ($offset || $length) {
403 $filtered = array_slice($filtered, (int)$offset, $length);
406 return $this->createFrom($filtered);