Created
September 11, 2018 04:09
-
-
Save cuonghuynh/103cb587b260411d678397a220380e66 to your computer and use it in GitHub Desktop.
ArrayCollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class ArrayCollection | |
* | |
* @package Shopmacher\Base\FrontendBundle\Model | |
*/ | |
class ArrayCollection implements CollectionInterface | |
{ | |
/** | |
* @var array | |
*/ | |
private $elements; | |
/** | |
* ArrayCollection constructor. | |
* | |
* @param array $elements | |
*/ | |
public function __construct(array $elements = []) | |
{ | |
$this->elements = $elements; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function next() | |
{ | |
return next($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function isEmpty() | |
{ | |
return empty($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 current() | |
{ | |
return current($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function slice($offset, $length = null) | |
{ | |
return array_slice($this->elements, $offset, $length, true); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function partition(Closure $p) | |
{ | |
$matches = $noMatches = array(); | |
foreach ($this->elements as $key => $element) { | |
if ($p($key, $element)) { | |
$matches[$key] = $element; | |
continue; | |
} | |
$noMatches[$key] = $element; | |
} | |
return array(new static($matches), new static($noMatches)); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function removeElement($element) | |
{ | |
$key = array_search($element, $this->elements, true); | |
if ($key === false) { | |
return false; | |
} | |
unset($this->elements[$key]); | |
return true; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function map(Closure $func) | |
{ | |
return new static(array_map($func, $this->elements)); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function key() | |
{ | |
return key($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function set($key, $value) | |
{ | |
$this->elements[$key] = $value; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function forAll(Closure $p) | |
{ | |
foreach ($this->elements as $key => $element) { | |
if (!$p($key, $element)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function clear() | |
{ | |
$this->elements = array(); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getValues() | |
{ | |
return array_values($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function filter(Closure $p) | |
{ | |
return new static(array_filter($this->elements, $p)); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function toArray() | |
{ | |
return $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 add($item) | |
{ | |
$this->elements[] = $item; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function first() | |
{ | |
return reset($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function last() | |
{ | |
return end($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getIterator() | |
{ | |
return new \ArrayIterator($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getElements() | |
{ | |
return $this->elements; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function count() | |
{ | |
return count($this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function offsetGet($offset) | |
{ | |
return $this->get($offset); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
if (!isset($offset)) { | |
$this->add($value); | |
return; | |
} | |
$this->set($offset, $value); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function offsetUnset($offset) | |
{ | |
return $this->remove($offset); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function offsetExists($offset) | |
{ | |
return $this->containsKey($offset); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function containsKey($key) | |
{ | |
return isset($this->elements[$key]) || array_key_exists($key, $this->elements); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function contains($element) | |
{ | |
return in_array($element, $this->elements, true); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function indexOf($element) | |
{ | |
return array_search($element, $this->elements, true); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function get($key) | |
{ | |
return isset($this->elements[$key]) ? $this->elements[$key] : null; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getKeys() | |
{ | |
return array_keys($this->elements); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Interface CollectionInterface | |
* | |
* @package Shopmacher\Base\FrontendBundle\Model | |
*/ | |
interface CollectionInterface extends Countable, IteratorAggregate, ArrayAccess | |
{ | |
/** | |
* Adds an element at the end of the collection. | |
* | |
* @param mixed $element The element to add. | |
* | |
* @return boolean Always TRUE. | |
*/ | |
public function add($element); | |
/** | |
* Clears the collection, removing all elements. | |
* | |
* @return void | |
*/ | |
public function clear(); | |
/** | |
* Checks whether an element is contained in the collection. | |
* This is an O(n) operation, where n is the size of the collection. | |
* | |
* @param mixed $element The element to search for. | |
* | |
* @return boolean TRUE if the collection contains the element, FALSE otherwise. | |
*/ | |
public function contains($element); | |
/** | |
* Checks whether the collection is empty (contains no elements). | |
* | |
* @return boolean TRUE if the collection is empty, FALSE otherwise. | |
*/ | |
public function isEmpty(); | |
/** | |
* Removes the element at the specified index from the collection. | |
* | |
* @param string|integer $key The kex/index of the element to remove. | |
* | |
* @return mixed The removed element or NULL, if the collection did not contain the element. | |
*/ | |
public function remove($key); | |
/** | |
* Removes the specified element from the collection, if it is found. | |
* | |
* @param mixed $element The element to remove. | |
* | |
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise. | |
*/ | |
public function removeElement($element); | |
/** | |
* Checks whether the collection contains an element with the specified key/index. | |
* | |
* @param string|integer $key The key/index to check for. | |
* | |
* @return boolean TRUE if the collection contains an element with the specified key/index, | |
* FALSE otherwise. | |
*/ | |
public function containsKey($key); | |
/** | |
* 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); | |
/** | |
* 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(); | |
/** | |
* 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(); | |
/** | |
* Sets an element in the collection at the specified key/index. | |
* | |
* @param string|integer $key The key/index of the element to set. | |
* @param mixed $value The element to set. | |
* | |
* @return void | |
*/ | |
public function set($key, $value); | |
/** | |
* Gets a native PHP array representation of the collection. | |
* | |
* @return array | |
*/ | |
public function toArray(); | |
/** | |
* Sets the internal iterator to the first element in the collection and returns this element. | |
* | |
* @return mixed | |
*/ | |
public function first(); | |
/** | |
* Sets the internal iterator to the last element in the collection and returns this element. | |
* | |
* @return mixed | |
*/ | |
public function last(); | |
/** | |
* Gets the key/index of the element at the current iterator position. | |
* | |
* @return int|string | |
*/ | |
public function key(); | |
/** | |
* Gets the element of the collection at the current iterator position. | |
* | |
* @return mixed | |
*/ | |
public function current(); | |
/** | |
* Moves the internal iterator position to the next element and returns this element. | |
* | |
* @return mixed | |
*/ | |
public function next(); | |
/** | |
* Tests for the existence of an element that satisfies the given predicate. | |
* | |
* @param Closure $p The predicate. | |
* | |
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise. | |
*/ | |
public function exists(Closure $p); | |
/** | |
* Returns all the elements of this collection that satisfy the predicate p. | |
* The order of the elements is preserved. | |
* | |
* @param Closure $p The predicate used for filtering. | |
* | |
* @return CollectionInterface A collection with the results of the filter operation. | |
*/ | |
public function filter(Closure $p); | |
/** | |
* Tests whether the given predicate p holds for all elements of this collection. | |
* | |
* @param Closure $p The predicate. | |
* | |
* @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. | |
*/ | |
public function forAll(Closure $p); | |
/** | |
* Applies the given function to each element in the collection and returns | |
* a new collection with the elements returned by the function. | |
* | |
* @param Closure $func | |
* | |
* @return CollectionInterface | |
*/ | |
public function map(Closure $func); | |
/** | |
* Partitions this collection in two collections according to a predicate. | |
* Keys are preserved in the resulting collections. | |
* | |
* @param Closure $p The predicate on which to partition. | |
* | |
* @return array An array with two elements. The first element contains the collection | |
* of elements where the predicate returned TRUE, the second element | |
* contains the collection of elements where the predicate returned FALSE. | |
*/ | |
public function partition(Closure $p); | |
/** | |
* Gets the index/key of a given element. The comparison of two elements is strict, | |
* that means not only the value but also the type must match. | |
* For objects this means reference equality. | |
* | |
* @param mixed $element The element to search for. | |
* | |
* @return int|string|bool The key/index of the element or FALSE if the element was not found. | |
*/ | |
public function indexOf($element); | |
/** | |
* Extracts a slice of $length elements starting at position $offset from the Collection. | |
* | |
* If $length is null it returns all elements from $offset to the end of the Collection. | |
* Keys have to be preserved by this method. Calling this method will only return the | |
* selected slice and NOT change the elements contained in the collection slice is called on. | |
* | |
* @param int $offset The offset to start from. | |
* @param int|null $length The maximum number of elements to return, or null for no limit. | |
* | |
* @return array | |
*/ | |
public function slice($offset, $length = null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment