From 89f89d8c19540a3ceb1700e7c4b49df2780b531e Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 15 Nov 2022 15:32:58 +0100 Subject: [PATCH] Allow addition of multiple values and collection merging/combining --- src/Collections/Collection.php | 17 +++++++-- src/Collections/ICollection.php | 24 ++++++++++-- src/Collections/IImmutableCollection.php | 9 +++++ src/Collections/ImmutableCollection.php | 47 ++++++++++++++++++++++-- tests/test_class.php | 3 +- 5 files changed, 87 insertions(+), 13 deletions(-) diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php index a48946d..a5d0a38 100644 --- a/src/Collections/Collection.php +++ b/src/Collections/Collection.php @@ -20,9 +20,20 @@ class Collection extends ImmutableCollection implements ICollection { /** * @inheritDoc */ - public function add ($value): self { - $this->elements[] = $value; - return $this; + public function merge (IImmutableCollection ...$collections): ICollection { + return $this->_merge(...$collections); + } + /** + * @inheritDoc + */ + public function add (...$values): self { + return $this->_add(...$values); + } + /** + * @inheritDoc + */ + public function addCollection (IImmutableCollection ...$collections): ICollection { + return $this->_addCollection(...$collections); } /** diff --git a/src/Collections/ICollection.php b/src/Collections/ICollection.php index b0dff6d..da749d0 100644 --- a/src/Collections/ICollection.php +++ b/src/Collections/ICollection.php @@ -23,15 +23,31 @@ interface ICollection extends IImmutableCollection { */ public function set ($key, $value): self; /** - * Add a value + * Merge one or multiple collections in the current one * - * @param mixed $value The value + * @param IImmutableCollection ...$collections The collections to merge * - * @psalm-param TValue $value + * @return $this + */ + public function merge (IImmutableCollection ...$collections): self; + /** + * Add one or multiple values + * + * @param mixed ...$values The values + * + * @psalm-param TValue ...$values * * @return $this */ - public function add ($value): self; + public function add (...$values): self; + /** + * Add the values of one or multiple collections + * + * @param IImmutableCollection ...$collections The collections to add + * + * @return mixed + */ + public function addCollection (IImmutableCollection ...$collections): self; /** * Empties the collection diff --git a/src/Collections/IImmutableCollection.php b/src/Collections/IImmutableCollection.php index ba58eb3..45a74ea 100644 --- a/src/Collections/IImmutableCollection.php +++ b/src/Collections/IImmutableCollection.php @@ -158,4 +158,13 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri * @return static */ public static function split (string $value, string $separator): self; + + /** + * Combine the current collection with one or multiple other collections + * + * @param IImmutableCollection ...$collections The other collections to combine + * + * @return static The combine collection + */ + public function combineWith (IImmutableCollection ...$collections): self; } \ No newline at end of file diff --git a/src/Collections/ImmutableCollection.php b/src/Collections/ImmutableCollection.php index 06711f0..e358d87 100644 --- a/src/Collections/ImmutableCollection.php +++ b/src/Collections/ImmutableCollection.php @@ -77,19 +77,51 @@ class ImmutableCollection implements IImmutableCollection { $this->elements[$this->_normalizeKey($key)] = $value; return $this; } + /** + * Internally merge one or multiple collections in the current one + * + * @param IImmutableCollection ...$collections The collections to merge + * + * @return $this + */ + protected function _merge (IImmutableCollection ...$collections): self { + foreach ($collections as $collection) { + foreach ($collection as $key => $value) { + $this->_set($key, $value); + } + } + return $this; + } /** * Internally add a value * - * @param mixed $value The value + * @param mixed ...$values The value * - * @psalm-param TValue $value + * @psalm-param TValue ...$value * * @return $this * @internal * */ - protected function _add ($value): self { - $this->elements[] = $value; + protected function _add (...$values): self { + foreach ($values as $value) { + $this->elements[] = $value; + } + return $this; + } + /** + * Internally add the values of one or multiple collections + * + * @param IImmutableCollection ...$collections The collections to add + * + * @return mixed + */ + protected function _addCollection (IImmutableCollection ...$collections): self { + foreach ($collections as $collection) { + foreach ($collection as $value) { + $this->_add($value); + } + } return $this; } /** @@ -296,4 +328,11 @@ class ImmutableCollection implements IImmutableCollection { public static function split (string $value, string $separator): self { return new static (explode($separator, $value)); } + + /** + * @inheritDoc + */ + public function combineWith (IImmutableCollection ...$collections): IImmutableCollection { + return (clone $this)->_merge(...$collections); + } } \ No newline at end of file diff --git a/tests/test_class.php b/tests/test_class.php index 15fc6d3..d9614bb 100644 --- a/tests/test_class.php +++ b/tests/test_class.php @@ -25,8 +25,7 @@ class Measure { function getMeasures (): Collection { $measures = new Collection(); $measures->add(new Measure(38)); - $measures->add(new Measure(27)); - $measures->add(new Measure(34)); + $measures->add(new Measure(27), new Measure(34)); return $measures; }