X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseCollection.php;h=977292e5159a24eafb851bd610dd913bdd81d766;hb=ddd2c72be8e7245389f97d74dd847f5a20410936;hp=0bf46f0dcb73d95d3639afe60f58a3a56ff85c54;hpb=322b7c856ca9ba53bd9c7da50dd5c1e3c9197d56;p=friendica.git diff --git a/src/BaseCollection.php b/src/BaseCollection.php index 0bf46f0dcb..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($offset) + #[\ReturnTypeWillChange] + public function offsetUnset($key): void { - if ($this->offsetExists($offset)) { + if ($this->offsetExists($key)) { $this->totalCount--; } - parent::offsetUnset($offset); + parent::offsetUnset($key); } /** - * @return int + * Getter for total count + * + * @return int Total count */ - public function getTotalCount() + public function getTotalCount(): int { return $this->totalCount; } @@ -85,7 +89,7 @@ class BaseCollection extends \ArrayIterator * @return array * @see array_column() */ - public function column($column, $index_key = null) + public function column(string $column, $index_key = null): array { return array_column($this->getArrayCopy(true), $column, $index_key); } @@ -97,7 +101,7 @@ class BaseCollection extends \ArrayIterator * @return BaseCollection * @see array_map() */ - public function map(callable $callback) + public function map(callable $callback): BaseCollection { return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount()); } @@ -110,7 +114,7 @@ class BaseCollection extends \ArrayIterator * @return BaseCollection * @see array_filter() */ - public function filter(callable $callback = null, int $flag = 0) + public function filter(callable $callback = null, int $flag = 0): BaseCollection { return new static(array_filter($this->getArrayCopy(), $callback, $flag)); } @@ -125,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 *