Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
67 changes: 67 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4070,6 +4070,68 @@ public function testCollect($collection)
], $data->all());
}

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

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

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

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

$data = $data->until(function ($item) {
return $item >= 3;
});

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

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

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

$this->assertSame($data->toArray(), $actual->toArray());

$actual = $data->until(function ($item) {
return $item >= 99;
});

$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 @@ -4097,6 +4159,11 @@ public function uppercase()
{
return $this->name = strtoupper($this->name);
}

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

class TestAccessorEloquentTestStub
Expand Down