/**
* An ArrayCollection is a Collection implementation that wraps a regular PHP array.
*
+ * Warning: Using (un-)serialize() on a collection is not a supported use-case
+ * and may break when we change the internals in the future. If you need to
+ * serialize a collection use {@link toArray()} and reconstruct the collection
+ * manually.
+ *
* @since 2.0
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
$this->elements = $elements;
}
+ /**
+ * Creates a new instance from the specified elements.
+ *
+ * This method is provided for derived classes to specify how a new
+ * instance should be created when constructor semantics have changed.
+ *
+ * @param array $elements Elements.
+ *
+ * @return static
+ */
+ protected function createFrom(array $elements)
+ {
+ return new static($elements);
+ }
+
/**
* {@inheritDoc}
*/
/**
* {@inheritDoc}
*/
- public function add($value)
+ public function add($element)
{
- $this->elements[] = $value;
+ $this->elements[] = $element;
return true;
}
*/
public function map(Closure $func)
{
- return new static(array_map($func, $this->elements));
+ return $this->createFrom(array_map($func, $this->elements));
}
/**
*/
public function filter(Closure $p)
{
- return new static(array_filter($this->elements, $p));
+ return $this->createFrom(array_filter($this->elements, $p));
}
/**
}
}
- return array(new static($matches), new static($noMatches));
+ return array($this->createFrom($matches), $this->createFrom($noMatches));
}
/**
}
if ($orderings = $criteria->getOrderings()) {
+ $next = null;
foreach (array_reverse($orderings) as $field => $ordering) {
- $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
+ $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
}
uasort($filtered, $next);
$filtered = array_slice($filtered, (int)$offset, $length);
}
- return new static($filtered);
+ return $this->createFrom($filtered);
}
}