ObjectCollectionTrait.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @package Grav\Framework\Object
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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\Object\Interfaces\ObjectInterface;
  10. /**
  11. * ObjectCollection Trait
  12. * @package Grav\Framework\Object
  13. */
  14. trait ObjectCollectionTrait
  15. {
  16. use ObjectTrait {
  17. setKey as public;
  18. }
  19. /**
  20. * Create a copy from this collection by cloning all objects in the collection.
  21. *
  22. * @return static
  23. */
  24. public function copy()
  25. {
  26. $list = [];
  27. foreach ($this->getIterator() as $key => $value) {
  28. $list[$key] = \is_object($value) ? clone $value : $value;
  29. }
  30. return $this->createFrom($list);
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function getObjectKeys()
  36. {
  37. return $this->call('getKey');
  38. }
  39. /**
  40. * @param string $property Object property to be matched.
  41. * @return bool[] Key/Value pairs of the properties.
  42. */
  43. public function doHasProperty($property)
  44. {
  45. $list = [];
  46. /** @var ObjectInterface $element */
  47. foreach ($this->getIterator() as $id => $element) {
  48. $list[$id] = $element->hasProperty($property);
  49. }
  50. return $list;
  51. }
  52. /**
  53. * @param string $property Object property to be fetched.
  54. * @param mixed $default Default value if not set.
  55. * @param bool $doCreate Not being used.
  56. * @return mixed[] Key/Value pairs of the properties.
  57. */
  58. public function &doGetProperty($property, $default = null, $doCreate = false)
  59. {
  60. $list = [];
  61. /** @var ObjectInterface $element */
  62. foreach ($this->getIterator() as $id => $element) {
  63. $list[$id] = $element->getProperty($property, $default);
  64. }
  65. return $list;
  66. }
  67. /**
  68. * @param string $property Object property to be updated.
  69. * @param mixed $value New value.
  70. * @return $this
  71. */
  72. public function doSetProperty($property, $value)
  73. {
  74. /** @var ObjectInterface $element */
  75. foreach ($this->getIterator() as $element) {
  76. $element->setProperty($property, $value);
  77. }
  78. return $this;
  79. }
  80. /**
  81. * @param string $property Object property to be updated.
  82. * @return $this
  83. */
  84. public function doUnsetProperty($property)
  85. {
  86. /** @var ObjectInterface $element */
  87. foreach ($this->getIterator() as $element) {
  88. $element->unsetProperty($property);
  89. }
  90. return $this;
  91. }
  92. /**
  93. * @param string $property Object property to be updated.
  94. * @param mixed $default Default value.
  95. * @return $this
  96. */
  97. public function doDefProperty($property, $default)
  98. {
  99. /** @var ObjectInterface $element */
  100. foreach ($this->getIterator() as $element) {
  101. $element->defProperty($property, $default);
  102. }
  103. return $this;
  104. }
  105. /**
  106. * @param string $method Method name.
  107. * @param array $arguments List of arguments passed to the function.
  108. * @return mixed[] Return values.
  109. */
  110. public function call($method, array $arguments = [])
  111. {
  112. $list = [];
  113. /**
  114. * @var string|int $id
  115. * @var ObjectInterface $element
  116. */
  117. foreach ($this->getIterator() as $id => $element) {
  118. $callable = method_exists($element, $method) ? [$element, $method] : null;
  119. $list[$id] = $callable ? \call_user_func_array($callable, $arguments) : null;
  120. }
  121. return $list;
  122. }
  123. /**
  124. * Group items in the collection by a field and return them as associated array.
  125. *
  126. * @param string $property
  127. * @return array
  128. */
  129. public function group($property)
  130. {
  131. $list = [];
  132. /** @var ObjectInterface $element */
  133. foreach ($this->getIterator() as $element) {
  134. $list[(string) $element->getProperty($property)][] = $element;
  135. }
  136. return $list;
  137. }
  138. /**
  139. * Group items in the collection by a field and return them as associated array of collections.
  140. *
  141. * @param string $property
  142. * @return static[]
  143. */
  144. public function collectionGroup($property)
  145. {
  146. $collections = [];
  147. foreach ($this->group($property) as $id => $elements) {
  148. $collection = $this->createFrom($elements);
  149. $collections[$id] = $collection;
  150. }
  151. return $collections;
  152. }
  153. /**
  154. * @return \Traversable
  155. */
  156. abstract public function getIterator();
  157. }