mirror of
https://github.com/friendica/friendica
synced 2025-05-04 04:24:11 +02:00
Add chunk method to BaseCollection
- Add test for BaseCollection->chunk
This commit is contained in:
parent
3333d4af88
commit
6d009a3e0f
2 changed files with 84 additions and 0 deletions
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue