2017-02-15 10:44:14 +01:00
|
|
|
<?php
|
2017-04-13 11:04:14 +02:00
|
|
|
/**
|
|
|
|
|
* @package Grav\Framework\Collection
|
|
|
|
|
*
|
2017-12-11 23:08:05 +01:00
|
|
|
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
2017-04-13 11:04:14 +02:00
|
|
|
* @license MIT License; see LICENSE file for details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Grav\Framework\Collection;
|
2017-02-15 10:44:14 +01:00
|
|
|
|
2017-05-09 20:27:33 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection as BaseArrayCollection;
|
2017-02-15 10:44:14 +01:00
|
|
|
|
2017-04-13 11:04:14 +02:00
|
|
|
/**
|
|
|
|
|
* General JSON serializable collection.
|
|
|
|
|
*
|
|
|
|
|
* @package Grav\Framework\Collection
|
|
|
|
|
*/
|
2017-05-09 20:27:33 +02:00
|
|
|
class ArrayCollection extends BaseArrayCollection implements CollectionInterface
|
2017-02-15 10:44:14 +01:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 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()));
|
2017-02-15 10:44:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-02-15 10:44:14 +01:00
|
|
|
}
|
2017-02-15 11:25:36 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 11:25:36 +01:00
|
|
|
/**
|
|
|
|
|
* Implementes JsonSerializable interface.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
|
{
|
|
|
|
|
return $this->toArray();
|
|
|
|
|
}
|
2017-02-15 10:44:14 +01:00
|
|
|
}
|