diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php index a5d0a38..972dcf1 100644 --- a/src/Collections/Collection.php +++ b/src/Collections/Collection.php @@ -23,6 +23,7 @@ class Collection extends ImmutableCollection implements ICollection { public function merge (IImmutableCollection ...$collections): ICollection { return $this->_merge(...$collections); } + /** * @inheritDoc */ @@ -36,6 +37,25 @@ class Collection extends ImmutableCollection implements ICollection { return $this->_addCollection(...$collections); } + /** + * @inheritDoc + */ + public function prepend (...$values): ICollection { + array_unshift($this->elements, ...$values); + return $this; + } + /** + * @inheritDoc + */ + public function prependCollection (IImmutableCollection ...$collections): ICollection { + foreach ($collections as $collection) { + foreach ($collection as $value) { + $this->prepend($value); + } + } + return $this; + } + /** * @inheritDoc */ diff --git a/src/Collections/ICollection.php b/src/Collections/ICollection.php index da749d0..857e62c 100644 --- a/src/Collections/ICollection.php +++ b/src/Collections/ICollection.php @@ -30,6 +30,7 @@ interface ICollection extends IImmutableCollection { * @return $this */ public function merge (IImmutableCollection ...$collections): self; + /** * Add one or multiple values * @@ -49,6 +50,25 @@ interface ICollection extends IImmutableCollection { */ public function addCollection (IImmutableCollection ...$collections): self; + /** + * Add one or multiple values at the beginning of the current collection + * + * @param mixed ...$values The values + * + * @psalm-param TValue ...$values + * + * @return $this + */ + public function prepend (...$values): self; + /** + * Add the values of one or multiple collections at the beginning of the current collection + * + * @param IImmutableCollection ...$collections The collections to add + * + * @return mixed + */ + public function prependCollection (IImmutableCollection ...$collections): self; + /** * Empties the collection * diff --git a/tests/test_class.php b/tests/test_class.php index d9614bb..48a2afd 100644 --- a/tests/test_class.php +++ b/tests/test_class.php @@ -24,8 +24,8 @@ class Measure { */ function getMeasures (): Collection { $measures = new Collection(); - $measures->add(new Measure(38)); $measures->add(new Measure(27), new Measure(34)); + $measures->prepend(new Measure(38)); return $measures; }