Collection.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page;
  9. use Exception;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Iterator;
  12. use Grav\Common\Page\Interfaces\PageCollectionInterface;
  13. use Grav\Common\Page\Interfaces\PageInterface;
  14. use Grav\Common\Utils;
  15. use InvalidArgumentException;
  16. use function array_key_exists;
  17. use function array_keys;
  18. use function array_search;
  19. use function count;
  20. use function in_array;
  21. use function is_array;
  22. use function is_string;
  23. /**
  24. * Class Collection
  25. * @package Grav\Common\Page
  26. * @implements PageCollectionInterface<string,Page>
  27. */
  28. class Collection extends Iterator implements PageCollectionInterface
  29. {
  30. /** @var Pages */
  31. protected $pages;
  32. /** @var array */
  33. protected $params;
  34. /**
  35. * Collection constructor.
  36. *
  37. * @param array $items
  38. * @param array $params
  39. * @param Pages|null $pages
  40. */
  41. public function __construct($items = [], array $params = [], Pages $pages = null)
  42. {
  43. parent::__construct($items);
  44. $this->params = $params;
  45. $this->pages = $pages ?: Grav::instance()->offsetGet('pages');
  46. }
  47. /**
  48. * Get the collection params
  49. *
  50. * @return array
  51. */
  52. public function params()
  53. {
  54. return $this->params;
  55. }
  56. /**
  57. * Set parameters to the Collection
  58. *
  59. * @param array $params
  60. * @return $this
  61. */
  62. public function setParams(array $params)
  63. {
  64. $this->params = array_merge($this->params, $params);
  65. return $this;
  66. }
  67. /**
  68. * Add a single page to a collection
  69. *
  70. * @param PageInterface $page
  71. * @return $this
  72. */
  73. public function addPage(PageInterface $page)
  74. {
  75. $this->items[$page->path()] = ['slug' => $page->slug()];
  76. return $this;
  77. }
  78. /**
  79. * Add a page with path and slug
  80. *
  81. * @param string $path
  82. * @param string $slug
  83. * @return $this
  84. */
  85. public function add($path, $slug)
  86. {
  87. $this->items[$path] = ['slug' => $slug];
  88. return $this;
  89. }
  90. /**
  91. *
  92. * Create a copy of this collection
  93. *
  94. * @return static
  95. */
  96. public function copy()
  97. {
  98. return new static($this->items, $this->params, $this->pages);
  99. }
  100. /**
  101. *
  102. * Merge another collection with the current collection
  103. *
  104. * @param PageCollectionInterface $collection
  105. * @return $this
  106. */
  107. public function merge(PageCollectionInterface $collection)
  108. {
  109. foreach ($collection as $page) {
  110. $this->addPage($page);
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Intersect another collection with the current collection
  116. *
  117. * @param PageCollectionInterface $collection
  118. * @return $this
  119. */
  120. public function intersect(PageCollectionInterface $collection)
  121. {
  122. $array1 = $this->items;
  123. $array2 = $collection->toArray();
  124. $this->items = array_uintersect($array1, $array2, function ($val1, $val2) {
  125. return strcmp($val1['slug'], $val2['slug']);
  126. });
  127. return $this;
  128. }
  129. /**
  130. * Set current page.
  131. */
  132. public function setCurrent(string $path): void
  133. {
  134. reset($this->items);
  135. while (($key = key($this->items)) !== null && $key !== $path) {
  136. next($this->items);
  137. }
  138. }
  139. /**
  140. * Returns current page.
  141. *
  142. * @return PageInterface
  143. */
  144. #[\ReturnTypeWillChange]
  145. public function current()
  146. {
  147. $current = parent::key();
  148. return $this->pages->get($current);
  149. }
  150. /**
  151. * Returns current slug.
  152. *
  153. * @return mixed
  154. */
  155. #[\ReturnTypeWillChange]
  156. public function key()
  157. {
  158. $current = parent::current();
  159. return $current['slug'];
  160. }
  161. /**
  162. * Returns the value at specified offset.
  163. *
  164. * @param string $offset
  165. * @return PageInterface|null
  166. */
  167. #[\ReturnTypeWillChange]
  168. public function offsetGet($offset)
  169. {
  170. return $this->pages->get($offset) ?: null;
  171. }
  172. /**
  173. * Split collection into array of smaller collections.
  174. *
  175. * @param int $size
  176. * @return Collection[]
  177. */
  178. public function batch($size)
  179. {
  180. $chunks = array_chunk($this->items, $size, true);
  181. $list = [];
  182. foreach ($chunks as $chunk) {
  183. $list[] = new static($chunk, $this->params, $this->pages);
  184. }
  185. return $list;
  186. }
  187. /**
  188. * Remove item from the list.
  189. *
  190. * @param PageInterface|string|null $key
  191. * @return $this
  192. * @throws InvalidArgumentException
  193. */
  194. public function remove($key = null)
  195. {
  196. if ($key instanceof PageInterface) {
  197. $key = $key->path();
  198. } elseif (null === $key) {
  199. $key = (string)key($this->items);
  200. }
  201. if (!is_string($key)) {
  202. throw new InvalidArgumentException('Invalid argument $key.');
  203. }
  204. parent::remove($key);
  205. return $this;
  206. }
  207. /**
  208. * Reorder collection.
  209. *
  210. * @param string $by
  211. * @param string $dir
  212. * @param array|null $manual
  213. * @param string|null $sort_flags
  214. * @return $this
  215. */
  216. public function order($by, $dir = 'asc', $manual = null, $sort_flags = null)
  217. {
  218. $this->items = $this->pages->sortCollection($this, $by, $dir, $manual, $sort_flags);
  219. return $this;
  220. }
  221. /**
  222. * Check to see if this item is the first in the collection.
  223. *
  224. * @param string $path
  225. * @return bool True if item is first.
  226. */
  227. public function isFirst($path): bool
  228. {
  229. return $this->items && $path === array_keys($this->items)[0];
  230. }
  231. /**
  232. * Check to see if this item is the last in the collection.
  233. *
  234. * @param string $path
  235. * @return bool True if item is last.
  236. */
  237. public function isLast($path): bool
  238. {
  239. return $this->items && $path === array_keys($this->items)[count($this->items) - 1];
  240. }
  241. /**
  242. * Gets the previous sibling based on current position.
  243. *
  244. * @param string $path
  245. *
  246. * @return PageInterface The previous item.
  247. */
  248. public function prevSibling($path)
  249. {
  250. return $this->adjacentSibling($path, -1);
  251. }
  252. /**
  253. * Gets the next sibling based on current position.
  254. *
  255. * @param string $path
  256. *
  257. * @return PageInterface The next item.
  258. */
  259. public function nextSibling($path)
  260. {
  261. return $this->adjacentSibling($path, 1);
  262. }
  263. /**
  264. * Returns the adjacent sibling based on a direction.
  265. *
  266. * @param string $path
  267. * @param int $direction either -1 or +1
  268. * @return PageInterface|Collection The sibling item.
  269. */
  270. public function adjacentSibling($path, $direction = 1)
  271. {
  272. $values = array_keys($this->items);
  273. $keys = array_flip($values);
  274. if (array_key_exists($path, $keys)) {
  275. $index = $keys[$path] - $direction;
  276. return isset($values[$index]) ? $this->offsetGet($values[$index]) : $this;
  277. }
  278. return $this;
  279. }
  280. /**
  281. * Returns the item in the current position.
  282. *
  283. * @param string $path the path the item
  284. * @return int|null The index of the current page, null if not found.
  285. */
  286. public function currentPosition($path): ?int
  287. {
  288. $pos = array_search($path, array_keys($this->items), true);
  289. return $pos !== false ? $pos : null;
  290. }
  291. /**
  292. * Returns the items between a set of date ranges of either the page date field (default) or
  293. * an arbitrary datetime page field where start date and end date are optional
  294. * Dates must be passed in as text that strtotime() can process
  295. * http://php.net/manual/en/function.strtotime.php
  296. *
  297. * @param string|null $startDate
  298. * @param string|null $endDate
  299. * @param string|null $field
  300. * @return $this
  301. * @throws Exception
  302. */
  303. public function dateRange($startDate = null, $endDate = null, $field = null)
  304. {
  305. $start = $startDate ? Utils::date2timestamp($startDate) : null;
  306. $end = $endDate ? Utils::date2timestamp($endDate) : null;
  307. $date_range = [];
  308. foreach ($this->items as $path => $slug) {
  309. $page = $this->pages->get($path);
  310. if (!$page) {
  311. continue;
  312. }
  313. $date = $field ? strtotime($page->value($field)) : $page->date();
  314. if ((!$start || $date >= $start) && (!$end || $date <= $end)) {
  315. $date_range[$path] = $slug;
  316. }
  317. }
  318. $this->items = $date_range;
  319. return $this;
  320. }
  321. /**
  322. * Creates new collection with only visible pages
  323. *
  324. * @return Collection The collection with only visible pages
  325. */
  326. public function visible()
  327. {
  328. $visible = [];
  329. foreach ($this->items as $path => $slug) {
  330. $page = $this->pages->get($path);
  331. if ($page !== null && $page->visible()) {
  332. $visible[$path] = $slug;
  333. }
  334. }
  335. $this->items = $visible;
  336. return $this;
  337. }
  338. /**
  339. * Creates new collection with only non-visible pages
  340. *
  341. * @return Collection The collection with only non-visible pages
  342. */
  343. public function nonVisible()
  344. {
  345. $visible = [];
  346. foreach ($this->items as $path => $slug) {
  347. $page = $this->pages->get($path);
  348. if ($page !== null && !$page->visible()) {
  349. $visible[$path] = $slug;
  350. }
  351. }
  352. $this->items = $visible;
  353. return $this;
  354. }
  355. /**
  356. * Creates new collection with only pages
  357. *
  358. * @return Collection The collection with only pages
  359. */
  360. public function pages()
  361. {
  362. $modular = [];
  363. foreach ($this->items as $path => $slug) {
  364. $page = $this->pages->get($path);
  365. if ($page !== null && !$page->isModule()) {
  366. $modular[$path] = $slug;
  367. }
  368. }
  369. $this->items = $modular;
  370. return $this;
  371. }
  372. /**
  373. * Creates new collection with only modules
  374. *
  375. * @return Collection The collection with only modules
  376. */
  377. public function modules()
  378. {
  379. $modular = [];
  380. foreach ($this->items as $path => $slug) {
  381. $page = $this->pages->get($path);
  382. if ($page !== null && $page->isModule()) {
  383. $modular[$path] = $slug;
  384. }
  385. }
  386. $this->items = $modular;
  387. return $this;
  388. }
  389. /**
  390. * Alias of pages()
  391. *
  392. * @return Collection The collection with only non-module pages
  393. */
  394. public function nonModular()
  395. {
  396. $this->pages();
  397. return $this;
  398. }
  399. /**
  400. * Alias of modules()
  401. *
  402. * @return Collection The collection with only modules
  403. */
  404. public function modular()
  405. {
  406. $this->modules();
  407. return $this;
  408. }
  409. /**
  410. * Creates new collection with only translated pages
  411. *
  412. * @return Collection The collection with only published pages
  413. * @internal
  414. */
  415. public function translated()
  416. {
  417. $published = [];
  418. foreach ($this->items as $path => $slug) {
  419. $page = $this->pages->get($path);
  420. if ($page !== null && $page->translated()) {
  421. $published[$path] = $slug;
  422. }
  423. }
  424. $this->items = $published;
  425. return $this;
  426. }
  427. /**
  428. * Creates new collection with only untranslated pages
  429. *
  430. * @return Collection The collection with only non-published pages
  431. * @internal
  432. */
  433. public function nonTranslated()
  434. {
  435. $published = [];
  436. foreach ($this->items as $path => $slug) {
  437. $page = $this->pages->get($path);
  438. if ($page !== null && !$page->translated()) {
  439. $published[$path] = $slug;
  440. }
  441. }
  442. $this->items = $published;
  443. return $this;
  444. }
  445. /**
  446. * Creates new collection with only published pages
  447. *
  448. * @return Collection The collection with only published pages
  449. */
  450. public function published()
  451. {
  452. $published = [];
  453. foreach ($this->items as $path => $slug) {
  454. $page = $this->pages->get($path);
  455. if ($page !== null && $page->published()) {
  456. $published[$path] = $slug;
  457. }
  458. }
  459. $this->items = $published;
  460. return $this;
  461. }
  462. /**
  463. * Creates new collection with only non-published pages
  464. *
  465. * @return Collection The collection with only non-published pages
  466. */
  467. public function nonPublished()
  468. {
  469. $published = [];
  470. foreach ($this->items as $path => $slug) {
  471. $page = $this->pages->get($path);
  472. if ($page !== null && !$page->published()) {
  473. $published[$path] = $slug;
  474. }
  475. }
  476. $this->items = $published;
  477. return $this;
  478. }
  479. /**
  480. * Creates new collection with only routable pages
  481. *
  482. * @return Collection The collection with only routable pages
  483. */
  484. public function routable()
  485. {
  486. $routable = [];
  487. foreach ($this->items as $path => $slug) {
  488. $page = $this->pages->get($path);
  489. if ($page !== null && $page->routable()) {
  490. $routable[$path] = $slug;
  491. }
  492. }
  493. $this->items = $routable;
  494. return $this;
  495. }
  496. /**
  497. * Creates new collection with only non-routable pages
  498. *
  499. * @return Collection The collection with only non-routable pages
  500. */
  501. public function nonRoutable()
  502. {
  503. $routable = [];
  504. foreach ($this->items as $path => $slug) {
  505. $page = $this->pages->get($path);
  506. if ($page !== null && !$page->routable()) {
  507. $routable[$path] = $slug;
  508. }
  509. }
  510. $this->items = $routable;
  511. return $this;
  512. }
  513. /**
  514. * Creates new collection with only pages of the specified type
  515. *
  516. * @param string $type
  517. * @return Collection The collection
  518. */
  519. public function ofType($type)
  520. {
  521. $items = [];
  522. foreach ($this->items as $path => $slug) {
  523. $page = $this->pages->get($path);
  524. if ($page !== null && $page->template() === $type) {
  525. $items[$path] = $slug;
  526. }
  527. }
  528. $this->items = $items;
  529. return $this;
  530. }
  531. /**
  532. * Creates new collection with only pages of one of the specified types
  533. *
  534. * @param string[] $types
  535. * @return Collection The collection
  536. */
  537. public function ofOneOfTheseTypes($types)
  538. {
  539. $items = [];
  540. foreach ($this->items as $path => $slug) {
  541. $page = $this->pages->get($path);
  542. if ($page !== null && in_array($page->template(), $types, true)) {
  543. $items[$path] = $slug;
  544. }
  545. }
  546. $this->items = $items;
  547. return $this;
  548. }
  549. /**
  550. * Creates new collection with only pages of one of the specified access levels
  551. *
  552. * @param array $accessLevels
  553. * @return Collection The collection
  554. */
  555. public function ofOneOfTheseAccessLevels($accessLevels)
  556. {
  557. $items = [];
  558. foreach ($this->items as $path => $slug) {
  559. $page = $this->pages->get($path);
  560. if ($page !== null && isset($page->header()->access)) {
  561. if (is_array($page->header()->access)) {
  562. //Multiple values for access
  563. $valid = false;
  564. foreach ($page->header()->access as $index => $accessLevel) {
  565. if (is_array($accessLevel)) {
  566. foreach ($accessLevel as $innerIndex => $innerAccessLevel) {
  567. if (in_array($innerAccessLevel, $accessLevels, false)) {
  568. $valid = true;
  569. }
  570. }
  571. } else {
  572. if (in_array($index, $accessLevels, false)) {
  573. $valid = true;
  574. }
  575. }
  576. }
  577. if ($valid) {
  578. $items[$path] = $slug;
  579. }
  580. } else {
  581. //Single value for access
  582. if (in_array($page->header()->access, $accessLevels, false)) {
  583. $items[$path] = $slug;
  584. }
  585. }
  586. }
  587. }
  588. $this->items = $items;
  589. return $this;
  590. }
  591. /**
  592. * Get the extended version of this Collection with each page keyed by route
  593. *
  594. * @return array
  595. * @throws Exception
  596. */
  597. public function toExtendedArray()
  598. {
  599. $items = [];
  600. foreach ($this->items as $path => $slug) {
  601. $page = $this->pages->get($path);
  602. if ($page !== null) {
  603. $items[$page->route()] = $page->toArray();
  604. }
  605. }
  606. return $items;
  607. }
  608. }