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
33 changes: 22 additions & 11 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,17 +736,6 @@ public function union($items)
return new static($this->items + $this->getArrayableItems($items));
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
return new static($this->lazy()->until($value)->all());
}

/**
* Create a new collection consisting of every n-th element.
*
Expand Down Expand Up @@ -1196,6 +1185,28 @@ public function take($limit)
return $this->slice(0, $limit);
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function takeUntil($value)
{
return new static($this->lazy()->takeUntil($value)->all());
}

/**
* Take items in the collection while the given condition is met.
*
* @param mixed $key
* @return static
*/
public function takeWhile($value)
{
return new static($this->lazy()->takeWhile($value)->all());
}

/**
* Transform each item in the collection using a callback.
*
Expand Down
57 changes: 36 additions & 21 deletions src/Illuminate/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,27 +737,6 @@ public function union($items)
return $this->passthru('union', func_get_args());
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);

return new static(function () use ($callback) {
foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

yield $key => $item;
}
});
}

/**
* Create a new collection consisting of every n-th element.
*
Expand Down Expand Up @@ -1135,6 +1114,42 @@ public function take($limit)
});
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function takeUntil($value)
{
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);

return new static(function () use ($callback) {
foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

yield $key => $item;
}
});
}

/**
* Take items in the collection while the given condition is met.
*
* @param mixed $key
* @return static
*/
public function takeWhile($value)
{
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);

return $this->takeUntil(function ($item, $key) use ($callback) {
return ! $callback($item, $key);
});
}

/**
* Pass each item in the collection to the given callback, lazily.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Support/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ trait EnumeratesValues
'sortBy',
'sortByDesc',
'sum',
'takeUntil',
'takeWhile',
'unique',
'until',
];
Expand Down Expand Up @@ -722,6 +724,19 @@ public function uniqueStrict($key = null)
return $this->unique($key, true);
}

/**
* Take items in the collection until the given condition is met.
*
* This is an alias to the "takeUntil" method.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
return $this->takeUntil($value);
}

/**
* Collect the values into a collection.
*
Expand Down
187 changes: 125 additions & 62 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,131 @@ public function testTakeLast($collection)
$this->assertEquals([1 => 'dayle', 2 => 'shawn'], $data->all());
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

$data = $data->takeWhile(1);

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

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

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

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

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

$actual = $data->takeWhile(2);

$this->assertSame([], $actual->toArray());

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

$this->assertSame([], $actual->toArray());
}

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

$actual = $data->takeWhile->is('Adam');

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

/**
* @dataProvider collectionClassProvider
*/
Expand Down Expand Up @@ -4094,68 +4219,6 @@ 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
Loading