AbstractIndexCollection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * @package Grav\Framework\Collection
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Collection;
  9. use ArrayIterator;
  10. use Closure;
  11. use Grav\Framework\Compat\Serializable;
  12. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  13. use InvalidArgumentException;
  14. use function array_key_exists;
  15. use function array_slice;
  16. use function count;
  17. /**
  18. * Abstract Index Collection.
  19. * @template TKey of array-key
  20. * @template T
  21. * @implements CollectionInterface<TKey,T>
  22. */
  23. abstract class AbstractIndexCollection implements CollectionInterface
  24. {
  25. use Serializable;
  26. /**
  27. * @var array
  28. * @phpstan-var array<TKey,T>
  29. */
  30. private $entries;
  31. /**
  32. * Initializes a new IndexCollection.
  33. *
  34. * @param array $entries
  35. * @phpstan-param array<TKey,T> $entries
  36. */
  37. public function __construct(array $entries = [])
  38. {
  39. $this->entries = $entries;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function toArray()
  45. {
  46. return $this->loadElements($this->entries);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function first()
  52. {
  53. $value = reset($this->entries);
  54. $key = (string)key($this->entries);
  55. return $this->loadElement($key, $value);
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function last()
  61. {
  62. $value = end($this->entries);
  63. $key = (string)key($this->entries);
  64. return $this->loadElement($key, $value);
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function key()
  70. {
  71. /** @phpstan-var TKey $key */
  72. $key = (string)key($this->entries);
  73. return $key;
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function next()
  79. {
  80. $value = next($this->entries);
  81. $key = (string)key($this->entries);
  82. return $this->loadElement($key, $value);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function current()
  88. {
  89. $value = current($this->entries);
  90. $key = (string)key($this->entries);
  91. return $this->loadElement($key, $value);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function remove($key)
  97. {
  98. if (!array_key_exists($key, $this->entries)) {
  99. return null;
  100. }
  101. $value = $this->entries[$key];
  102. unset($this->entries[$key]);
  103. return $this->loadElement((string)$key, $value);
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function removeElement($element)
  109. {
  110. $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
  111. if (!$key || !isset($this->entries[$key])) {
  112. return false;
  113. }
  114. unset($this->entries[$key]);
  115. return true;
  116. }
  117. /**
  118. * Required by interface ArrayAccess.
  119. *
  120. * {@inheritDoc}
  121. */
  122. public function offsetExists($offset)
  123. {
  124. return $this->containsKey($offset);
  125. }
  126. /**
  127. * Required by interface ArrayAccess.
  128. *
  129. * {@inheritDoc}
  130. */
  131. public function offsetGet($offset)
  132. {
  133. return $this->get($offset);
  134. }
  135. /**
  136. * Required by interface ArrayAccess.
  137. *
  138. * {@inheritDoc}
  139. */
  140. public function offsetSet($offset, $value)
  141. {
  142. if (null === $offset) {
  143. $this->add($value);
  144. }
  145. $this->set($offset, $value);
  146. }
  147. /**
  148. * Required by interface ArrayAccess.
  149. *
  150. * {@inheritDoc}
  151. */
  152. public function offsetUnset($offset)
  153. {
  154. return $this->remove($offset);
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function containsKey($key)
  160. {
  161. return isset($this->entries[$key]) || array_key_exists($key, $this->entries);
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. public function contains($element)
  167. {
  168. $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
  169. return $key && isset($this->entries[$key]);
  170. }
  171. /**
  172. * {@inheritDoc}
  173. */
  174. public function exists(Closure $p)
  175. {
  176. return $this->loadCollection($this->entries)->exists($p);
  177. }
  178. /**
  179. * {@inheritDoc}
  180. */
  181. public function indexOf($element)
  182. {
  183. $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
  184. return $key && isset($this->entries[$key]) ? $key : false;
  185. }
  186. /**
  187. * {@inheritDoc}
  188. */
  189. public function get($key)
  190. {
  191. if (!isset($this->entries[$key])) {
  192. return null;
  193. }
  194. return $this->loadElement((string)$key, $this->entries[$key]);
  195. }
  196. /**
  197. * {@inheritDoc}
  198. */
  199. public function getKeys()
  200. {
  201. return array_keys($this->entries);
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function getValues()
  207. {
  208. return array_values($this->loadElements($this->entries));
  209. }
  210. /**
  211. * {@inheritDoc}
  212. */
  213. public function count()
  214. {
  215. return count($this->entries);
  216. }
  217. /**
  218. * {@inheritDoc}
  219. */
  220. public function set($key, $value)
  221. {
  222. if (!$this->isAllowedElement($value)) {
  223. throw new InvalidArgumentException('Invalid argument $value');
  224. }
  225. $this->entries[$key] = $this->getElementMeta($value);
  226. }
  227. /**
  228. * {@inheritDoc}
  229. */
  230. public function add($element)
  231. {
  232. if (!$this->isAllowedElement($element)) {
  233. throw new InvalidArgumentException('Invalid argument $element');
  234. }
  235. $this->entries[$this->getCurrentKey($element)] = $this->getElementMeta($element);
  236. return true;
  237. }
  238. /**
  239. * {@inheritDoc}
  240. */
  241. public function isEmpty()
  242. {
  243. return empty($this->entries);
  244. }
  245. /**
  246. * Required by interface IteratorAggregate.
  247. *
  248. * {@inheritDoc}
  249. */
  250. public function getIterator()
  251. {
  252. return new ArrayIterator($this->loadElements());
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. public function map(Closure $func)
  258. {
  259. return $this->loadCollection($this->entries)->map($func);
  260. }
  261. /**
  262. * {@inheritDoc}
  263. */
  264. public function filter(Closure $p)
  265. {
  266. return $this->loadCollection($this->entries)->filter($p);
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. public function forAll(Closure $p)
  272. {
  273. return $this->loadCollection($this->entries)->forAll($p);
  274. }
  275. /**
  276. * {@inheritDoc}
  277. */
  278. public function partition(Closure $p)
  279. {
  280. return $this->loadCollection($this->entries)->partition($p);
  281. }
  282. /**
  283. * Returns a string representation of this object.
  284. *
  285. * @return string
  286. */
  287. public function __toString()
  288. {
  289. return __CLASS__ . '@' . spl_object_hash($this);
  290. }
  291. /**
  292. * {@inheritDoc}
  293. */
  294. public function clear()
  295. {
  296. $this->entries = [];
  297. }
  298. /**
  299. * {@inheritDoc}
  300. */
  301. public function slice($offset, $length = null)
  302. {
  303. return $this->loadElements(array_slice($this->entries, $offset, $length, true));
  304. }
  305. /**
  306. * @param int $start
  307. * @param int|null $limit
  308. * @return static
  309. * @phpstan-return static<TKey,T>
  310. */
  311. public function limit($start, $limit = null)
  312. {
  313. return $this->createFrom(array_slice($this->entries, $start, $limit, true));
  314. }
  315. /**
  316. * Reverse the order of the items.
  317. *
  318. * @return static
  319. * @phpstan-return static<TKey,T>
  320. */
  321. public function reverse()
  322. {
  323. return $this->createFrom(array_reverse($this->entries));
  324. }
  325. /**
  326. * Shuffle items.
  327. *
  328. * @return static
  329. * @phpstan-return static<TKey,T>
  330. */
  331. public function shuffle()
  332. {
  333. $keys = $this->getKeys();
  334. shuffle($keys);
  335. return $this->createFrom(array_replace(array_flip($keys), $this->entries) ?? []);
  336. }
  337. /**
  338. * Select items from collection.
  339. *
  340. * Collection is returned in the order of $keys given to the function.
  341. *
  342. * @param array $keys
  343. * @return static
  344. * @phpstan-return static<TKey,T>
  345. */
  346. public function select(array $keys)
  347. {
  348. $list = [];
  349. foreach ($keys as $key) {
  350. if (isset($this->entries[$key])) {
  351. $list[$key] = $this->entries[$key];
  352. }
  353. }
  354. return $this->createFrom($list);
  355. }
  356. /**
  357. * Un-select items from collection.
  358. *
  359. * @param array $keys
  360. * @return static
  361. * @phpstan-return static<TKey,T>
  362. */
  363. public function unselect(array $keys)
  364. {
  365. return $this->select(array_diff($this->getKeys(), $keys));
  366. }
  367. /**
  368. * Split collection into chunks.
  369. *
  370. * @param int $size Size of each chunk.
  371. * @return array
  372. */
  373. public function chunk($size)
  374. {
  375. return $this->loadCollection($this->entries)->chunk($size);
  376. }
  377. /**
  378. * @return array
  379. */
  380. public function __serialize(): array
  381. {
  382. return [
  383. 'entries' => $this->entries
  384. ];
  385. }
  386. /**
  387. * @param array $data
  388. * @return void
  389. */
  390. public function __unserialize(array $data): void
  391. {
  392. $this->entries = $data['entries'];
  393. }
  394. /**
  395. * Implements JsonSerializable interface.
  396. *
  397. * @return array
  398. */
  399. public function jsonSerialize()
  400. {
  401. return $this->loadCollection()->jsonSerialize();
  402. }
  403. /**
  404. * Creates a new instance from the specified elements.
  405. *
  406. * This method is provided for derived classes to specify how a new
  407. * instance should be created when constructor semantics have changed.
  408. *
  409. * @param array $entries Elements.
  410. * @return static
  411. * @phpstan-return static<TKey,T>
  412. */
  413. protected function createFrom(array $entries)
  414. {
  415. return new static($entries);
  416. }
  417. /**
  418. * @return array
  419. */
  420. protected function getEntries(): array
  421. {
  422. return $this->entries;
  423. }
  424. /**
  425. * @param array $entries
  426. * @return void
  427. * @phpstan-param array<TKey,T> $entries
  428. */
  429. protected function setEntries(array $entries): void
  430. {
  431. $this->entries = $entries;
  432. }
  433. /**
  434. * @param FlexObjectInterface $element
  435. * @return string
  436. * @phpstan-param T $element
  437. * @phpstan-return TKey
  438. */
  439. protected function getCurrentKey($element)
  440. {
  441. return $element->getKey();
  442. }
  443. /**
  444. * @param string $key
  445. * @param mixed $value
  446. * @return mixed|null
  447. */
  448. abstract protected function loadElement($key, $value);
  449. /**
  450. * @param array|null $entries
  451. * @return array
  452. * @phpstan-return array<TKey,T>
  453. */
  454. abstract protected function loadElements(array $entries = null): array;
  455. /**
  456. * @param array|null $entries
  457. * @return CollectionInterface
  458. * @phpstan-return T
  459. */
  460. abstract protected function loadCollection(array $entries = null): CollectionInterface;
  461. /**
  462. * @param mixed $value
  463. * @return bool
  464. */
  465. abstract protected function isAllowedElement($value): bool;
  466. /**
  467. * @param mixed $element
  468. * @return mixed
  469. */
  470. abstract protected function getElementMeta($element);
  471. }