Model.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs\Internal\Common;
  17. class Model implements \ArrayAccess, \IteratorAggregate, \Countable, ToArrayInterface
  18. {
  19. protected $data;
  20. public function __construct(array $data = [])
  21. {
  22. $this->data = $data;
  23. }
  24. public function count()
  25. {
  26. return count($this->data);
  27. }
  28. public function getIterator()
  29. {
  30. return new \ArrayIterator($this->data);
  31. }
  32. public function toArray()
  33. {
  34. return $this->data;
  35. }
  36. public function clear()
  37. {
  38. $this->data = [];
  39. return $this;
  40. }
  41. public function getAll(array $keys = null)
  42. {
  43. return $keys ? array_intersect_key($this->data, array_flip($keys)) : $this->data;
  44. }
  45. public function get($key)
  46. {
  47. return isset($this->data[$key]) ? $this->data[$key] : null;
  48. }
  49. public function set($key, $value)
  50. {
  51. $this->data[$key] = $value;
  52. return $this;
  53. }
  54. public function add($key, $value)
  55. {
  56. if (!array_key_exists($key, $this->data)) {
  57. $this->data[$key] = $value;
  58. } elseif (is_array($this->data[$key])) {
  59. $this->data[$key][] = $value;
  60. } else {
  61. $this->data[$key] = [$this->data[$key], $value];
  62. }
  63. return $this;
  64. }
  65. public function remove($key)
  66. {
  67. unset($this->data[$key]);
  68. return $this;
  69. }
  70. public function getKeys()
  71. {
  72. return array_keys($this->data);
  73. }
  74. public function hasKey($key)
  75. {
  76. return array_key_exists($key, $this->data);
  77. }
  78. public function keySearch($key)
  79. {
  80. foreach (array_keys($this->data) as $k) {
  81. if (!strcasecmp($k, $key)) {
  82. return $k;
  83. }
  84. }
  85. return false;
  86. }
  87. public function hasValue($value)
  88. {
  89. return array_search($value, $this->data);
  90. }
  91. public function replace(array $data)
  92. {
  93. $this->data = $data;
  94. return $this;
  95. }
  96. public function merge($data)
  97. {
  98. foreach ($data as $key => $value) {
  99. $this->add($key, $value);
  100. }
  101. return $this;
  102. }
  103. public function overwriteWith($data)
  104. {
  105. if (is_array($data)) {
  106. $this->data = $data + $this->data;
  107. } else {
  108. foreach ($data as $key => $value) {
  109. $this->data[$key] = $value;
  110. }
  111. }
  112. return $this;
  113. }
  114. public function map(\Closure $closure, array $context = [], $static = true)
  115. {
  116. $collection = $static ? new static() : new self();
  117. foreach ($this as $key => $value) {
  118. $collection->add($key, $closure($key, $value, $context));
  119. }
  120. return $collection;
  121. }
  122. public function filter(\Closure $closure, $static = true)
  123. {
  124. $collection = ($static) ? new static() : new self();
  125. foreach ($this->data as $key => $value) {
  126. if ($closure($key, $value)) {
  127. $collection->add($key, $value);
  128. }
  129. }
  130. return $collection;
  131. }
  132. public function offsetExists($offset)
  133. {
  134. return isset($this->data[$offset]);
  135. }
  136. public function offsetGet($offset)
  137. {
  138. return isset($this->data[$offset]) ? $this->data[$offset] : null;
  139. }
  140. public function offsetSet($offset, $value)
  141. {
  142. $this->data[$offset] = $value;
  143. }
  144. public function offsetUnset($offset)
  145. {
  146. unset($this->data[$offset]);
  147. }
  148. public function setPath($path, $value)
  149. {
  150. $current =& $this->data;
  151. $queue = explode('/', $path);
  152. while (null !== ($key = array_shift($queue))) {
  153. if (!is_array($current)) {
  154. throw new \RuntimeException("Trying to setPath {$path}, but {$key} is set and is not an array");
  155. } elseif (!$queue) {
  156. $current[$key] = $value;
  157. } elseif (isset($current[$key])) {
  158. $current =& $current[$key];
  159. } else {
  160. $current[$key] = [];
  161. $current =& $current[$key];
  162. }
  163. }
  164. return $this;
  165. }
  166. public function getPath($path, $separator = '/', $data = null)
  167. {
  168. if ($data === null) {
  169. $data =& $this->data;
  170. }
  171. $path = is_array($path) ? $path : explode($separator, $path);
  172. while (null !== ($part = array_shift($path))) {
  173. if (!is_array($data)) {
  174. return null;
  175. } elseif (isset($data[$part])) {
  176. $data =& $data[$part];
  177. } elseif ($part != '*') {
  178. return null;
  179. } else {
  180. // Perform a wildcard search by diverging and merging paths
  181. $result = [];
  182. foreach ($data as $value) {
  183. if (!$path) {
  184. $result = array_merge_recursive($result, (array) $value);
  185. } elseif (null !== ($test = $this->getPath($path, $separator, $value))) {
  186. $result = array_merge_recursive($result, (array) $test);
  187. }
  188. }
  189. return $result;
  190. }
  191. }
  192. return $data;
  193. }
  194. public function __toString()
  195. {
  196. $output = 'Debug output of ';
  197. $output .= 'model';
  198. $output = str_repeat('=', strlen($output)) . "\n" . $output . "\n" . str_repeat('=', strlen($output)) . "\n\n";
  199. $output .= "Model data\n-----------\n\n";
  200. $output .= "This data can be retrieved from the model object using the get() method of the model "
  201. . "(e.g. \$model->get(\$key)) or accessing the model like an associative array (e.g. \$model['key']).\n\n";
  202. $lines = array_slice(explode("\n", trim(print_r($this->toArray(), true))), 2, -1);
  203. $output .= implode("\n", $lines);
  204. return $output . "\n";
  205. }
  206. }