From ae35ccf91c994482c4ab6c715a7ebcfddbe9deaf Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Fri, 21 Jun 2024 15:21:36 +1000 Subject: [PATCH 1/3] Add multiply to collection Multiply the items in the collection by the multiplier. --- 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 ee48d37daff0..8498d8a773cc 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -830,6 +830,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 $multiplier + * @return static + */ + public function multiply($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..5f89c6b94c20 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 $multiplier + * @return static + */ + public function multiply($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 0b1a0f6fcb67..2c691fef1521 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1231,6 +1231,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) { From 0d9ca3cd9c66ab6f92abbd21b16c9bc48a7612bf Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 21 Jun 2024 10:47:50 -0500 Subject: [PATCH 2/3] Update Collection.php --- src/Illuminate/Collections/Collection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 8498d8a773cc..b83ab66d7695 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -833,12 +833,12 @@ public function mergeRecursive($items) /** * Multiply the items in the collection by the multiplier. * - * @param $multiplier + * @param int $multiplier * @return static */ - public function multiply($multiplier) + public function multiply(int $multiplier) { - $new = new static(); + $new = new static; for ($i = 0; $i < $multiplier; $i++) { $new->push(...$this->items); From 3efb9d202e096f85c7608c3836fecb7c53e944d0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 21 Jun 2024 10:48:24 -0500 Subject: [PATCH 3/3] Update LazyCollection.php --- src/Illuminate/Collections/LazyCollection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 5f89c6b94c20..374c300021f6 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -844,10 +844,10 @@ public function mergeRecursive($items) /** * Multiply the items in the collection by the multiplier. * - * @param $multiplier + * @param int $multiplier * @return static */ - public function multiply($multiplier) + public function multiply(int $multiplier) { return $this->passthru('multiply', func_get_args()); }