Skip to content
Closed
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
28 changes: 28 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,34 @@ public function chunk($size)
return new static($chunks);
}

/**
* Chunk the collection into chunks that
* start with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkStartingWith(callable $callback)
{
return new static(
$this->lazy()->chunkStartingWith($callback)->mapInto(static::class)
);
}

/**
* Chunk the collection into chunks that
* end with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkEndingWith(callable $callback)
{
return new static(
$this->lazy()->chunkEndingWith($callback)->mapInto(static::class)
);
}

/**
* Sort through each item with a callback.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Support/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,24 @@ public function split($numberOfGroups);
*/
public function chunk($size);

/**
* Chunk the collection into chunks that
* start with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkStartingWith(callable $callback);

/**
* Chunk the collection into chunks that
* end with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkEndingWith(callable $callback);

/**
* Sort through each item with a callback.
*
Expand Down
56 changes: 56 additions & 0 deletions src/Illuminate/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,62 @@ public function chunk($size)
});
}

/**
* Chunk the collection into chunks that
* start with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkStartingWith(callable $callback)
{
return new static(function () use ($callback) {
$chunk = [];

foreach ($this as $key => $value) {
if ($callback($value, $key) && ! empty($chunk)) {
yield new static($chunk);

$chunk = [];
}

$chunk[$key] = $value;
}

if (! empty($chunk)) {
yield new static($chunk);
}
});
}

/**
* Chunk the collection into chunks that
* end with an item passing the given test.
*
* @param callable $callback
* @return static
*/
public function chunkEndingWith(callable $callback)
{
return new static(function () use ($callback) {
$chunk = [];

foreach ($this as $key => $value) {
$chunk[$key] = $value;

if ($callback($value, $key) && ! empty($chunk)) {
yield new static($chunk);

$chunk = [];
}
}

if (! empty($chunk)) {
yield new static($chunk);
}
});
}

/**
* Sort through each item with a callback.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,40 @@ public function testChunkWhenGivenLessThanZero($collection)
);
}

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

$this->assertEquals(
[['header', 1, 2], ['header', 1], ['header'], ['header']],
$data->chunkStartingWith(function ($value, $key) {
return $value == 'header';
})->map->values()->toArray()
);
}

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

$this->assertEquals(
[['footer'], [1, 2, 'footer'], [1, 'footer'], [1, 2]],
$data->chunkEndingWith(function ($value, $key) {
return $value == 'footer';
})->map->values()->toArray()
);
}

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

public function testChunkEndingWithIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->chunkEndingWith(function ($value) {
return $value % 5 == 0;
});
});

$this->assertEnumerates(15, function ($collection) {
$collection->chunkEndingWith(function ($value) {
return $value % 5 == 0;
})->take(3)->all();
});
}

public function testChunkStartingWithIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->chunkStartingWith(function ($value) {
return $value % 5 == 0;
});
});

$this->assertEnumerates(15, function ($collection) {
$collection->chunkStartingWith(function ($value) {
return $value % 5 == 0;
})->take(3)->all();
});
}

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