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
20 changes: 3 additions & 17 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,23 +1078,9 @@ public function chunk($size)
*/
public function chunkWhile(callable $callback)
{
$chunks = [];

$chunk = [];

foreach ($this->items as $current) {
if (isset($previous) && ! $callback($previous, $current)) {
$chunks[] = new static($chunk);
$chunk = [];
}

$chunk[] = $current;
$previous = $current;
}

$chunks[] = new static($chunk);

return new static($chunks);
return new static(
$this->lazy()->chunkWhile($callback)->mapInto(static::class)
);
}

/**
Expand Down
19 changes: 13 additions & 6 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,22 +1093,29 @@ public function chunkWhile(callable $callback)
return new static(function () use ($callback) {
$iterator = $this->getIterator();

$chunk = [];
$chunk = new Collection();

if ($iterator->valid()) {
$chunk[$iterator->key()] = $iterator->current();

$iterator->next();
}

while ($iterator->valid()) {
if (isset($previous) && ! $callback($previous, $iterator->current())) {
if (! $callback($iterator->current(), $iterator->key(), $chunk)) {
yield new static($chunk);

$chunk = [];
$chunk = new Collection();
}

$chunk[] = $iterator->current();
$previous = $iterator->current();
$chunk[$iterator->key()] = $iterator->current();

$iterator->next();
}

yield new static($chunk);
if ($chunk->isNotEmpty()) {
yield new static($chunk);
}
});
}

Expand Down
24 changes: 12 additions & 12 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1650,15 +1650,15 @@ public function testChunkWhenGivenLessThanZero($collection)
public function testChunkWhileOnEqualElements($collection)
{
$data = (new $collection(['A', 'A', 'B', 'B', 'C', 'C', 'C']))
->chunkWhile(function ($previous, $current) {
return $previous === $current;
->chunkWhile(function ($current, $key, $chunk) {
return $chunk->last() === $current;
});

$this->assertInstanceOf($collection, $data);
$this->assertInstanceOf($collection, $data->first());
$this->assertEquals(['A', 'A'], $data->first()->toArray());
$this->assertEquals(['B', 'B'], $data->get(1)->toArray());
$this->assertEquals(['C', 'C', 'C'], $data->last()->toArray());
$this->assertEquals([0 => 'A', 1 => 'A'], $data->first()->toArray());
$this->assertEquals([2 => 'B', 3 => 'B'], $data->get(1)->toArray());
$this->assertEquals([4 => 'C', 5 => 'C', 6 => 'C'], $data->last()->toArray());
}

/**
Expand All @@ -1667,17 +1667,17 @@ public function testChunkWhileOnEqualElements($collection)
public function testChunkWhileOnContiguouslyIncreasingIntegers($collection)
{
$data = (new $collection([1, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]))
->chunkWhile(function ($previous, $current) {
return $previous + 1 == $current;
->chunkWhile(function ($current, $key, $chunk) {
return $chunk->last() + 1 == $current;
});

$this->assertInstanceOf($collection, $data);
$this->assertInstanceOf($collection, $data->first());
$this->assertEquals([1], $data->first()->toArray());
$this->assertEquals([4], $data->get(1)->toArray());
$this->assertEquals([9, 10, 11, 12], $data->get(2)->toArray());
$this->assertEquals([15, 16], $data->get(3)->toArray());
$this->assertEquals([19, 20, 21], $data->last()->toArray());
$this->assertEquals([0 => 1], $data->first()->toArray());
$this->assertEquals([1 => 4], $data->get(1)->toArray());
$this->assertEquals([2 => 9, 3 => 10, 4 => 11, 5 => 12], $data->get(2)->toArray());
$this->assertEquals([6 => 15, 7 => 16], $data->get(3)->toArray());
$this->assertEquals([8 => 19, 9 => 20, 10 => 21], $data->last()->toArray());
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ public function testChunkIsLazy()
});
}

public function testChunkWhileIsLazy()
{
$collection = LazyCollection::make(['A', 'A', 'B', 'B', 'C', 'C', 'C']);

$this->assertDoesNotEnumerateCollection($collection, function ($collection) {
$collection->chunkWhile(function ($current, $key, $chunk) {
return $current === $chunk->last();
});
});

$this->assertEnumeratesCollection($collection, 7, function ($collection) {
$collection->chunkWhile(function ($current, $key, $chunk) {
return $current === $chunk->last();
})->take(7)->all();
});
}

public function testCollapseIsLazy()
{
$collection = LazyCollection::make([
Expand Down