ObjectIndex.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * @package Grav\Framework\Object
  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\Framework\Object;
  9. use Doctrine\Common\Collections\Criteria;
  10. use Grav\Framework\Collection\AbstractIndexCollection;
  11. use Grav\Framework\Object\Interfaces\NestedObjectCollectionInterface;
  12. use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
  13. use function get_class;
  14. use function is_object;
  15. /**
  16. * Keeps index of objects instead of collection of objects. This class allows you to keep a list of objects and load
  17. * them on demand. The class can be used seemingly instead of ObjectCollection when the objects haven't been loaded yet.
  18. *
  19. * This is an abstract class and has some protected abstract methods to load objects which you need to implement in
  20. * order to use the class.
  21. *
  22. * @template TKey of array-key
  23. * @template T of \Grav\Framework\Object\Interfaces\ObjectInterface
  24. * @template C of ObjectCollectionInterface
  25. * @extends AbstractIndexCollection<TKey,T,C>
  26. * @implements NestedObjectCollectionInterface<TKey,T>
  27. */
  28. abstract class ObjectIndex extends AbstractIndexCollection implements NestedObjectCollectionInterface
  29. {
  30. /** @var string */
  31. protected static $type;
  32. /** @var string */
  33. protected $_key;
  34. /**
  35. * @param bool $prefix
  36. * @return string
  37. */
  38. public function getType($prefix = true)
  39. {
  40. $type = $prefix ? $this->getTypePrefix() : '';
  41. if (static::$type) {
  42. return $type . static::$type;
  43. }
  44. $class = get_class($this);
  45. return $type . strtolower(substr($class, strrpos($class, '\\') + 1));
  46. }
  47. /**
  48. * @return string
  49. */
  50. public function getKey()
  51. {
  52. return $this->_key ?: $this->getType() . '@@' . spl_object_hash($this);
  53. }
  54. /**
  55. * @param string $key
  56. * @return $this
  57. */
  58. public function setKey($key)
  59. {
  60. $this->_key = $key;
  61. return $this;
  62. }
  63. /**
  64. * @param string $property Object property name.
  65. * @return bool[] True if property has been defined (can be null).
  66. */
  67. public function hasProperty($property)
  68. {
  69. return $this->__call('hasProperty', [$property]);
  70. }
  71. /**
  72. * @param string $property Object property to be fetched.
  73. * @param mixed $default Default value if property has not been set.
  74. * @return mixed[] Property values.
  75. */
  76. public function getProperty($property, $default = null)
  77. {
  78. return $this->__call('getProperty', [$property, $default]);
  79. }
  80. /**
  81. * @param string $property Object property to be updated.
  82. * @param string $value New value.
  83. * @return ObjectCollectionInterface
  84. * @phpstan-return C
  85. */
  86. public function setProperty($property, $value)
  87. {
  88. return $this->__call('setProperty', [$property, $value]);
  89. }
  90. /**
  91. * @param string $property Object property to be defined.
  92. * @param mixed $default Default value.
  93. * @return ObjectCollectionInterface
  94. * @phpstan-return C
  95. */
  96. public function defProperty($property, $default)
  97. {
  98. return $this->__call('defProperty', [$property, $default]);
  99. }
  100. /**
  101. * @param string $property Object property to be unset.
  102. * @return ObjectCollectionInterface
  103. * @phpstan-return C
  104. */
  105. public function unsetProperty($property)
  106. {
  107. return $this->__call('unsetProperty', [$property]);
  108. }
  109. /**
  110. * @param string $property Object property name.
  111. * @param string|null $separator Separator, defaults to '.'
  112. * @return bool[] True if property has been defined (can be null).
  113. */
  114. public function hasNestedProperty($property, $separator = null)
  115. {
  116. return $this->__call('hasNestedProperty', [$property, $separator]);
  117. }
  118. /**
  119. * @param string $property Object property to be fetched.
  120. * @param mixed $default Default value if property has not been set.
  121. * @param string|null $separator Separator, defaults to '.'
  122. * @return mixed[] Property values.
  123. */
  124. public function getNestedProperty($property, $default = null, $separator = null)
  125. {
  126. return $this->__call('getNestedProperty', [$property, $default, $separator]);
  127. }
  128. /**
  129. * @param string $property Object property to be updated.
  130. * @param mixed $value New value.
  131. * @param string|null $separator Separator, defaults to '.'
  132. * @return ObjectCollectionInterface
  133. * @phpstan-return C
  134. */
  135. public function setNestedProperty($property, $value, $separator = null)
  136. {
  137. return $this->__call('setNestedProperty', [$property, $value, $separator]);
  138. }
  139. /**
  140. * @param string $property Object property to be defined.
  141. * @param mixed $default Default value.
  142. * @param string|null $separator Separator, defaults to '.'
  143. * @return ObjectCollectionInterface
  144. * @phpstan-return C
  145. */
  146. public function defNestedProperty($property, $default, $separator = null)
  147. {
  148. return $this->__call('defNestedProperty', [$property, $default, $separator]);
  149. }
  150. /**
  151. * @param string $property Object property to be unset.
  152. * @param string|null $separator Separator, defaults to '.'
  153. * @return ObjectCollectionInterface
  154. * @phpstan-return C
  155. */
  156. public function unsetNestedProperty($property, $separator = null)
  157. {
  158. return $this->__call('unsetNestedProperty', [$property, $separator]);
  159. }
  160. /**
  161. * Create a copy from this collection by cloning all objects in the collection.
  162. *
  163. * @return static
  164. * @return static<TKey,T,C>
  165. */
  166. public function copy()
  167. {
  168. $list = [];
  169. foreach ($this->getIterator() as $key => $value) {
  170. /** @phpstan-ignore-next-line */
  171. $list[$key] = is_object($value) ? clone $value : $value;
  172. }
  173. /** @phpstan-var static<TKey,T,C> */
  174. return $this->createFrom($list);
  175. }
  176. /**
  177. * @return array
  178. */
  179. public function getObjectKeys()
  180. {
  181. return $this->getKeys();
  182. }
  183. /**
  184. * @param array $ordering
  185. * @return ObjectCollectionInterface
  186. * @phpstan-return C
  187. */
  188. public function orderBy(array $ordering)
  189. {
  190. return $this->__call('orderBy', [$ordering]);
  191. }
  192. /**
  193. * @param string $method
  194. * @param array $arguments
  195. * @return array|mixed
  196. */
  197. public function call($method, array $arguments = [])
  198. {
  199. return $this->__call('call', [$method, $arguments]);
  200. }
  201. /**
  202. * Group items in the collection by a field and return them as associated array.
  203. *
  204. * @param string $property
  205. * @return array
  206. */
  207. public function group($property)
  208. {
  209. return $this->__call('group', [$property]);
  210. }
  211. /**
  212. * Group items in the collection by a field and return them as associated array of collections.
  213. *
  214. * @param string $property
  215. * @return ObjectCollectionInterface[]
  216. * @phpstan-return C[]
  217. */
  218. public function collectionGroup($property)
  219. {
  220. return $this->__call('collectionGroup', [$property]);
  221. }
  222. /**
  223. * @param Criteria $criteria
  224. * @return ObjectCollectionInterface
  225. * @phpstan-return C
  226. */
  227. public function matching(Criteria $criteria)
  228. {
  229. $collection = $this->loadCollection($this->getEntries());
  230. /** @phpstan-var C $matching */
  231. $matching = $collection->matching($criteria);
  232. return $matching;
  233. }
  234. /**
  235. * @param string $name
  236. * @param array $arguments
  237. * @return mixed
  238. */
  239. #[\ReturnTypeWillChange]
  240. abstract public function __call($name, $arguments);
  241. /**
  242. * @return string
  243. */
  244. protected function getTypePrefix()
  245. {
  246. return '';
  247. }
  248. }