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.
30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
31 * @author Jonathan Wage <jonwage@gmail.com>
32 * @author Roman Borschel <roman@code-factory.org>
34 class ArrayCollection implements Collection, Selectable
37 * An array containing the entries of this collection.
44 * Initializes a new ArrayCollection.
46 * @param array $elements
48 public function __construct(array $elements = array())
50 $this->elements = $elements;
56 public function toArray()
58 return $this->elements;
64 public function first()
66 return reset($this->elements);
72 public function last()
74 return end($this->elements);
82 return key($this->elements);
88 public function next()
90 return next($this->elements);
96 public function current()
98 return current($this->elements);
104 public function remove($key)
106 if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
110 $removed = $this->elements[$key];
111 unset($this->elements[$key]);
119 public function removeElement($element)
121 $key = array_search($element, $this->elements, true);
123 if ($key === false) {
127 unset($this->elements[$key]);
133 * Required by interface ArrayAccess.
137 public function offsetExists($offset)
139 return $this->containsKey($offset);
143 * Required by interface ArrayAccess.
147 public function offsetGet($offset)
149 return $this->get($offset);
153 * Required by interface ArrayAccess.
157 public function offsetSet($offset, $value)
159 if ( ! isset($offset)) {
160 return $this->add($value);
163 $this->set($offset, $value);
167 * Required by interface ArrayAccess.
171 public function offsetUnset($offset)
173 return $this->remove($offset);
179 public function containsKey($key)
181 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
187 public function contains($element)
189 return in_array($element, $this->elements, true);
195 public function exists(Closure $p)
197 foreach ($this->elements as $key => $element) {
198 if ($p($key, $element)) {
209 public function indexOf($element)
211 return array_search($element, $this->elements, true);
217 public function get($key)
219 return isset($this->elements[$key]) ? $this->elements[$key] : null;
225 public function getKeys()
227 return array_keys($this->elements);
233 public function getValues()
235 return array_values($this->elements);
241 public function count()
243 return count($this->elements);
249 public function set($key, $value)
251 $this->elements[$key] = $value;
257 public function add($value)
259 $this->elements[] = $value;
267 public function isEmpty()
269 return empty($this->elements);
273 * Required by interface IteratorAggregate.
277 public function getIterator()
279 return new ArrayIterator($this->elements);
285 public function map(Closure $func)
287 return new static(array_map($func, $this->elements));
293 public function filter(Closure $p)
295 return new static(array_filter($this->elements, $p));
301 public function forAll(Closure $p)
303 foreach ($this->elements as $key => $element) {
304 if ( ! $p($key, $element)) {
315 public function partition(Closure $p)
317 $matches = $noMatches = array();
319 foreach ($this->elements as $key => $element) {
320 if ($p($key, $element)) {
321 $matches[$key] = $element;
323 $noMatches[$key] = $element;
327 return array(new static($matches), new static($noMatches));
331 * Returns a string representation of this object.
335 public function __toString()
337 return __CLASS__ . '@' . spl_object_hash($this);
343 public function clear()
345 $this->elements = array();
351 public function slice($offset, $length = null)
353 return array_slice($this->elements, $offset, $length, true);
359 public function matching(Criteria $criteria)
361 $expr = $criteria->getWhereExpression();
362 $filtered = $this->elements;
365 $visitor = new ClosureExpressionVisitor();
366 $filter = $visitor->dispatch($expr);
367 $filtered = array_filter($filtered, $filter);
370 if ($orderings = $criteria->getOrderings()) {
371 foreach (array_reverse($orderings) as $field => $ordering) {
372 $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
375 uasort($filtered, $next);
378 $offset = $criteria->getFirstResult();
379 $length = $criteria->getMaxResults();
381 if ($offset || $length) {
382 $filtered = array_slice($filtered, (int)$offset, $length);
385 return new static($filtered);