Skip to content

Commit

Permalink
Added ArrayCollection::createFrom.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Jul 8, 2016
1 parent 315485a commit b83f187
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/Doctrine/Common/Collections/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public function __construct(array $elements = array())
$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}
*/
Expand Down Expand Up @@ -289,15 +304,15 @@ public function getIterator()
*/
public function map(Closure $func)
{
return new static(array_map($func, $this->elements));
return $this->createFrom(array_map($func, $this->elements));
}

/**
* {@inheritDoc}
*/
public function filter(Closure $p)
{
return new static(array_filter($this->elements, $p));
return $this->createFrom(array_filter($this->elements, $p));
}

/**
Expand Down Expand Up @@ -329,7 +344,7 @@ public function partition(Closure $p)
}
}

return array(new static($matches), new static($noMatches));
return array($this->createFrom($matches), $this->createFrom($noMatches));
}

/**
Expand Down Expand Up @@ -388,6 +403,6 @@ public function matching(Criteria $criteria)
$filtered = array_slice($filtered, (int)$offset, $length);
}

return new static($filtered);
return $this->createFrom($filtered);
}
}

0 comments on commit b83f187

Please sign in to comment.