Skip to content

Commit

Permalink
[11.x] Add multiply to collection (laravel#51870)
Browse files Browse the repository at this point in the history
* Add multiply to collection

Multiply the items in the collection by the multiplier.

* Update Collection.php

* Update LazyCollection.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
patrickomeara and taylorotwell authored Jun 21, 2024
1 parent 111f67b commit 33424be
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 33424be

Please sign in to comment.