ArrayCollection.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @package Grav\Framework\Collection
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Collection;
  9. use Doctrine\Common\Collections\ArrayCollection as BaseArrayCollection;
  10. /**
  11. * General JSON serializable collection.
  12. *
  13. * @package Grav\Framework\Collection
  14. */
  15. class ArrayCollection extends BaseArrayCollection implements CollectionInterface
  16. {
  17. /**
  18. * Reverse the order of the items.
  19. *
  20. * @return static
  21. */
  22. public function reverse()
  23. {
  24. return $this->createFrom(array_reverse($this->toArray()));
  25. }
  26. /**
  27. * Shuffle items.
  28. *
  29. * @return static
  30. */
  31. public function shuffle()
  32. {
  33. $keys = $this->getKeys();
  34. shuffle($keys);
  35. return $this->createFrom(array_replace(array_flip($keys), $this->toArray()));
  36. }
  37. /**
  38. * Split collection into chunks.
  39. *
  40. * @param int $size Size of each chunk.
  41. * @return array
  42. */
  43. public function chunk($size)
  44. {
  45. return array_chunk($this->toArray(), $size, true);
  46. }
  47. /**
  48. * Implementes JsonSerializable interface.
  49. *
  50. * @return array
  51. */
  52. public function jsonSerialize()
  53. {
  54. return $this->toArray();
  55. }
  56. }