X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseCollection.php;h=977292e5159a24eafb851bd610dd913bdd81d766;hb=ddd2c72be8e7245389f97d74dd847f5a20410936;hp=c711755722c13804bb1fdef439b700bf58c62375;hpb=7a39be8270c1ce56c7a2df078bfbfd310601c569;p=friendica.git diff --git a/src/BaseCollection.php b/src/BaseCollection.php index c711755722..977292e515 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -1,6 +1,6 @@ totalCount++; } - parent::offsetSet($offset, $value); + parent::offsetSet($key, $value); } /** * @inheritDoc */ - public function offsetUnset(mixed $offset): void + #[\ReturnTypeWillChange] + public function offsetUnset($key): void { - if ($this->offsetExists($offset)) { + if ($this->offsetExists($key)) { $this->totalCount--; } - parent::offsetUnset($offset); + parent::offsetUnset($key); } /** @@ -127,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 *