ArrayCollection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @package Grav\Framework\Collection
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 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. * @template TKey of array-key
  15. * @template T
  16. * @extends BaseArrayCollection<TKey,T>
  17. * @implements CollectionInterface<TKey,T>
  18. */
  19. class ArrayCollection extends BaseArrayCollection implements CollectionInterface
  20. {
  21. /**
  22. * Reverse the order of the items.
  23. *
  24. * @return static
  25. * @phpstan-return static<TKey,T>
  26. */
  27. public function reverse()
  28. {
  29. $keys = array_reverse($this->toArray());
  30. /** @phpstan-var static<TKey,T> */
  31. return $this->createFrom($keys);
  32. }
  33. /**
  34. * Shuffle items.
  35. *
  36. * @return static
  37. * @phpstan-return static<TKey,T>
  38. */
  39. public function shuffle()
  40. {
  41. $keys = $this->getKeys();
  42. shuffle($keys);
  43. $keys = array_replace(array_flip($keys), $this->toArray());
  44. /** @phpstan-var static<TKey,T> */
  45. return $this->createFrom($keys);
  46. }
  47. /**
  48. * Split collection into chunks.
  49. *
  50. * @param int $size Size of each chunk.
  51. * @return array
  52. * @phpstan-return array<array<TKey,T>>
  53. */
  54. public function chunk($size)
  55. {
  56. /** @phpstan-var array<array<TKey,T>> */
  57. return array_chunk($this->toArray(), $size, true);
  58. }
  59. /**
  60. * Select items from collection.
  61. *
  62. * Collection is returned in the order of $keys given to the function.
  63. *
  64. * @param array<int,string> $keys
  65. * @return static
  66. * @phpstan-param TKey[] $keys
  67. * @phpstan-return static<TKey,T>
  68. */
  69. public function select(array $keys)
  70. {
  71. $list = [];
  72. foreach ($keys as $key) {
  73. if ($this->containsKey($key)) {
  74. $list[$key] = $this->get($key);
  75. }
  76. }
  77. /** @phpstan-var static<TKey,T> */
  78. return $this->createFrom($list);
  79. }
  80. /**
  81. * Un-select items from collection.
  82. *
  83. * @param array<int|string> $keys
  84. * @return static
  85. * @phpstan-param TKey[] $keys
  86. * @phpstan-return static<TKey,T>
  87. */
  88. public function unselect(array $keys)
  89. {
  90. $list = array_diff($this->getKeys(), $keys);
  91. /** @phpstan-var static<TKey,T> */
  92. return $this->select($list);
  93. }
  94. /**
  95. * Implements JsonSerializable interface.
  96. *
  97. * @return array
  98. */
  99. #[\ReturnTypeWillChange]
  100. public function jsonSerialize()
  101. {
  102. return $this->toArray();
  103. }
  104. }