ObjectCollectionTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @package Grav\Framework\Object
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 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 array 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. * @return array Key/Value pairs of the properties.
  56. */
  57. public function doGetProperty($property, $default = null)
  58. {
  59. $list = [];
  60. /** @var ObjectInterface $element */
  61. foreach ($this->getIterator() as $id => $element) {
  62. $list[$id] = $element->getProperty($property, $default);
  63. }
  64. return $list;
  65. }
  66. /**
  67. * @param string $property Object property to be updated.
  68. * @param string $value New value.
  69. * @return $this
  70. */
  71. public function doSetProperty($property, $value)
  72. {
  73. /** @var ObjectInterface $element */
  74. foreach ($this->getIterator() as $element) {
  75. $element->setProperty($property, $value);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * @param string $property Object property to be updated.
  81. * @return $this
  82. */
  83. public function doUnsetProperty($property)
  84. {
  85. /** @var ObjectInterface $element */
  86. foreach ($this->getIterator() as $element) {
  87. $element->unsetProperty($property);
  88. }
  89. return $this;
  90. }
  91. /**
  92. * @param string $property Object property to be updated.
  93. * @param string $default Default value.
  94. * @return $this
  95. */
  96. public function doDefProperty($property, $default)
  97. {
  98. /** @var ObjectInterface $element */
  99. foreach ($this->getIterator() as $element) {
  100. $element->defProperty($property, $default);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * @param string $method Method name.
  106. * @param array $arguments List of arguments passed to the function.
  107. * @return array Return values.
  108. */
  109. public function call($method, array $arguments = [])
  110. {
  111. $list = [];
  112. foreach ($this->getIterator() as $id => $element) {
  113. $list[$id] = method_exists($element, $method)
  114. ? call_user_func_array([$element, $method], $arguments) : null;
  115. }
  116. return $list;
  117. }
  118. /**
  119. * Group items in the collection by a field and return them as associated array.
  120. *
  121. * @param string $property
  122. * @return array
  123. */
  124. public function group($property)
  125. {
  126. $list = [];
  127. /** @var ObjectInterface $element */
  128. foreach ($this->getIterator() as $element) {
  129. $list[(string) $element->getProperty($property)][] = $element;
  130. }
  131. return $list;
  132. }
  133. /**
  134. * Group items in the collection by a field and return them as associated array of collections.
  135. *
  136. * @param string $property
  137. * @return static[]
  138. */
  139. public function collectionGroup($property)
  140. {
  141. $collections = [];
  142. foreach ($this->group($property) as $id => $elements) {
  143. $collection = $this->createFrom($elements);
  144. $collections[$id] = $collection;
  145. }
  146. return $collections;
  147. }
  148. /**
  149. * @return \Traversable
  150. */
  151. abstract public function getIterator();
  152. }