NestedPropertyTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\ObjectInterface;
  10. use RuntimeException;
  11. use stdClass;
  12. use function is_array;
  13. use function is_object;
  14. /**
  15. * Nested Property Object Trait
  16. * @package Grav\Framework\Object\Traits
  17. */
  18. trait NestedPropertyTrait
  19. {
  20. /**
  21. * @param string $property Object property name.
  22. * @param string|null $separator Separator, defaults to '.'
  23. * @return bool True if property has been defined (can be null).
  24. */
  25. public function hasNestedProperty($property, $separator = null)
  26. {
  27. $test = new stdClass;
  28. return $this->getNestedProperty($property, $test, $separator) !== $test;
  29. }
  30. /**
  31. * @param string $property Object property to be fetched.
  32. * @param mixed|null $default Default value if property has not been set.
  33. * @param string|null $separator Separator, defaults to '.'
  34. * @return mixed Property value.
  35. */
  36. public function getNestedProperty($property, $default = null, $separator = null)
  37. {
  38. $separator = $separator ?: '.';
  39. $path = explode($separator, (string) $property);
  40. $offset = array_shift($path);
  41. if (!$this->hasProperty($offset)) {
  42. return $default;
  43. }
  44. $current = $this->getProperty($offset);
  45. while ($path) {
  46. // Get property of nested Object.
  47. if ($current instanceof ObjectInterface) {
  48. if (method_exists($current, 'getNestedProperty')) {
  49. return $current->getNestedProperty(implode($separator, $path), $default, $separator);
  50. }
  51. return $current->getProperty(implode($separator, $path), $default);
  52. }
  53. $offset = array_shift($path);
  54. if ((is_array($current) || is_a($current, 'ArrayAccess')) && isset($current[$offset])) {
  55. $current = $current[$offset];
  56. } elseif (is_object($current) && isset($current->{$offset})) {
  57. $current = $current->{$offset};
  58. } else {
  59. return $default;
  60. }
  61. };
  62. return $current;
  63. }
  64. /**
  65. * @param string $property Object property to be updated.
  66. * @param mixed $value New value.
  67. * @param string|null $separator Separator, defaults to '.'
  68. * @return $this
  69. * @throws RuntimeException
  70. */
  71. public function setNestedProperty($property, $value, $separator = null)
  72. {
  73. $separator = $separator ?: '.';
  74. $path = explode($separator, $property);
  75. $offset = array_shift($path);
  76. if (!$path) {
  77. $this->setProperty($offset, $value);
  78. return $this;
  79. }
  80. $current = &$this->doGetProperty($offset, null, true);
  81. while ($path) {
  82. $offset = array_shift($path);
  83. // Handle arrays and scalars.
  84. if ($current === null) {
  85. $current = [$offset => []];
  86. } elseif (is_array($current)) {
  87. if (!isset($current[$offset])) {
  88. $current[$offset] = [];
  89. }
  90. } else {
  91. throw new RuntimeException("Cannot set nested property {$property} on non-array value");
  92. }
  93. $current = &$current[$offset];
  94. };
  95. $current = $value;
  96. return $this;
  97. }
  98. /**
  99. * @param string $property Object property to be updated.
  100. * @param string|null $separator Separator, defaults to '.'
  101. * @return $this
  102. * @throws RuntimeException
  103. */
  104. public function unsetNestedProperty($property, $separator = null)
  105. {
  106. $separator = $separator ?: '.';
  107. $path = explode($separator, $property);
  108. $offset = array_shift($path);
  109. if (!$path) {
  110. $this->unsetProperty($offset);
  111. return $this;
  112. }
  113. $last = array_pop($path);
  114. $current = &$this->doGetProperty($offset, null, true);
  115. while ($path) {
  116. $offset = array_shift($path);
  117. // Handle arrays and scalars.
  118. if ($current === null) {
  119. return $this;
  120. }
  121. if (is_array($current)) {
  122. if (!isset($current[$offset])) {
  123. return $this;
  124. }
  125. } else {
  126. throw new RuntimeException("Cannot unset nested property {$property} on non-array value");
  127. }
  128. $current = &$current[$offset];
  129. };
  130. unset($current[$last]);
  131. return $this;
  132. }
  133. /**
  134. * @param string $property Object property to be updated.
  135. * @param mixed $default Default value.
  136. * @param string|null $separator Separator, defaults to '.'
  137. * @return $this
  138. * @throws RuntimeException
  139. */
  140. public function defNestedProperty($property, $default, $separator = null)
  141. {
  142. if (!$this->hasNestedProperty($property, $separator)) {
  143. $this->setNestedProperty($property, $default, $separator);
  144. }
  145. return $this;
  146. }
  147. }