Skip to content

Commit 07ea288

Browse files
CupOfTea696taylorotwell
authored andcommitted
Make the Collection::times callback optional (#19278)
* Make the Collection::times callback optional * Add test * Update Collection.php
1 parent 7c17bf5 commit 07ea288

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ public static function make($items = [])
6565
* @param callable $callback
6666
* @return static
6767
*/
68-
public static function times($amount, callable $callback)
68+
public static function times($amount, callable $callback = null)
6969
{
7070
if ($amount < 1) {
7171
return new static;
7272
}
7373

74+
if (is_null($callback)) {
75+
return new static(range(1, $amount));
76+
}
77+
7478
return (new static(range(1, $amount)))->map($callback);
7579
}
7680

tests/Support/SupportCollectionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,12 @@ public function testTimesMethod()
998998
return 'slug-'.$number;
999999
});
10001000

1001+
$range = Collection::times(5);
1002+
10011003
$this->assertEquals(['slug-1', 'slug-2'], $two->all());
10021004
$this->assertTrue($zero->isEmpty());
10031005
$this->assertTrue($negative->isEmpty());
1006+
$this->assertEquals(range(1, 5), $range->all());
10041007
}
10051008

10061009
public function testConstructMakeFromObject()

0 commit comments

Comments
 (0)