Skip to content

Commit

Permalink
Add until to HigherOrder and LazyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Apr 7, 2020
1 parent 127c916 commit 54f2137
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
26 changes: 0 additions & 26 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,30 +1333,4 @@ public function offsetUnset($key)
{
unset($this->items[$key]);
}

/**
* Take items in the collection until condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
$passed = [];

$callback = $this->useAsCallable($value) ? $value :
function ($item) use ($value) {
return $item === $value;
};

foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

$passed[$key] = $item;
}

return new static($passed);
}
}
29 changes: 28 additions & 1 deletion src/Illuminate/Support/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @property-read HigherOrderCollectionProxy $sortByDesc
* @property-read HigherOrderCollectionProxy $sum
* @property-read HigherOrderCollectionProxy $unique
* @property-read HigherOrderCollectionProxy $until
*/
trait EnumeratesValues
{
Expand All @@ -47,7 +48,7 @@ trait EnumeratesValues
protected static $proxies = [
'average', 'avg', 'contains', 'each', 'every', 'filter', 'first',
'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition',
'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique',
'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', 'until',
];

/**
Expand Down Expand Up @@ -703,6 +704,32 @@ public function uniqueStrict($key = null)
return $this->unique($key, true);
}

/**
* Take items in the collection until condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
$passed = [];

$callback = $this->useAsCallable($value) ? $value :
function ($item) use ($value) {
return $item === $value;
};

foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

$passed[$key] = $item;
}

return new static($passed);
}

/**
* Collect the values into a collection.
*
Expand Down
44 changes: 38 additions & 6 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4070,18 +4070,24 @@ public function testCollect($collection)
], $data->all());
}

public function testUntilUsingValue()
/**
* @dataProvider collectionClassProvider
*/
public function testUntilUsingValue($collection)
{
$data = new Collection([1, 2, 3, 4]);
$data = new $collection([1, 2, 3, 4]);

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

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

public function testUntilUsingCallback()
/**
* @dataProvider collectionClassProvider
*/
public function testUntilUsingCallback($collection)
{
$data = new Collection([1, 2, 3, 4]);
$data = new $collection([1, 2, 3, 4]);

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

public function testUntilReturnsAllItemsForUnmetValue()
/**
* @dataProvider collectionClassProvider
*/
public function testUntilReturnsAllItemsForUnmetValue($collection)
{
$data = new Collection([1, 2, 3, 4]);
$data = new $collection([1, 2, 3, 4]);

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

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

/**
* @dataProvider collectionClassProvider
*/
public function testUntilCanBeProxied($collection)
{
$data = new $collection([
new TestSupportCollectionHigherOrderItem('Adam'),
new TestSupportCollectionHigherOrderItem('Taylor'),
new TestSupportCollectionHigherOrderItem('Jason'),
]);

$actual = $data->until->is('Jason');

$this->assertCount(2, $actual);
$this->assertSame('Adam', $actual->get(0)->name);
$this->assertSame('Taylor', $actual->get(1)->name);
}

/**
* Provides each collection class, respectively.
*
Expand Down Expand Up @@ -4132,6 +4159,11 @@ public function uppercase()
{
return $this->name = strtoupper($this->name);
}

public function is($name)
{
return $this->name === $name;
}
}

class TestAccessorEloquentTestStub
Expand Down

0 comments on commit 54f2137

Please sign in to comment.