/**
* 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>
*
* @param array $elements
*/
- public function __construct(array $elements = array())
+ public function __construct(array $elements = [])
{
$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}
*/
public function offsetSet($offset, $value)
{
if ( ! isset($offset)) {
- return $this->add($value);
+ $this->add($value);
+ return;
}
$this->set($offset, $value);
*/
public function offsetUnset($offset)
{
- return $this->remove($offset);
+ $this->remove($offset);
}
/**
*/
public function get($key)
{
- return isset($this->elements[$key]) ? $this->elements[$key] : null;
+ return $this->elements[$key] ?? null;
}
/**
/**
* {@inheritDoc}
*/
- public function add($value)
+ public function add($element)
{
- $this->elements[] = $value;
+ $this->elements[] = $element;
return true;
}
/**
* {@inheritDoc}
+ *
+ * @return static
*/
public function map(Closure $func)
{
- return new static(array_map($func, $this->elements));
+ return $this->createFrom(array_map($func, $this->elements));
}
/**
* {@inheritDoc}
+ *
+ * @return static
*/
public function filter(Closure $p)
{
- return new static(array_filter($this->elements, $p));
+ return $this->createFrom(array_filter($this->elements, $p));
}
/**
*/
public function partition(Closure $p)
{
- $matches = $noMatches = array();
+ $matches = $noMatches = [];
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
}
}
- return array(new static($matches), new static($noMatches));
+ return [$this->createFrom($matches), $this->createFrom($noMatches)];
}
/**
*/
public function clear()
{
- $this->elements = array();
+ $this->elements = [];
}
/**
}
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);
}
}