diff --git a/src/Collection/BaseCollection.php b/src/Collection/BaseCollection.php index 5eb5c3e..36e46b7 100644 --- a/src/Collection/BaseCollection.php +++ b/src/Collection/BaseCollection.php @@ -10,8 +10,9 @@ declare(strict_types=1); namespace Meritoo\Common\Collection; +use ArrayIterator; use Meritoo\Common\Contract\Collection\CollectionInterface; -use Meritoo\Common\Traits\CollectionTrait; +use Meritoo\Common\Utilities\Arrays; /** * Collection of elements with the same type @@ -21,7 +22,12 @@ use Meritoo\Common\Traits\CollectionTrait; */ abstract class BaseCollection implements CollectionInterface { - use CollectionTrait; + /** + * The elements of collection + * + * @var array + */ + private $elements; /** * Class constructor @@ -34,6 +40,225 @@ abstract class BaseCollection implements CollectionInterface $this->elements = $this->prepareElements($validated); } + /** + * Returns representation of object as array + * + * @return array + */ + public function toArray(): array + { + return $this->elements; + } + + /** + * Adds given element (at the end of collection) + * + * @param mixed $element The element to add + * @param mixed $index (optional) Index / key of the element + */ + public function add($element, $index = null): void + { + if (!$this->isValidType($element)) { + return; + } + + if (null === $index) { + $this->elements[] = $element; + + return; + } + + $this->elements[$index] = $element; + } + + /** + * Adds given elements (at the end of collection) + * + * @param array|CollectionInterface $elements The elements to add + * @param bool $useIndexes (optional) If is set to true, indexes of given elements will be + * used in this collection. Otherwise - not. + */ + public function addMultiple($elements, bool $useIndexes = false): void + { + if (empty($elements)) { + return; + } + + foreach ($elements as $index => $element) { + if ($useIndexes) { + $this->add($element, $index); + + continue; + } + + $this->add($element); + } + } + + /** + * Prepends given element (adds given element at the beginning of collection) + * + * @param mixed $element The element to prepend + */ + public function prepend($element): void + { + array_unshift($this->elements, $element); + } + + /** + * Removes given element + * + * @param mixed $element The element to remove + */ + public function remove($element): void + { + if (0 === $this->count()) { + return; + } + + foreach ($this->elements as $index => $existing) { + if ($element === $existing) { + unset($this->elements[$index]); + + break; + } + } + } + + /** + * Returns previous element for given element + * + * @param mixed $element The element to verify + * @return null|mixed + */ + public function getPrevious($element) + { + return Arrays::getPreviousElement($this->elements, $element); + } + + /** + * Returns next element for given element + * + * @param mixed $element The element to verify + * @return null|mixed + */ + public function getNext($element) + { + return Arrays::getNextElement($this->elements, $element); + } + + /** + * Returns the first element in the collection + * + * @return mixed + */ + public function getFirst() + { + return Arrays::getFirstElement($this->elements); + } + + /** + * Returns the last element in the collection + * + * @return mixed + */ + public function getLast() + { + return Arrays::getLastElement($this->elements); + } + + /** + * Returns element with given index + * + * @param mixed $index Index / key of the element + * @return null|mixed + */ + public function getByIndex($index) + { + return $this->elements[$index] ?? null; + } + + /** + * Returns information if collection is empty + * + * @return bool + */ + public function isEmpty(): bool + { + return empty($this->elements); + } + + /** + * Returns information if given element is first in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function isFirst($element): bool + { + return reset($this->elements) === $element; + } + + /** + * Returns information if given element is last in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function isLast($element): bool + { + return end($this->elements) === $element; + } + + /** + * Returns information if the collection has given element, iow. if given element exists in the collection + * + * @param mixed $element The element to verify + * @return bool + */ + public function has($element): bool + { + $index = Arrays::getIndexOf($this->elements, $element); + + return null !== $index && false !== $index; + } + + public function count(): int + { + return count($this->elements); + } + + public function offsetExists($offset): bool + { + return $this->exists($offset); + } + + public function offsetGet($offset) + { + if ($this->exists($offset)) { + return $this->elements[$offset]; + } + + return null; + } + + public function offsetSet($offset, $value): void + { + $this->elements[$offset] = $value; + } + + public function offsetUnset($offset): void + { + if ($this->exists($offset)) { + unset($this->elements[$offset]); + } + } + + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->elements); + } + /** * Prepares elements to initialize the collection. * Feel free to override and prepare elements in your way. @@ -78,4 +303,15 @@ abstract class BaseCollection implements CollectionInterface return $result; } + + /** + * Returns information if element with given index/key exists + * + * @param int|string $index The index/key of element + * @return bool + */ + private function exists($index): bool + { + return isset($this->elements[$index]) || array_key_exists($index, $this->elements); + } } diff --git a/src/Traits/Collection/AddTrait.php b/src/Traits/Collection/AddTrait.php deleted file mode 100644 index 62b715d..0000000 --- a/src/Traits/Collection/AddTrait.php +++ /dev/null @@ -1,65 +0,0 @@ - - * @copyright Meritoo - */ -trait AddTrait -{ - /** - * Adds given element (at the end of collection) - * - * @param mixed $element The element to add - * @param mixed $index (optional) Index / key of the element - */ - public function add($element, $index = null): void - { - if (!$this->isValidType($element)) { - return; - } - - if (null === $index) { - $this->elements[] = $element; - - return; - } - - $this->elements[$index] = $element; - } - - /** - * Adds given elements (at the end of collection) - * - * @param array|CollectionInterface $elements The elements to add - * @param bool $useIndexes (optional) If is set to true, indexes of given elements will be - * used in this collection. Otherwise - not. - */ - public function addMultiple($elements, bool $useIndexes = false): void - { - if (empty($elements)) { - return; - } - - foreach ($elements as $index => $element) { - if ($useIndexes) { - $this->add($element, $index); - - continue; - } - - $this->add($element); - } - } -} diff --git a/src/Traits/Collection/ArrayAccessTrait.php b/src/Traits/Collection/ArrayAccessTrait.php deleted file mode 100644 index faa6076..0000000 --- a/src/Traits/Collection/ArrayAccessTrait.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @copyright Meritoo - */ -trait ArrayAccessTrait -{ - /** - * {@inheritdoc} - */ - public function offsetExists($offset): bool - { - return $this->exists($offset); - } - - /** - * {@inheritdoc} - */ - public function offsetGet($offset) - { - if ($this->exists($offset)) { - return $this->elements[$offset]; - } - - return null; - } - - /** - * {@inheritdoc} - */ - public function offsetSet($offset, $value): void - { - $this->elements[$offset] = $value; - } - - /** - * {@inheritdoc} - */ - public function offsetUnset($offset): void - { - if ($this->exists($offset)) { - unset($this->elements[$offset]); - } - } - - /** - * Returns information if element with given index/key exists - * - * @param int|string $index The index/key of element - * @return bool - */ - private function exists($index): bool - { - return isset($this->elements[$index]) || array_key_exists($index, $this->elements); - } -} diff --git a/src/Traits/Collection/CountableTrait.php b/src/Traits/Collection/CountableTrait.php deleted file mode 100644 index 761581f..0000000 --- a/src/Traits/Collection/CountableTrait.php +++ /dev/null @@ -1,26 +0,0 @@ - - * @copyright Meritoo - */ -trait CountableTrait -{ - /** - * {@inheritdoc} - */ - public function count(): int - { - return count($this->elements); - } -} diff --git a/src/Traits/Collection/GetTrait.php b/src/Traits/Collection/GetTrait.php deleted file mode 100644 index 4cba31b..0000000 --- a/src/Traits/Collection/GetTrait.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @copyright Meritoo - */ -trait GetTrait -{ - /** - * Returns previous element for given element - * - * @param mixed $element The element to verify - * @return null|mixed - */ - public function getPrevious($element) - { - return Arrays::getPreviousElement($this->elements, $element); - } - - /** - * Returns next element for given element - * - * @param mixed $element The element to verify - * @return null|mixed - */ - public function getNext($element) - { - return Arrays::getNextElement($this->elements, $element); - } - - /** - * Returns the first element in the collection - * - * @return mixed - */ - public function getFirst() - { - return Arrays::getFirstElement($this->elements); - } - - /** - * Returns the last element in the collection - * - * @return mixed - */ - public function getLast() - { - return Arrays::getLastElement($this->elements); - } - - /** - * Returns element with given index - * - * @param mixed $index Index / key of the element - * @return null|mixed - */ - public function getByIndex($index) - { - return $this->elements[$index] ?? null; - } -} diff --git a/src/Traits/Collection/IteratorAggregateTrait.php b/src/Traits/Collection/IteratorAggregateTrait.php deleted file mode 100644 index 7430212..0000000 --- a/src/Traits/Collection/IteratorAggregateTrait.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @copyright Meritoo - */ -trait IteratorAggregateTrait -{ - /** - * {@inheritdoc} - */ - public function getIterator(): ArrayIterator - { - return new ArrayIterator($this->elements); - } -} diff --git a/src/Traits/Collection/MainTrait.php b/src/Traits/Collection/MainTrait.php deleted file mode 100644 index 8b08e6c..0000000 --- a/src/Traits/Collection/MainTrait.php +++ /dev/null @@ -1,35 +0,0 @@ - - * @copyright Meritoo - */ -trait MainTrait -{ - /** - * The elements of collection - * - * @var array - */ - private $elements; - - /** - * Returns representation of object as array - * - * @return array - */ - public function toArray(): array - { - return $this->elements; - } -} diff --git a/src/Traits/Collection/ModifyTrait.php b/src/Traits/Collection/ModifyTrait.php deleted file mode 100644 index c08e677..0000000 --- a/src/Traits/Collection/ModifyTrait.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @copyright Meritoo - */ -trait ModifyTrait -{ - /** - * Prepends given element (adds given element at the beginning of collection) - * - * @param mixed $element The element to prepend - */ - public function prepend($element): void - { - array_unshift($this->elements, $element); - } - - /** - * Removes given element - * - * @param mixed $element The element to remove - */ - public function remove($element): void - { - if (0 === $this->count()) { - return; - } - - foreach ($this->elements as $index => $existing) { - if ($element === $existing) { - unset($this->elements[$index]); - - break; - } - } - } -} diff --git a/src/Traits/Collection/VerifyTrait.php b/src/Traits/Collection/VerifyTrait.php deleted file mode 100644 index b88d821..0000000 --- a/src/Traits/Collection/VerifyTrait.php +++ /dev/null @@ -1,65 +0,0 @@ - - * @copyright Meritoo - */ -trait VerifyTrait -{ - /** - * Returns information if collection is empty - * - * @return bool - */ - public function isEmpty(): bool - { - return empty($this->elements); - } - - /** - * Returns information if given element is first in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function isFirst($element): bool - { - return reset($this->elements) === $element; - } - - /** - * Returns information if given element is last in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function isLast($element): bool - { - return end($this->elements) === $element; - } - - /** - * Returns information if the collection has given element, iow. if given element exists in the collection - * - * @param mixed $element The element to verify - * @return bool - */ - public function has($element): bool - { - $index = Arrays::getIndexOf($this->elements, $element); - - return null !== $index && false !== $index; - } -} diff --git a/src/Traits/CollectionTrait.php b/src/Traits/CollectionTrait.php deleted file mode 100644 index 4ffafa1..0000000 --- a/src/Traits/CollectionTrait.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @copyright Meritoo - */ -trait CollectionTrait -{ - use MainTrait; - use AddTrait; - use ModifyTrait; - use GetTrait; - use VerifyTrait; - use CountableTrait; - use ArrayAccessTrait; - use IteratorAggregateTrait; -}