NestedPropertyCollectionTrait.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Access;
  9. use Grav\Framework\Object\Interfaces\NestedObjectInterface;
  10. /**
  11. * Nested Properties Collection Trait
  12. * @package Grav\Framework\Object\Properties
  13. */
  14. trait NestedPropertyCollectionTrait
  15. {
  16. /**
  17. * @param string $property Object property to be matched.
  18. * @param string|null $separator Separator, defaults to '.'
  19. * @return array Key/Value pairs of the properties.
  20. */
  21. public function hasNestedProperty($property, $separator = null)
  22. {
  23. $list = [];
  24. /** @var NestedObjectInterface $element */
  25. foreach ($this->getIterator() as $id => $element) {
  26. $list[$id] = $element->hasNestedProperty($property, $separator);
  27. }
  28. return $list;
  29. }
  30. /**
  31. * @param string $property Object property to be fetched.
  32. * @param mixed $default Default value if not set.
  33. * @param string|null $separator Separator, defaults to '.'
  34. * @return array Key/Value pairs of the properties.
  35. */
  36. public function getNestedProperty($property, $default = null, $separator = null)
  37. {
  38. $list = [];
  39. /** @var NestedObjectInterface $element */
  40. foreach ($this->getIterator() as $id => $element) {
  41. $list[$id] = $element->getNestedProperty($property, $default, $separator);
  42. }
  43. return $list;
  44. }
  45. /**
  46. * @param string $property Object property to be updated.
  47. * @param mixed $value New value.
  48. * @param string|null $separator Separator, defaults to '.'
  49. * @return $this
  50. */
  51. public function setNestedProperty($property, $value, $separator = null)
  52. {
  53. /** @var NestedObjectInterface $element */
  54. foreach ($this->getIterator() as $element) {
  55. $element->setNestedProperty($property, $value, $separator);
  56. }
  57. return $this;
  58. }
  59. /**
  60. * @param string $property Object property to be updated.
  61. * @param string|null $separator Separator, defaults to '.'
  62. * @return $this
  63. */
  64. public function unsetNestedProperty($property, $separator = null)
  65. {
  66. /** @var NestedObjectInterface $element */
  67. foreach ($this->getIterator() as $element) {
  68. $element->unsetNestedProperty($property, $separator);
  69. }
  70. return $this;
  71. }
  72. /**
  73. * @param string $property Object property to be updated.
  74. * @param string $default Default value.
  75. * @param string|null $separator Separator, defaults to '.'
  76. * @return $this
  77. */
  78. public function defNestedProperty($property, $default, $separator = null)
  79. {
  80. /** @var NestedObjectInterface $element */
  81. foreach ($this->getIterator() as $element) {
  82. $element->defNestedProperty($property, $default, $separator);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Group items in the collection by a field.
  88. *
  89. * @param string $property Object property to be used to make groups.
  90. * @param string|null $separator Separator, defaults to '.'
  91. * @return array
  92. */
  93. public function group($property, $separator = null)
  94. {
  95. $list = [];
  96. /** @var NestedObjectInterface $element */
  97. foreach ($this->getIterator() as $element) {
  98. $list[(string) $element->getNestedProperty($property, null, $separator)][] = $element;
  99. }
  100. return $list;
  101. }
  102. }