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 = [])
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)) {
184 $this->set($offset, $value);
188 * Required by interface ArrayAccess.
192 public function offsetUnset($offset)
194 $this->remove($offset);
200 public function containsKey($key)
202 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
208 public function contains($element)
210 return in_array($element, $this->elements, true);
216 public function exists(Closure $p)
218 foreach ($this->elements as $key => $element) {
219 if ($p($key, $element)) {
230 public function indexOf($element)
232 return array_search($element, $this->elements, true);
238 public function get($key)
240 return $this->elements[$key] ?? null;
246 public function getKeys()
248 return array_keys($this->elements);
254 public function getValues()
256 return array_values($this->elements);
262 public function count()
264 return count($this->elements);
270 public function set($key, $value)
272 $this->elements[$key] = $value;
278 public function add($element)
280 $this->elements[] = $element;
288 public function isEmpty()
290 return empty($this->elements);
294 * Required by interface IteratorAggregate.
298 public function getIterator()
300 return new ArrayIterator($this->elements);
308 public function map(Closure $func)
310 return $this->createFrom(array_map($func, $this->elements));
318 public function filter(Closure $p)
320 return $this->createFrom(array_filter($this->elements, $p));
326 public function forAll(Closure $p)
328 foreach ($this->elements as $key => $element) {
329 if ( ! $p($key, $element)) {
340 public function partition(Closure $p)
342 $matches = $noMatches = [];
344 foreach ($this->elements as $key => $element) {
345 if ($p($key, $element)) {
346 $matches[$key] = $element;
348 $noMatches[$key] = $element;
352 return [$this->createFrom($matches), $this->createFrom($noMatches)];
356 * Returns a string representation of this object.
360 public function __toString()
362 return __CLASS__ . '@' . spl_object_hash($this);
368 public function clear()
370 $this->elements = [];
376 public function slice($offset, $length = null)
378 return array_slice($this->elements, $offset, $length, true);
384 public function matching(Criteria $criteria)
386 $expr = $criteria->getWhereExpression();
387 $filtered = $this->elements;
390 $visitor = new ClosureExpressionVisitor();
391 $filter = $visitor->dispatch($expr);
392 $filtered = array_filter($filtered, $filter);
395 if ($orderings = $criteria->getOrderings()) {
397 foreach (array_reverse($orderings) as $field => $ordering) {
398 $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
401 uasort($filtered, $next);
404 $offset = $criteria->getFirstResult();
405 $length = $criteria->getMaxResults();
407 if ($offset || $length) {
408 $filtered = array_slice($filtered, (int)$offset, $length);
411 return $this->createFrom($filtered);