FlexPageCollection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\Flex\Pages;
  10. use Grav\Common\Page\Interfaces\PageInterface;
  11. use Grav\Framework\Flex\FlexCollection;
  12. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  13. use function array_search;
  14. use function assert;
  15. use function is_int;
  16. /**
  17. * Class FlexPageCollection
  18. * @package Grav\Plugin\FlexObjects\Types\FlexPages
  19. * @template T of FlexObjectInterface
  20. * @extends FlexCollection<T>
  21. */
  22. class FlexPageCollection extends FlexCollection
  23. {
  24. /**
  25. * @return array
  26. */
  27. public static function getCachedMethods(): array
  28. {
  29. return [
  30. // Collection filtering
  31. 'withPublished' => true,
  32. 'withVisible' => true,
  33. 'withRoutable' => true,
  34. 'isFirst' => true,
  35. 'isLast' => true,
  36. // Find objects
  37. 'prevSibling' => false,
  38. 'nextSibling' => false,
  39. 'adjacentSibling' => false,
  40. 'currentPosition' => true,
  41. 'getNextOrder' => false,
  42. ] + parent::getCachedMethods();
  43. }
  44. /**
  45. * @param bool $bool
  46. * @return static
  47. * @phpstan-return static<T>
  48. */
  49. public function withPublished(bool $bool = true)
  50. {
  51. /** @var string[] $list */
  52. $list = array_keys(array_filter($this->call('isPublished', [$bool])));
  53. /** @phpstan-var static<T> */
  54. return $this->select($list);
  55. }
  56. /**
  57. * @param bool $bool
  58. * @return static
  59. * @phpstan-return static<T>
  60. */
  61. public function withVisible(bool $bool = true)
  62. {
  63. /** @var string[] $list */
  64. $list = array_keys(array_filter($this->call('isVisible', [$bool])));
  65. /** @phpstan-var static<T> */
  66. return $this->select($list);
  67. }
  68. /**
  69. * @param bool $bool
  70. * @return static
  71. * @phpstan-return static<T>
  72. */
  73. public function withRoutable(bool $bool = true)
  74. {
  75. /** @var string[] $list */
  76. $list = array_keys(array_filter($this->call('isRoutable', [$bool])));
  77. /** @phpstan-var static<T> */
  78. return $this->select($list);
  79. }
  80. /**
  81. * Check to see if this item is the first in the collection.
  82. *
  83. * @param string $path
  84. * @return bool True if item is first.
  85. */
  86. public function isFirst($path): bool
  87. {
  88. $keys = $this->getKeys();
  89. $first = reset($keys);
  90. return $path === $first;
  91. }
  92. /**
  93. * Check to see if this item is the last in the collection.
  94. *
  95. * @param string $path
  96. * @return bool True if item is last.
  97. */
  98. public function isLast($path): bool
  99. {
  100. $keys = $this->getKeys();
  101. $last = end($keys);
  102. return $path === $last;
  103. }
  104. /**
  105. * Gets the previous sibling based on current position.
  106. *
  107. * @param string $path
  108. * @return PageInterface|false The previous item.
  109. * @phpstan-return T|false
  110. */
  111. public function prevSibling($path)
  112. {
  113. return $this->adjacentSibling($path, -1);
  114. }
  115. /**
  116. * Gets the next sibling based on current position.
  117. *
  118. * @param string $path
  119. * @return PageInterface|false The next item.
  120. * @phpstan-return T|false
  121. */
  122. public function nextSibling($path)
  123. {
  124. return $this->adjacentSibling($path, 1);
  125. }
  126. /**
  127. * Returns the adjacent sibling based on a direction.
  128. *
  129. * @param string $path
  130. * @param int $direction either -1 or +1
  131. * @return PageInterface|false The sibling item.
  132. * @phpstan-return T|false
  133. */
  134. public function adjacentSibling($path, $direction = 1)
  135. {
  136. $keys = $this->getKeys();
  137. $direction = (int)$direction;
  138. $pos = array_search($path, $keys, true);
  139. if (is_int($pos)) {
  140. $pos += $direction;
  141. if (isset($keys[$pos])) {
  142. return $this[$keys[$pos]];
  143. }
  144. }
  145. return false;
  146. }
  147. /**
  148. * Returns the item in the current position.
  149. *
  150. * @param string $path the path the item
  151. * @return int|null The index of the current page, null if not found.
  152. */
  153. public function currentPosition($path): ?int
  154. {
  155. $pos = array_search($path, $this->getKeys(), true);
  156. return is_int($pos) ? $pos : null;
  157. }
  158. /**
  159. * @return string
  160. */
  161. public function getNextOrder()
  162. {
  163. $directory = $this->getFlexDirectory();
  164. $collection = $directory->getIndex();
  165. $keys = $collection->getStorageKeys();
  166. // Assign next free order.
  167. $last = null;
  168. $order = 0;
  169. foreach ($keys as $folder => $key) {
  170. preg_match(FlexPageIndex::ORDER_PREFIX_REGEX, $folder, $test);
  171. $test = $test[0] ?? null;
  172. if ($test && $test > $order) {
  173. $order = $test;
  174. $last = $key;
  175. }
  176. }
  177. /** @var FlexPageObject|null $last */
  178. $last = $collection[$last];
  179. return sprintf('%d.', $last ? $last->getFormValue('order') + 1 : 1);
  180. }
  181. }