Skip to content

Commit b155ee2

Browse files
Merge branch '8.x' into master
2 parents 05f6a07 + 446310d commit b155ee2

File tree

4 files changed

+58
-48
lines changed

4 files changed

+58
-48
lines changed

Collection.php

+5-13
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,15 @@ public function __construct($items = [])
3131
}
3232

3333
/**
34-
* Create a new collection by invoking the callback a given amount of times.
34+
* Create a collection with the given range.
3535
*
36-
* @param int $number
37-
* @param callable|null $callback
36+
* @param int $from
37+
* @param int $to
3838
* @return static
3939
*/
40-
public static function times($number, callable $callback = null)
40+
public static function range($from, $to)
4141
{
42-
if ($number < 1) {
43-
return new static;
44-
}
45-
46-
if (is_null($callback)) {
47-
return new static(range(1, $number));
48-
}
49-
50-
return (new static(range(1, $number)))->map($callback);
42+
return new static(range($from, $to));
5143
}
5244

5345
/**

Enumerable.php

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static function make($items = []);
2727
*/
2828
public static function times($number, callable $callback = null);
2929

30+
/**
31+
* Create a collection with the given range.
32+
*
33+
* @param int $from
34+
* @param int $to
35+
* @return static
36+
*/
37+
public static function range($from, $to);
38+
3039
/**
3140
* Wrap the given value in a collection if applicable.
3241
*
@@ -43,6 +52,13 @@ public static function wrap($value);
4352
*/
4453
public static function unwrap($value);
4554

55+
/**
56+
* Create a new instance with no items.
57+
*
58+
* @return static
59+
*/
60+
public static function empty();
61+
4662
/**
4763
* Get all items in the enumerable.
4864
*

LazyCollection.php

+9-35
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,7 @@ public function __construct($source = null)
3838
}
3939

4040
/**
41-
* Create a new instance with no items.
42-
*
43-
* @return static
44-
*/
45-
public static function empty()
46-
{
47-
return new static([]);
48-
}
49-
50-
/**
51-
* Create a new instance by invoking the callback a given amount of times.
52-
*
53-
* @param int $number
54-
* @param callable|null $callback
55-
* @return static
56-
*/
57-
public static function times($number, callable $callback = null)
58-
{
59-
if ($number < 1) {
60-
return new static;
61-
}
62-
63-
$instance = new static(function () use ($number) {
64-
for ($current = 1; $current <= $number; $current++) {
65-
yield $current;
66-
}
67-
});
68-
69-
return is_null($callback) ? $instance : $instance->map($callback);
70-
}
71-
72-
/**
73-
* Create an enumerable with the given range.
41+
* Create a collection with the given range.
7442
*
7543
* @param int $from
7644
* @param int $to
@@ -79,8 +47,14 @@ public static function times($number, callable $callback = null)
7947
public static function range($from, $to)
8048
{
8149
return new static(function () use ($from, $to) {
82-
for (; $from <= $to; $from++) {
83-
yield $from;
50+
if ($from <= $to) {
51+
for (; $from <= $to; $from++) {
52+
yield $from;
53+
}
54+
} else {
55+
for (; $from >= $to; $from--) {
56+
yield $from;
57+
}
8458
}
8559
});
8660
}

Traits/EnumeratesValues.php

+28
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ public static function unwrap($value)
109109
return $value instanceof Enumerable ? $value->all() : $value;
110110
}
111111

112+
/**
113+
* Create a new instance with no items.
114+
*
115+
* @return static
116+
*/
117+
public static function empty()
118+
{
119+
return new static([]);
120+
}
121+
122+
/**
123+
* Create a new collection by invoking the callback a given amount of times.
124+
*
125+
* @param int $number
126+
* @param callable|null $callback
127+
* @return static
128+
*/
129+
public static function times($number, callable $callback = null)
130+
{
131+
if ($number < 1) {
132+
return new static;
133+
}
134+
135+
return static::range(1, $number)
136+
->when($callback)
137+
->map($callback);
138+
}
139+
112140
/**
113141
* Alias for the "avg" method.
114142
*

0 commit comments

Comments
 (0)