grav/system/src/Grav/Framework/Collection/ArrayCollection.php

96 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* @package Grav\Framework\Collection
*
2019-01-31 09:04:57 +01:00
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Collection;
use Doctrine\Common\Collections\ArrayCollection as BaseArrayCollection;
/**
* General JSON serializable collection.
*
* @package Grav\Framework\Collection
*/
class ArrayCollection extends BaseArrayCollection implements CollectionInterface
{
/**
* Reverse the order of the items.
*
* @return static
*/
public function reverse()
{
2017-08-18 11:28:13 +02:00
return $this->createFrom(array_reverse($this->toArray()));
}
/**
* Shuffle items.
*
* @return static
*/
public function shuffle()
{
$keys = $this->getKeys();
shuffle($keys);
2017-08-18 11:28:13 +02:00
return $this->createFrom(array_replace(array_flip($keys), $this->toArray()));
}
2017-09-15 13:05:21 +02:00
/**
* Split collection into chunks.
*
* @param int $size Size of each chunk.
* @return array
*/
public function chunk($size)
{
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
*/
public function select(array $keys)
{
$list = [];
foreach ($keys as $key) {
if ($this->containsKey($key)) {
$list[$key] = $this->get($key);
}
}
return $this->createFrom($list);
}
/**
* Un-select items from collection.
*
* @param array $keys
* @return static
*/
public function unselect(array $keys)
{
return $this->select(array_diff($this->getKeys(), $keys));
}
/**
2019-06-13 23:53:33 +02:00
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
}