Add chunk method to BaseCollection

- Add test for BaseCollection->chunk
This commit is contained in:
Hypolite Petovan 2023-09-29 03:07:38 -04:00
parent 3333d4af88
commit 6d009a3e0f
2 changed files with 84 additions and 0 deletions

View file

@ -129,6 +129,24 @@ class BaseCollection extends \ArrayIterator
return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
}
/**
* Split the collection in smaller collections no bigger than the provided length
*
* @param int $length
* @return static[]
*/
public function chunk(int $length): array
{
if ($length < 1) {
throw new \RangeException('BaseCollection->chunk(): Size parameter expected to be greater than 0');
}
return array_map(function ($array) {
return new static($array);
}, array_chunk($this->getArrayCopy(), $length));
}
/**
* @inheritDoc
*