Here is a list of things from Doctrine's collections classes that I want to implement as well...
[ ] Collection::__toString()
/**
* Returns a string representation of this object.
*
* @return string
*/
public function __toString()
{
return __CLASS__ . '@' . spl_object_hash($this);
}
[ ] Basic iterator methods and toArray()
/**
* {@inheritDoc}
*/
public function toArray()
{
return $this->elements;
}
/**
* {@inheritDoc}
*/
public function first()
{
return reset($this->elements);
}
/**
* {@inheritDoc}
*/
public function last()
{
return end($this->elements);
}
/**
* {@inheritDoc}
*/
public function key()
{
return key($this->elements);
}
/**
* {@inheritDoc}
*/
public function next()
{
return next($this->elements);
}
/**
* {@inheritDoc}
*/
public function current()
{
return current($this->elements);
}
/**
* {@inheritDoc}
*/
public function remove($key)
{
if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
return null;
}
$removed = $this->elements[$key];
unset($this->elements[$key]);
return $removed;
}
/**
* {@inheritDoc}
*/
public function removeElement($element)
{
$key = array_search($element, $this->elements, true);
if ($key === false) {
return false;
}
unset($this->elements[$key]);
return true;
}
[ ] Contains and exists rather than both wrapped into one
/**
* {@inheritDoc}
*/
public function contains($element)
{
return in_array($element, $this->elements, true);
}
/**
* {@inheritDoc}
*/
public function exists(Closure $p)
{
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
return true;
}
}
return false;
}
[ ] indexOf
/**
* {@inheritDoc}
*/
public function indexOf($element)
{
return array_search($element, $this->elements, true);
}
[ ] getIterator()
/**
* Required by interface IteratorAggregate.
*
* {@inheritDoc}
*/
public function getIterator()
{
return new ArrayIterator($this->elements);
}
[ ] forAll()
/**
* {@inheritDoc}
*/
public function forAll(Closure $p)
{
foreach ($this->elements as $key => $element) {
if ( ! $p($key, $element)) {
return false;
}
}
return true;
}
[ ] partition
/**
* {@inheritDoc}
*/
public function partition(Closure $p)
{
$matches = $noMatches = array();
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
$matches[$key] = $element;
} else {
$noMatches[$key] = $element;
}
}
return array(new static($matches), new static($noMatches));
}
[ ] clear
/**
* {@inheritDoc}
*/
public function clear()
{
$this->elements = array();
}
[ ] slice
/**
* {@inheritDoc}
*/
public function slice($offset, $length = null)
{
return array_slice($this->elements, $offset, $length, true);
}
[ ] matching (like my where method was)
/**
* {@inheritDoc}
*/
public function matching(Criteria $criteria)
{
$expr = $criteria->getWhereExpression();
$filtered = $this->elements;
if ($expr) {
$visitor = new ClosureExpressionVisitor();
$filter = $visitor->dispatch($expr);
$filtered = array_filter($filtered, $filter);
}
if ($orderings = $criteria->getOrderings()) {
foreach (array_reverse($orderings) as $field => $ordering) {
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
}
uasort($filtered, $next);
}
$offset = $criteria->getFirstResult();
$length = $criteria->getMaxResults();
if ($offset || $length) {
$filtered = array_slice($filtered, (int)$offset, $length);
}
return new static($filtered);
}
[ ] Visitor pattern for matching criteria - take a look at the way they use the visitor pattern to match collection elements against a given set of criteria. It's pretty clever. I want something like this.
I've only given it a rudimentary glance, but one thing I liked and will most likely want to implement in my collections classes is a "SequentialCollection" or possibly a "IsSequential" trait. This would add functionality for collections where their order ALWAYS MATTERS. They are a sequence of elements. This would be useful for things such as job queues where the order they are executed matters (FIFO). Or maybe like a collection of orders where the earliest orders receive the highest priority. I'm sure there are tons of other applications for a sequential collection but I can't think properly right now.
Here is a list of things from Doctrine's collections classes that I want to implement as well...
I found another interesting Collections library which is actually implemented as a Symfony bundle, here: http://jmsyst.com/libs/PHP-Collection
I've only given it a rudimentary glance, but one thing I liked and will most likely want to implement in my collections classes is a "SequentialCollection" or possibly a "IsSequential" trait. This would add functionality for collections where their order ALWAYS MATTERS. They are a sequence of elements. This would be useful for things such as job queues where the order they are executed matters (FIFO). Or maybe like a collection of orders where the earliest orders receive the highest priority. I'm sure there are tons of other applications for a sequential collection but I can't think properly right now.