123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- <?php
- /**
- * @package Grav\Framework\Collection
- *
- * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Framework\Collection;
- use ArrayIterator;
- use Closure;
- /**
- * Abstract Index Collection.
- */
- abstract class AbstractIndexCollection implements CollectionInterface
- {
- /** @var array */
- private $entries;
- /**
- * Initializes a new IndexCollection.
- *
- * @param array $entries
- */
- public function __construct(array $entries = [])
- {
- $this->entries = $entries;
- }
- /**
- * {@inheritDoc}
- */
- public function toArray()
- {
- return $this->loadElements($this->entries);
- }
- /**
- * {@inheritDoc}
- */
- public function first()
- {
- $value = reset($this->entries);
- $key = key($this->entries);
- return $this->loadElement($key, $value);
- }
- /**
- * {@inheritDoc}
- */
- public function last()
- {
- $value = end($this->entries);
- $key = key($this->entries);
- return $this->loadElement($key, $value);
- }
- /**
- * {@inheritDoc}
- */
- public function key()
- {
- return key($this->entries);
- }
- /**
- * {@inheritDoc}
- */
- public function next()
- {
- $value = next($this->entries);
- $key = key($this->entries);
- return $this->loadElement($key, $value);
- }
- /**
- * {@inheritDoc}
- */
- public function current()
- {
- $value = current($this->entries);
- $key = key($this->entries);
- return $this->loadElement($key, $value);
- }
- /**
- * {@inheritDoc}
- */
- public function remove($key)
- {
- if (!array_key_exists($key, $this->entries)) {
- return null;
- }
- $value = $this->entries[$key];
- unset($this->entries[$key]);
- return $this->loadElement($key, $value);
- }
- /**
- * {@inheritDoc}
- */
- public function removeElement($element)
- {
- $key = $this->isAllowedElement($element) ? $element->getKey() : null;
- if (!$key || !isset($this->entries[$key])) {
- return false;
- }
- unset($this->entries[$key]);
- return 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 (null === $offset) {
- $this->add($value);
- }
- $this->set($offset, $value);
- }
- /**
- * Required by interface ArrayAccess.
- *
- * {@inheritDoc}
- */
- public function offsetUnset($offset)
- {
- return $this->remove($offset);
- }
- /**
- * {@inheritDoc}
- */
- public function containsKey($key)
- {
- return isset($this->entries[$key]) || array_key_exists($key, $this->entries);
- }
- /**
- * {@inheritDoc}
- */
- public function contains($element)
- {
- $key = $this->isAllowedElement($element) ? $element->getKey() : null;
- return $key && isset($this->entries[$key]);
- }
- /**
- * {@inheritDoc}
- */
- public function exists(Closure $p)
- {
- return $this->loadCollection($this->entries)->exists($p);
- }
- /**
- * {@inheritDoc}
- */
- public function indexOf($element)
- {
- $key = $this->isAllowedElement($element) ? $element->getKey() : null;
- return $key && isset($this->entries[$key]) ? $key : null;
- }
- /**
- * {@inheritDoc}
- */
- public function get($key)
- {
- if (!isset($this->entries[$key])) {
- return null;
- }
- return $this->loadElement($key, $this->entries[$key]);
- }
- /**
- * {@inheritDoc}
- */
- public function getKeys()
- {
- return array_keys($this->entries);
- }
- /**
- * {@inheritDoc}
- */
- public function getValues()
- {
- return array_values($this->loadElements($this->entries));
- }
- /**
- * {@inheritDoc}
- */
- public function count()
- {
- return \count($this->entries);
- }
- /**
- * {@inheritDoc}
- */
- public function set($key, $value)
- {
- if (!$this->isAllowedElement($value)) {
- throw new \InvalidArgumentException('Invalid argument $value');
- }
- if ($key !== $value->getKey()) {
- $value->setKey($key);
- }
- $this->entries[$key] = $this->getElementMeta($value);
- }
- /**
- * {@inheritDoc}
- */
- public function add($element)
- {
- if (!$this->isAllowedElement($element)) {
- throw new \InvalidArgumentException('Invalid argument $element');
- }
- $this->entries[$element->getKey()] = $this->getElementMeta($element);
- return true;
- }
- /**
- * {@inheritDoc}
- */
- public function isEmpty()
- {
- return empty($this->entries);
- }
- /**
- * Required by interface IteratorAggregate.
- *
- * {@inheritDoc}
- */
- public function getIterator()
- {
- return new ArrayIterator($this->loadElements());
- }
- /**
- * {@inheritDoc}
- */
- public function map(Closure $func)
- {
- return $this->loadCollection($this->entries)->map($func);
- }
- /**
- * {@inheritDoc}
- */
- public function filter(Closure $p)
- {
- return $this->loadCollection($this->entries)->filter($p);
- }
- /**
- * {@inheritDoc}
- */
- public function forAll(Closure $p)
- {
- return $this->loadCollection($this->entries)->forAll($p);
- }
- /**
- * {@inheritDoc}
- */
- public function partition(Closure $p)
- {
- return $this->loadCollection($this->entries)->partition($p);
- }
- /**
- * Returns a string representation of this object.
- *
- * @return string
- */
- public function __toString()
- {
- return __CLASS__ . '@' . spl_object_hash($this);
- }
- /**
- * {@inheritDoc}
- */
- public function clear()
- {
- $this->entries = [];
- }
- /**
- * {@inheritDoc}
- */
- public function slice($offset, $length = null)
- {
- return $this->loadElements(\array_slice($this->entries, $offset, $length, true));
- }
- /**
- * @param int $start
- * @param int|null $limit
- * @return static
- */
- public function limit($start, $limit = null)
- {
- return $this->createFrom(\array_slice($this->entries, $start, $limit, true));
- }
- /**
- * Reverse the order of the items.
- *
- * @return static
- */
- public function reverse()
- {
- return $this->createFrom(array_reverse($this->entries));
- }
- /**
- * Shuffle items.
- *
- * @return static
- */
- public function shuffle()
- {
- $keys = $this->getKeys();
- shuffle($keys);
- return $this->createFrom(array_replace(array_flip($keys), $this->entries));
- }
- /**
- * Select items from collection.
- *
- * Collection is returned in the order of $keys given to the function.
- *
- * @param array $keys
- * @return static
- */
- public function select(array $keys)
- {
- $list = [];
- foreach ($keys as $key) {
- if (isset($this->entries[$key])) {
- $list[$key] = $this->entries[$key];
- }
- }
- return $this->createFrom($list);
- }
- /**
- * Un-select items from collection.
- *
- * @param array $keys
- * @return static
- */
- public function unselect(array $keys)
- {
- return $this->select(array_diff($this->getKeys(), $keys));
- }
- /**
- * Split collection into chunks.
- *
- * @param int $size Size of each chunk.
- * @return array
- */
- public function chunk($size)
- {
- return $this->loadCollection($this->entries)->chunk($size);
- }
- /**
- * @return string
- */
- public function serialize()
- {
- return serialize(['entries' => $this->entries]);
- }
- /**
- * @param string $serialized
- */
- public function unserialize($serialized)
- {
- $data = unserialize($serialized, ['allowed_classes' => false]);
- $this->entries = $data['entries'];
- }
- /**
- * Implements JsonSerializable interface.
- *
- * @return array
- */
- public function jsonSerialize()
- {
- return $this->loadCollection()->jsonSerialize();
- }
- /**
- * 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 $entries Elements.
- *
- * @return static
- */
- protected function createFrom(array $entries)
- {
- return new static($entries);
- }
- /**
- * @return array
- */
- protected function getEntries() : array
- {
- return $this->entries;
- }
- /**
- * @param array $entries
- */
- protected function setEntries(array $entries) : void
- {
- $this->entries = $entries;
- }
- /**
- * @param string $key
- * @param mixed $value
- * @return mixed|null
- */
- abstract protected function loadElement($key, $value);
- /**
- * @param array|null $entries
- * @return array
- */
- abstract protected function loadElements(array $entries = null) : array;
- /**
- * @param array|null $entries
- * @return CollectionInterface
- */
- abstract protected function loadCollection(array $entries = null) : CollectionInterface;
- /**
- * @param mixed $value
- * @return bool
- */
- abstract protected function isAllowedElement($value) : bool;
- /**
- * @param mixed $element
- * @return mixed
- */
- abstract protected function getElementMeta($element);
- }
|