* @implements CollectionInterface */ class ArrayCollection extends BaseArrayCollection implements CollectionInterface { /** * Reverse the order of the items. * * @return static * @phpstan-return static */ public function reverse() { $keys = array_reverse($this->toArray()); /** @phpstan-var static */ return $this->createFrom($keys); } /** * Shuffle items. * * @return static * @phpstan-return static */ public function shuffle() { $keys = $this->getKeys(); shuffle($keys); $keys = array_replace(array_flip($keys), $this->toArray()); /** @phpstan-var static */ return $this->createFrom($keys); } /** * Split collection into chunks. * * @param int $size Size of each chunk. * @return array * @phpstan-return array> */ public function chunk($size) { /** @phpstan-var array> */ return array_chunk($this->toArray(), $size, true); } /** * Select items from collection. * * Collection is returned in the order of $keys given to the function. * * @param array $keys * @return static * @phpstan-param TKey[] $keys * @phpstan-return static */ public function select(array $keys) { $list = []; foreach ($keys as $key) { if ($this->containsKey($key)) { $list[$key] = $this->get($key); } } /** @phpstan-var static */ return $this->createFrom($list); } /** * Un-select items from collection. * * @param array $keys * @return static * @phpstan-param TKey[] $keys * @phpstan-return static */ public function unselect(array $keys) { $list = array_diff($this->getKeys(), $keys); /** @phpstan-var static */ return $this->select($list); } /** * Implements JsonSerializable interface. * * @return array */ #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->toArray(); } }