From 3da6845b0503ddd3744e44457124e715312a910d Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 15 Nov 2022 17:36:53 +0100 Subject: [PATCH] Correction + test Collection::prependCollection --- src/Collections/Collection.php | 15 +++++++++------ tests/test_class.php | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php index 972dcf1..509a167 100644 --- a/src/Collections/Collection.php +++ b/src/Collections/Collection.php @@ -48,12 +48,15 @@ class Collection extends ImmutableCollection implements ICollection { * @inheritDoc */ public function prependCollection (IImmutableCollection ...$collections): ICollection { - foreach ($collections as $collection) { - foreach ($collection as $value) { - $this->prepend($value); - } - } - return $this; + $prepend = array_merge( + ...array_map( + function (IImmutableCollection $collection): array { + return $collection->values()->toArray(); + }, + $collections + ) + ); + return $this->prepend(...$prepend); } /** diff --git a/tests/test_class.php b/tests/test_class.php index 48a2afd..77cbd71 100644 --- a/tests/test_class.php +++ b/tests/test_class.php @@ -23,9 +23,12 @@ class Measure { * @return Collection */ function getMeasures (): Collection { + $mesures_avant = new Collection(); + $mesures_avant->add(new Measure(38), new Measure(29)); + $measures = new Collection(); $measures->add(new Measure(27), new Measure(34)); - $measures->prepend(new Measure(38)); + $measures->prependCollection($mesures_avant); return $measures; }