Skip to content

Commit 5c07a42

Browse files
committed
Create a static range method on the collection
1 parent b2b1f99 commit 5c07a42

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Support/Collection.php

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public static function make($items = [])
5858
return new static($items);
5959
}
6060

61+
/**
62+
* Create a new collection by invoking the callback a given amount of times.
63+
*
64+
* @param int $amount
65+
* @param callable $callback
66+
* @return static
67+
*/
68+
public static function times($amount, callable $callback)
69+
{
70+
return (new static(range(1, $amount)))->map($callback);
71+
}
72+
6173
/**
6274
* Get all of the items in the collection.
6375
*

tests/Support/SupportCollectionTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,13 @@ public function testMakeMethodFromArray()
936936
$this->assertEquals(['foo' => 'bar'], $collection->all());
937937
}
938938

939+
public function testTimesMethod()
940+
{
941+
$this->assertEquals(['slug-1', 'slug-2'], Collection::range(2, function ($number) {
942+
return 'slug-'.$number;
943+
})->all());
944+
}
945+
939946
public function testConstructMakeFromObject()
940947
{
941948
$object = new stdClass();

0 commit comments

Comments
 (0)