From 33424bef66b1b16dbf6323f6f3dd75e44d488bc2 Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Sat, 22 Jun 2024 01:52:51 +1000 Subject: [PATCH] [11.x] Add multiply to collection (#51870) * Add multiply to collection Multiply the items in the collection by the multiplier. * Update Collection.php * Update LazyCollection.php --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Collections/Collection.php | 17 +++++++++++++++++ src/Illuminate/Collections/LazyCollection.php | 11 +++++++++++ .../DatabaseEloquentCollectionTest.php | 15 +++++++++++++++ tests/Support/SupportCollectionTest.php | 19 +++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 207022e8b3f4..7c3b72f29cae 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -831,6 +831,23 @@ public function mergeRecursive($items) return new static(array_merge_recursive($this->items, $this->getArrayableItems($items))); } + /** + * Multiply the items in the collection by the multiplier. + * + * @param int $multiplier + * @return static + */ + public function multiply(int $multiplier) + { + $new = new static; + + for ($i = 0; $i < $multiplier; $i++) { + $new->push(...$this->items); + } + + return $new; + } + /** * Create a collection by using this collection for keys and another for its values. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 958daf8a7508..374c300021f6 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -841,6 +841,17 @@ public function mergeRecursive($items) return $this->passthru('mergeRecursive', func_get_args()); } + /** + * Multiply the items in the collection by the multiplier. + * + * @param int $multiplier + * @return static + */ + public function multiply(int $multiplier) + { + return $this->passthru('multiply', func_get_args()); + } + /** * Create a collection by using this collection for keys and another for its values. * diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 1cafbbc3cc83..d5e9d64652d3 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -510,6 +510,21 @@ public function testMakeVisibleRemovesHiddenAndIncludesVisible() $this->assertEquals(['visible', 'hidden'], $c[0]->getVisible()); } + public function testMultiply() + { + $a = new TestEloquentCollectionModel(); + $b = new TestEloquentCollectionModel(); + + $c = new Collection([$a, $b]); + + $this->assertEquals([], $c->multiply(-1)->all()); + $this->assertEquals([], $c->multiply(0)->all()); + + $this->assertEquals([$a, $b], $c->multiply(1)->all()); + + $this->assertEquals([$a, $b, $a, $b, $a, $b], $c->multiply(3)->all()); + } + public function testQueueableCollectionImplementation() { $c = new Collection([new TestEloquentCollectionModel, new TestEloquentCollectionModel]); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index c345699818ad..f8b0b9d02aa6 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1259,6 +1259,25 @@ public function testMergeRecursiveCollection($collection) ); } + #[DataProvider('collectionClassProvider')] + public function testMultiplyCollection($collection) + { + $c = new $collection(['Hello', 1, ['tags' => ['a', 'b'], 'admin']]); + + $this->assertEquals([], $c->multiply(-1)->all()); + $this->assertEquals([], $c->multiply(0)->all()); + + $this->assertEquals( + ['Hello', 1, ['tags' => ['a', 'b'], 'admin']], + $c->multiply(1)->all() + ); + + $this->assertEquals( + ['Hello', 1, ['tags' => ['a', 'b'], 'admin'], 'Hello', 1, ['tags' => ['a', 'b'], 'admin'], 'Hello', 1, ['tags' => ['a', 'b'], 'admin']], + $c->multiply(3)->all() + ); + } + #[DataProvider('collectionClassProvider')] public function testReplaceNull($collection) {