Skip to content

Instantly share code, notes, and snippets.

@lutangar
Created June 4, 2015 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lutangar/958ca72534b91efba5ed to your computer and use it in GitHub Desktop.
Save lutangar/958ca72534b91efba5ed to your computer and use it in GitHub Desktop.
ArrayCollection based on Doctrine's Collection and ArrayCollection
<?php
namespace Collection;
use ArrayIterator;
use Doctrine\Common\Collections\Collection as CollectionInterface;
use Closure;
/**
* Class ArrayCollection
*
* Based on Doctrine's Collection and ArrayCollection
* The CollectionInterface should be decoupled from Doctrine anyway
*/
class ArrayCollection implements CollectionInterface
{
/**
* An array containing the entries of this collection.
*
* @var array
*/
private $elements;
/**
* Initializes a new collection
*
* @param array $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
/**
* {@inheritDoc}
*/
public function add($value)
{
$this->elements[] = $value;
return true;
}
/**
* {@inheritDoc}
*/
public function clear()
{
$this->elements = [];
}
/**
* {@inheritDoc}
*/
public function contains($element)
{
return in_array($element, $this->elements, true);
}
/**
* {@inheritDoc}
*/
public function isEmpty()
{
return !$this->elements;
}
/**
* {@inheritDoc}
*/
public function remove($key)
{
if (isset($this->elements[$key]) || array_key_exists($key, $this->elements)) {
$removed = $this->elements[$key];
unset($this->elements[$key]);
return $removed;
}
return null;
}
/**
* {@inheritDoc}
*/
public function removeElement($element)
{
$key = array_search($element, $this->elements, true);
if ($key !== false) {
unset($this->elements[$key]);
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
public function containsKey($key)
{
return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
}
/**
* Gets all keys/indices of the collection.
*
* @return array The keys/indices of the collection, in the order of the corresponding
* elements in the collection.
*/
public function getKeys()
{
return array_keys($this->elements);
}
/**
* Gets all values of the collection.
*
* @return array The values of all elements in the collection, in the order they
* appear in the collection.
*/
public function getValues()
{
return array_values($this->elements);
}
/**
* Gets the element at the specified key/index.
*
* @param string|integer $key The key/index of the element to retrieve.
*
* @return mixed
*/
public function get($key)
{
if (isset($this->elements[$key])) {
return $this->elements[$key];
}
return null;
}
/**
* {@inheritDoc}
*/
public function set($key, $value)
{
$this->elements[$key] = $value;
return $this;
}
/**
* {@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 current()
{
return current($this->elements);
}
/**
* {@inheritDoc}
*/
public function next()
{
return next($this->elements);
}
/**
* {@inheritDoc}
*/
public function exists(Closure $p)
{
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
public function filter(Closure $p)
{
return new static(array_filter($this->elements, $p));
}
/**
* {@inheritDoc}
*/
public function forAll(Closure $p)
{
foreach ($this->elements as $key => $element) {
if (!$p($key, $element)) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
public function map(Closure $func)
{
return new static(array_map($func, $this->elements));
}
/**
* {@inheritDoc}
*/
public function partition(Closure $p)
{
$matches = $noMatches = [];
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
$matches[$key] = $element;
} else {
$noMatches[$key] = $element;
}
}
return [new static($matches), new static($noMatches)];
}
/**
* {@inheritDoc}
*/
public function indexOf($element)
{
return array_search($element, $this->elements, true);
}
/**
* {@inheritDoc}
*/
public function slice($offset, $length = null)
{
return array_slice($this->elements, $offset, $length, true);
}
/**
* Required by interface ArrayAccess.
*
* {@inheritDoc}
*/
public function offsetExists($offset)
{
return $this->containsKey($offset);
}
/**
* Required by interface ArrayAccess.
*
* {@inheritDoc}
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Required by interface ArrayAccess.
*
* {@inheritDoc}
*/
public function offsetSet($offset, $value)
{
if (!isset($offset)) {
return $this->add($value);
}
return $this->set($offset, $value);
}
/**
* Required by interface ArrayAccess.
*
* {@inheritDoc}
*/
public function offsetUnset($offset)
{
return $this->remove($offset);
}
/**
* {@inheritDoc}
*/
public function count()
{
return count($this->elements);
}
/**
* Required by interface IteratorAggregate.
*
* {@inheritDoc}
*/
public function getIterator()
{
return new ArrayIterator($this->elements);
}
/**
* Returns a string representation of this object.
*
* @return string
*/
public function __toString()
{
return __CLASS__ . '@' . spl_object_hash($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment