Skip to content

Commit 1bc80d4

Browse files
committed
Add until to HigherOrder and LazyCollection
1 parent 127c916 commit 1bc80d4

File tree

3 files changed

+65
-33
lines changed

3 files changed

+65
-33
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,30 +1333,4 @@ public function offsetUnset($key)
13331333
{
13341334
unset($this->items[$key]);
13351335
}
1336-
1337-
/**
1338-
* Take items in the collection until condition is met.
1339-
*
1340-
* @param mixed $key
1341-
* @return static
1342-
*/
1343-
public function until($value)
1344-
{
1345-
$passed = [];
1346-
1347-
$callback = $this->useAsCallable($value) ? $value :
1348-
function ($item) use ($value) {
1349-
return $item === $value;
1350-
};
1351-
1352-
foreach ($this as $key => $item) {
1353-
if ($callback($item, $key)) {
1354-
break;
1355-
}
1356-
1357-
$passed[$key] = $item;
1358-
}
1359-
1360-
return new static($passed);
1361-
}
13621336
}

src/Illuminate/Support/Traits/EnumeratesValues.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @property-read HigherOrderCollectionProxy $sortByDesc
3737
* @property-read HigherOrderCollectionProxy $sum
3838
* @property-read HigherOrderCollectionProxy $unique
39+
* @property-read HigherOrderCollectionProxy $until
3940
*/
4041
trait EnumeratesValues
4142
{
@@ -47,7 +48,7 @@ trait EnumeratesValues
4748
protected static $proxies = [
4849
'average', 'avg', 'contains', 'each', 'every', 'filter', 'first',
4950
'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition',
50-
'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique',
51+
'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', 'until',
5152
];
5253

5354
/**
@@ -703,6 +704,32 @@ public function uniqueStrict($key = null)
703704
return $this->unique($key, true);
704705
}
705706

707+
/**
708+
* Take items in the collection until condition is met.
709+
*
710+
* @param mixed $key
711+
* @return static
712+
*/
713+
public function until($value)
714+
{
715+
$passed = [];
716+
717+
$callback = $this->useAsCallable($value) ? $value :
718+
function ($item) use ($value) {
719+
return $item === $value;
720+
};
721+
722+
foreach ($this as $key => $item) {
723+
if ($callback($item, $key)) {
724+
break;
725+
}
726+
727+
$passed[$key] = $item;
728+
}
729+
730+
return new static($passed);
731+
}
732+
706733
/**
707734
* Collect the values into a collection.
708735
*

tests/Support/SupportCollectionTest.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,18 +4070,24 @@ public function testCollect($collection)
40704070
], $data->all());
40714071
}
40724072

4073-
public function testUntilUsingValue()
4073+
/**
4074+
* @dataProvider collectionClassProvider
4075+
*/
4076+
public function testUntilUsingValue($collection)
40744077
{
4075-
$data = new Collection([1, 2, 3, 4]);
4078+
$data = new $collection([1, 2, 3, 4]);
40764079

40774080
$data = $data->until(3);
40784081

40794082
$this->assertSame([1, 2], $data->toArray());
40804083
}
40814084

4082-
public function testUntilUsingCallback()
4085+
/**
4086+
* @dataProvider collectionClassProvider
4087+
*/
4088+
public function testUntilUsingCallback($collection)
40834089
{
4084-
$data = new Collection([1, 2, 3, 4]);
4090+
$data = new $collection([1, 2, 3, 4]);
40854091

40864092
$data = $data->until(function ($item) {
40874093
return $item >= 3;
@@ -4090,9 +4096,12 @@ public function testUntilUsingCallback()
40904096
$this->assertSame([1, 2], $data->toArray());
40914097
}
40924098

4093-
public function testUntilReturnsAllItemsForUnmetValue()
4099+
/**
4100+
* @dataProvider collectionClassProvider
4101+
*/
4102+
public function testUntilReturnsAllItemsForUnmetValue($collection)
40944103
{
4095-
$data = new Collection([1, 2, 3, 4]);
4104+
$data = new $collection([1, 2, 3, 4]);
40964105

40974106
$actual = $data->until(99);
40984107

@@ -4105,6 +4114,24 @@ public function testUntilReturnsAllItemsForUnmetValue()
41054114
$this->assertSame($data->toArray(), $actual->toArray());
41064115
}
41074116

4117+
/**
4118+
* @dataProvider collectionClassProvider
4119+
*/
4120+
public function testUntilCanBeProxied($collection)
4121+
{
4122+
$data = new $collection([
4123+
new TestSupportCollectionHigherOrderItem('Adam'),
4124+
new TestSupportCollectionHigherOrderItem('Taylor'),
4125+
new TestSupportCollectionHigherOrderItem('Jason'),
4126+
]);
4127+
4128+
$actual = $data->until->is('Jason');
4129+
4130+
$this->assertCount(2, $actual);
4131+
$this->assertSame('Adam', $actual->get(0)->name);
4132+
$this->assertSame('Taylor', $actual->get(1)->name);
4133+
}
4134+
41084135
/**
41094136
* Provides each collection class, respectively.
41104137
*
@@ -4132,6 +4159,10 @@ public function uppercase()
41324159
{
41334160
return $this->name = strtoupper($this->name);
41344161
}
4162+
4163+
public function is($name) {
4164+
return $this->name === $name;
4165+
}
41354166
}
41364167

41374168
class TestAccessorEloquentTestStub

0 commit comments

Comments
 (0)