ObjectCollectionTrait.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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\Base;
  9. use Grav\Framework\Compat\Serializable;
  10. use Grav\Framework\Object\Interfaces\ObjectInterface;
  11. use InvalidArgumentException;
  12. use function call_user_func_array;
  13. use function get_class;
  14. use function is_callable;
  15. use function is_object;
  16. /**
  17. * ObjectCollection Trait
  18. * @package Grav\Framework\Object
  19. *
  20. * @template TKey as array-key
  21. * @template T as ObjectInterface
  22. */
  23. trait ObjectCollectionTrait
  24. {
  25. use Serializable;
  26. /** @var string */
  27. protected static $type;
  28. /** @var string */
  29. private $_key;
  30. /**
  31. * @return string
  32. */
  33. protected function getTypePrefix()
  34. {
  35. return '';
  36. }
  37. /**
  38. * @param bool $prefix
  39. * @return string
  40. */
  41. public function getType($prefix = true)
  42. {
  43. $type = $prefix ? $this->getTypePrefix() : '';
  44. if (static::$type) {
  45. return $type . static::$type;
  46. }
  47. $class = get_class($this);
  48. return $type . strtolower(substr($class, strrpos($class, '\\') + 1));
  49. }
  50. /**
  51. * @return string
  52. */
  53. public function getKey()
  54. {
  55. return $this->_key ?: $this->getType() . '@@' . spl_object_hash($this);
  56. }
  57. /**
  58. * @return bool
  59. */
  60. public function hasKey()
  61. {
  62. return !empty($this->_key);
  63. }
  64. /**
  65. * @param string $property Object property name.
  66. * @return bool[] True if property has been defined (can be null).
  67. */
  68. public function hasProperty($property)
  69. {
  70. return $this->doHasProperty($property);
  71. }
  72. /**
  73. * @param string $property Object property to be fetched.
  74. * @param mixed $default Default value if property has not been set.
  75. * @return mixed[] Property values.
  76. */
  77. public function getProperty($property, $default = null)
  78. {
  79. return $this->doGetProperty($property, $default);
  80. }
  81. /**
  82. * @param string $property Object property to be updated.
  83. * @param mixed $value New value.
  84. * @return $this
  85. */
  86. public function setProperty($property, $value)
  87. {
  88. $this->doSetProperty($property, $value);
  89. return $this;
  90. }
  91. /**
  92. * @param string $property Object property to be unset.
  93. * @return $this
  94. */
  95. public function unsetProperty($property)
  96. {
  97. $this->doUnsetProperty($property);
  98. return $this;
  99. }
  100. /**
  101. * @param string $property Object property to be defined.
  102. * @param mixed $default Default value.
  103. * @return $this
  104. */
  105. public function defProperty($property, $default)
  106. {
  107. if (!$this->hasProperty($property)) {
  108. $this->setProperty($property, $default);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * @return array
  114. */
  115. final public function __serialize(): array
  116. {
  117. return $this->doSerialize();
  118. }
  119. /**
  120. * @param array $data
  121. * @return void
  122. */
  123. final public function __unserialize(array $data): void
  124. {
  125. if (method_exists($this, 'initObjectProperties')) {
  126. $this->initObjectProperties();
  127. }
  128. $this->doUnserialize($data);
  129. }
  130. /**
  131. * @return array
  132. */
  133. protected function doSerialize()
  134. {
  135. return [
  136. 'key' => $this->getKey(),
  137. 'type' => $this->getType(),
  138. 'elements' => $this->getElements()
  139. ];
  140. }
  141. /**
  142. * @param array $data
  143. * @return void
  144. */
  145. protected function doUnserialize(array $data)
  146. {
  147. if (!isset($data['key'], $data['type'], $data['elements']) || $data['type'] !== $this->getType()) {
  148. throw new InvalidArgumentException("Cannot unserialize '{$this->getType()}': Bad data");
  149. }
  150. $this->setKey($data['key']);
  151. $this->setElements($data['elements']);
  152. }
  153. /**
  154. * Implements JsonSerializable interface.
  155. *
  156. * @return array
  157. */
  158. #[\ReturnTypeWillChange]
  159. public function jsonSerialize()
  160. {
  161. return $this->doSerialize();
  162. }
  163. /**
  164. * Returns a string representation of this object.
  165. *
  166. * @return string
  167. */
  168. #[\ReturnTypeWillChange]
  169. public function __toString()
  170. {
  171. return $this->getKey();
  172. }
  173. /**
  174. * @param string $key
  175. * @return $this
  176. */
  177. public function setKey($key)
  178. {
  179. $this->_key = (string) $key;
  180. return $this;
  181. }
  182. /**
  183. * Create a copy from this collection by cloning all objects in the collection.
  184. *
  185. * @return static<TKey,T>
  186. */
  187. public function copy()
  188. {
  189. $list = [];
  190. foreach ($this->getIterator() as $key => $value) {
  191. /** @phpstan-ignore-next-line */
  192. $list[$key] = is_object($value) ? clone $value : $value;
  193. }
  194. /** @phpstan-var static<TKey,T> */
  195. return $this->createFrom($list);
  196. }
  197. /**
  198. * @return string[]
  199. */
  200. public function getObjectKeys()
  201. {
  202. return $this->call('getKey');
  203. }
  204. /**
  205. * @param string $property Object property to be matched.
  206. * @return bool[] Key/Value pairs of the properties.
  207. */
  208. public function doHasProperty($property)
  209. {
  210. $list = [];
  211. /** @var ObjectInterface $element */
  212. foreach ($this->getIterator() as $id => $element) {
  213. $list[$id] = (bool)$element->hasProperty($property);
  214. }
  215. return $list;
  216. }
  217. /**
  218. * @param string $property Object property to be fetched.
  219. * @param mixed $default Default value if not set.
  220. * @param bool $doCreate Not being used.
  221. * @return mixed[] Key/Value pairs of the properties.
  222. */
  223. public function &doGetProperty($property, $default = null, $doCreate = false)
  224. {
  225. $list = [];
  226. /** @var ObjectInterface $element */
  227. foreach ($this->getIterator() as $id => $element) {
  228. $list[$id] = $element->getProperty($property, $default);
  229. }
  230. return $list;
  231. }
  232. /**
  233. * @param string $property Object property to be updated.
  234. * @param mixed $value New value.
  235. * @return $this
  236. */
  237. public function doSetProperty($property, $value)
  238. {
  239. /** @var ObjectInterface $element */
  240. foreach ($this->getIterator() as $element) {
  241. $element->setProperty($property, $value);
  242. }
  243. return $this;
  244. }
  245. /**
  246. * @param string $property Object property to be updated.
  247. * @return $this
  248. */
  249. public function doUnsetProperty($property)
  250. {
  251. /** @var ObjectInterface $element */
  252. foreach ($this->getIterator() as $element) {
  253. $element->unsetProperty($property);
  254. }
  255. return $this;
  256. }
  257. /**
  258. * @param string $property Object property to be updated.
  259. * @param mixed $default Default value.
  260. * @return $this
  261. */
  262. public function doDefProperty($property, $default)
  263. {
  264. /** @var ObjectInterface $element */
  265. foreach ($this->getIterator() as $element) {
  266. $element->defProperty($property, $default);
  267. }
  268. return $this;
  269. }
  270. /**
  271. * @param string $method Method name.
  272. * @param array $arguments List of arguments passed to the function.
  273. * @return mixed[] Return values.
  274. */
  275. public function call($method, array $arguments = [])
  276. {
  277. $list = [];
  278. /**
  279. * @var string|int $id
  280. * @var ObjectInterface $element
  281. */
  282. foreach ($this->getIterator() as $id => $element) {
  283. $callable = [$element, $method];
  284. $list[$id] = is_callable($callable) ? call_user_func_array($callable, $arguments) : null;
  285. }
  286. return $list;
  287. }
  288. /**
  289. * Group items in the collection by a field and return them as associated array.
  290. *
  291. * @param string $property
  292. * @return array
  293. * @phpstan-return array<TKey,T>
  294. */
  295. public function group($property)
  296. {
  297. $list = [];
  298. /** @var ObjectInterface $element */
  299. foreach ($this->getIterator() as $element) {
  300. $list[(string) $element->getProperty($property)][] = $element;
  301. }
  302. return $list;
  303. }
  304. /**
  305. * Group items in the collection by a field and return them as associated array of collections.
  306. *
  307. * @param string $property
  308. * @return static[]
  309. * @phpstan-return array<static<TKey,T>>
  310. */
  311. public function collectionGroup($property)
  312. {
  313. $collections = [];
  314. foreach ($this->group($property) as $id => $elements) {
  315. /** @phpstan-var static<TKey,T> $collection */
  316. $collection = $this->createFrom($elements);
  317. $collections[$id] = $collection;
  318. }
  319. return $collections;
  320. }
  321. }