From e75854feee46685ff7918df75b7edadbbd63e306 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 10 May 2019 23:47:48 +0200 Subject: [PATCH] Collection > trait > split into smaller traits (to make it more flexible) --- CHANGELOG.md | 1 + src/Traits/Collection/AddTrait.php | 63 +++++++++ src/Traits/Collection/GetTrait.php | 73 +++++++++++ src/Traits/Collection/MainTrait.php | 182 -------------------------- src/Traits/Collection/ModifyTrait.php | 52 ++++++++ src/Traits/Collection/VerifyTrait.php | 65 +++++++++ src/Traits/CollectionTrait.php | 8 ++ 7 files changed, 262 insertions(+), 182 deletions(-) create mode 100644 src/Traits/Collection/AddTrait.php create mode 100644 src/Traits/Collection/GetTrait.php create mode 100644 src/Traits/Collection/ModifyTrait.php create mode 100644 src/Traits/Collection/VerifyTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2987754..69edba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Common and useful classes, methods, exceptions etc. 1. PHP Coding Standards Fixer > update configuration 2. Phing > tests > add task for Psalm (https://psalm.dev) +3. Collection > trait > split into smaller traits (to make it more flexible) # 1.0.3 diff --git a/src/Traits/Collection/AddTrait.php b/src/Traits/Collection/AddTrait.php new file mode 100644 index 0000000..b51a921 --- /dev/null +++ b/src/Traits/Collection/AddTrait.php @@ -0,0 +1,63 @@ + + * @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 + * @return $this + */ + public function add($element, $index = null): self + { + if (null === $index || '' === $index) { + $this->elements[] = $element; + } else { + $this->elements[$index] = $element; + } + + return $this; + } + + /** + * Adds given elements (at the end of collection) + * + * @param array|Collection $elements The elements to add + * @param bool|false $useIndexes (optional) If is set to true, indexes of given elements will be used in + * this collection. Otherwise - not. + * @return $this + */ + public function addMultiple($elements, bool $useIndexes = false): self + { + if (!empty($elements)) { + foreach ($elements as $index => $element) { + if ($useIndexes) { + $this->add($element, $index); + + continue; + } + + $this->add($element); + } + } + + return $this; + } +} diff --git a/src/Traits/Collection/GetTrait.php b/src/Traits/Collection/GetTrait.php new file mode 100644 index 0000000..4cba31b --- /dev/null +++ b/src/Traits/Collection/GetTrait.php @@ -0,0 +1,73 @@ + + * @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/MainTrait.php b/src/Traits/Collection/MainTrait.php index fae3153..8b08e6c 100644 --- a/src/Traits/Collection/MainTrait.php +++ b/src/Traits/Collection/MainTrait.php @@ -8,9 +8,6 @@ namespace Meritoo\Common\Traits\Collection; -use Meritoo\Common\Collection\Collection; -use Meritoo\Common\Utilities\Arrays; - /** * Main trait for the Collection * @@ -26,185 +23,6 @@ trait MainTrait */ private $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 - * @return $this - */ - public function add($element, $index = null): self - { - if (null === $index || '' === $index) { - $this->elements[] = $element; - } else { - $this->elements[$index] = $element; - } - - return $this; - } - - /** - * Adds given elements (at the end of collection) - * - * @param array|Collection $elements The elements to add - * @param bool|false $useIndexes (optional) If is set to true, indexes of given elements will be used in - * this collection. Otherwise - not. - * @return $this - */ - public function addMultiple($elements, bool $useIndexes = false): self - { - if (!empty($elements)) { - foreach ($elements as $index => $element) { - if ($useIndexes) { - $this->add($element, $index); - - continue; - } - - $this->add($element); - } - } - - return $this; - } - - /** - * Prepends given element (adds given element at the beginning of collection) - * - * @param mixed $element The element to prepend - * @return $this - */ - public function prepend($element): self - { - array_unshift($this->elements, $element); - - return $this; - } - - /** - * Removes given element - * - * @param mixed $element The element to remove - * @return $this - */ - public function remove($element): self - { - if ($this->count() > 0) { - foreach ($this->elements as $index => $existing) { - if ($element === $existing) { - unset($this->elements[$index]); - - break; - } - } - } - - return $this; - } - - /** - * 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; - } - - /** - * 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) - { - if (isset($this->elements[$index])) { - return $this->elements[$index]; - } - - return null; - } - /** * Returns representation of object as array * diff --git a/src/Traits/Collection/ModifyTrait.php b/src/Traits/Collection/ModifyTrait.php new file mode 100644 index 0000000..958e4de --- /dev/null +++ b/src/Traits/Collection/ModifyTrait.php @@ -0,0 +1,52 @@ + + * @copyright Meritoo + */ +trait ModifyTrait +{ + /** + * Prepends given element (adds given element at the beginning of collection) + * + * @param mixed $element The element to prepend + * @return $this + */ + public function prepend($element): self + { + array_unshift($this->elements, $element); + + return $this; + } + + /** + * Removes given element + * + * @param mixed $element The element to remove + * @return $this + */ + public function remove($element): self + { + if ($this->count() > 0) { + foreach ($this->elements as $index => $existing) { + if ($element === $existing) { + unset($this->elements[$index]); + + break; + } + } + } + + return $this; + } +} diff --git a/src/Traits/Collection/VerifyTrait.php b/src/Traits/Collection/VerifyTrait.php new file mode 100644 index 0000000..b88d821 --- /dev/null +++ b/src/Traits/Collection/VerifyTrait.php @@ -0,0 +1,65 @@ + + * @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 index 8cb0cad..d7ac305 100644 --- a/src/Traits/CollectionTrait.php +++ b/src/Traits/CollectionTrait.php @@ -8,10 +8,14 @@ namespace Meritoo\Common\Traits; +use Meritoo\Common\Traits\Collection\AddTrait; use Meritoo\Common\Traits\Collection\ArrayAccessTrait; use Meritoo\Common\Traits\Collection\CountableTrait; +use Meritoo\Common\Traits\Collection\GetTrait; use Meritoo\Common\Traits\Collection\IteratorAggregateTrait; use Meritoo\Common\Traits\Collection\MainTrait; +use Meritoo\Common\Traits\Collection\ModifyTrait; +use Meritoo\Common\Traits\Collection\VerifyTrait; /** * Trait for the Collection @@ -22,6 +26,10 @@ use Meritoo\Common\Traits\Collection\MainTrait; trait CollectionTrait { use MainTrait; + use AddTrait; + use ModifyTrait; + use GetTrait; + use VerifyTrait; use CountableTrait; use ArrayAccessTrait; use IteratorAggregateTrait;