PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/gantry5/vendor/rockettheme/toolbox/ArrayTraits/src/NestedArrayAccess.php

https://gitlab.com/ricardosanchez/prueba
PHP | 190 lines | 97 code | 18 blank | 75 comment | 18 complexity | 3f2e898ae5b0f5c30d856d736fbc6658 MD5 | raw file
  1. <?php
  2. namespace RocketTheme\Toolbox\ArrayTraits;
  3. /**
  4. * Implements nested ArrayAccess interface with dot notation.
  5. *
  6. * @package RocketTheme\Toolbox\ArrayTraits
  7. * @author RocketTheme
  8. * @license MIT
  9. *
  10. * @property array $items
  11. */
  12. trait NestedArrayAccess
  13. {
  14. protected $nestedSeparator = '.';
  15. /**
  16. * Get value by using dot notation for nested arrays/objects.
  17. *
  18. * @example $value = $this->get('this.is.my.nested.variable');
  19. *
  20. * @param string $name Dot separated path to the requested value.
  21. * @param mixed $default Default value (or null).
  22. * @param string $separator Separator, defaults to '.'
  23. * @return mixed Value.
  24. */
  25. public function get($name, $default = null, $separator = null)
  26. {
  27. $path = explode($separator ?: $this->nestedSeparator, $name);
  28. $current = $this->items;
  29. foreach ($path as $field) {
  30. if (is_object($current) && isset($current->{$field})) {
  31. $current = $current->{$field};
  32. } elseif (is_array($current) && isset($current[$field])) {
  33. $current = $current[$field];
  34. } else {
  35. return $default;
  36. }
  37. }
  38. return $current;
  39. }
  40. /**
  41. * Set value by using dot notation for nested arrays/objects.
  42. *
  43. * @example $data->set('this.is.my.nested.variable', $value);
  44. *
  45. * @param string $name Dot separated path to the requested value.
  46. * @param mixed $value New value.
  47. * @param string $separator Separator, defaults to '.'
  48. * @return $this
  49. */
  50. public function set($name, $value, $separator = null)
  51. {
  52. $path = explode($separator ?: $this->nestedSeparator, $name);
  53. $current = &$this->items;
  54. foreach ($path as $field) {
  55. if (is_object($current)) {
  56. // Handle objects.
  57. if (!isset($current->{$field})) {
  58. $current->{$field} = array();
  59. }
  60. $current = &$current->{$field};
  61. } else {
  62. // Handle arrays and scalars.
  63. if (!is_array($current)) {
  64. $current = array($field => array());
  65. } elseif (!isset($current[$field])) {
  66. $current[$field] = array();
  67. }
  68. $current = &$current[$field];
  69. }
  70. }
  71. $current = $value;
  72. return $this;
  73. }
  74. /**
  75. * Unset value by using dot notation for nested arrays/objects.
  76. *
  77. * @example $data->undef('this.is.my.nested.variable');
  78. *
  79. * @param string $name Dot separated path to the requested value.
  80. * @param string $separator Separator, defaults to '.'
  81. * @return $this
  82. */
  83. public function undef($name, $separator = null)
  84. {
  85. if ($name === '') {
  86. $this->items = [];
  87. return $this;
  88. }
  89. $path = explode($separator ?: $this->nestedSeparator, $name);
  90. $var = array_pop($path);
  91. $current = &$this->items;
  92. foreach ($path as $field) {
  93. if (is_object($current)) {
  94. // Handle objects.
  95. if (!isset($current->{$field})) {
  96. return $this;
  97. }
  98. $current = &$current->{$field};
  99. } else {
  100. // Handle arrays and scalars.
  101. if (!is_array($current) || !isset($current[$field])) {
  102. return $this;
  103. }
  104. $current = &$current[$field];
  105. }
  106. }
  107. unset($current[$var]);
  108. return $this;
  109. }
  110. /**
  111. * Set default value by using dot notation for nested arrays/objects.
  112. *
  113. * @example $data->def('this.is.my.nested.variable', 'default');
  114. *
  115. * @param string $name Dot separated path to the requested value.
  116. * @param mixed $default Default value (or null).
  117. * @param string $separator Separator, defaults to '.'
  118. * @return $this
  119. */
  120. public function def($name, $default = null, $separator = null)
  121. {
  122. $this->set($name, $this->get($name, $default, $separator), $separator);
  123. return $this;
  124. }
  125. /**
  126. * Whether or not an offset exists.
  127. *
  128. * @param mixed $offset An offset to check for.
  129. * @return bool Returns TRUE on success or FALSE on failure.
  130. */
  131. public function offsetExists($offset)
  132. {
  133. return $this->get($offset) !== null;
  134. }
  135. /**
  136. * Returns the value at specified offset.
  137. *
  138. * @param mixed $offset The offset to retrieve.
  139. * @return mixed Can return all value types.
  140. */
  141. public function offsetGet($offset)
  142. {
  143. return $this->get($offset);
  144. }
  145. /**
  146. * Assigns a value to the specified offset.
  147. *
  148. * @param mixed $offset The offset to assign the value to.
  149. * @param mixed $value The value to set.
  150. */
  151. public function offsetSet($offset, $value)
  152. {
  153. if (is_null($offset)) {
  154. $this->items[] = $value;
  155. } else {
  156. $this->set($offset, $value);
  157. }
  158. }
  159. /**
  160. * Unsets variable at specified offset.
  161. *
  162. * @param $offset
  163. */
  164. public function offsetUnset($offset)
  165. {
  166. if (is_null($offset)) {
  167. $this->items[] = [];
  168. } else {
  169. $this->undef($offset);
  170. }
  171. }
  172. }