Skip to content

Commit e983851

Browse files
committed
Merge branch 'until-collection-method' of https://github.com/jasonmccreary/framework into jasonmccreary-until-collection-method
2 parents d3bdde1 + 54f2137 commit e983851

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,6 +4070,68 @@ public function testCollect($collection)
40704070
], $data->all());
40714071
}
40724072

4073+
/**
4074+
* @dataProvider collectionClassProvider
4075+
*/
4076+
public function testUntilUsingValue($collection)
4077+
{
4078+
$data = new $collection([1, 2, 3, 4]);
4079+
4080+
$data = $data->until(3);
4081+
4082+
$this->assertSame([1, 2], $data->toArray());
4083+
}
4084+
4085+
/**
4086+
* @dataProvider collectionClassProvider
4087+
*/
4088+
public function testUntilUsingCallback($collection)
4089+
{
4090+
$data = new $collection([1, 2, 3, 4]);
4091+
4092+
$data = $data->until(function ($item) {
4093+
return $item >= 3;
4094+
});
4095+
4096+
$this->assertSame([1, 2], $data->toArray());
4097+
}
4098+
4099+
/**
4100+
* @dataProvider collectionClassProvider
4101+
*/
4102+
public function testUntilReturnsAllItemsForUnmetValue($collection)
4103+
{
4104+
$data = new $collection([1, 2, 3, 4]);
4105+
4106+
$actual = $data->until(99);
4107+
4108+
$this->assertSame($data->toArray(), $actual->toArray());
4109+
4110+
$actual = $data->until(function ($item) {
4111+
return $item >= 99;
4112+
});
4113+
4114+
$this->assertSame($data->toArray(), $actual->toArray());
4115+
}
4116+
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+
40734135
/**
40744136
* Provides each collection class, respectively.
40754137
*
@@ -4097,6 +4159,11 @@ public function uppercase()
40974159
{
40984160
return $this->name = strtoupper($this->name);
40994161
}
4162+
4163+
public function is($name)
4164+
{
4165+
return $this->name === $name;
4166+
}
41004167
}
41014168

41024169
class TestAccessorEloquentTestStub

0 commit comments

Comments
 (0)