HasDataTrait.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace AlibabaCloud\Client\Traits;
  3. use Adbar\Dot;
  4. use ArrayIterator;
  5. use JmesPath\Env as JmesPath;
  6. use AlibabaCloud\Client\Result\Result;
  7. /**
  8. * Trait HasDataTrait
  9. *
  10. * @package AlibabaCloud\Client\Traits
  11. * @mixin Result
  12. */
  13. trait HasDataTrait
  14. {
  15. /**
  16. * Instance of the Dot.
  17. *
  18. * @var Dot
  19. */
  20. protected $dot;
  21. /**
  22. * @param string $expression
  23. *
  24. * @return mixed|null
  25. */
  26. public function search($expression)
  27. {
  28. return JmesPath::search($expression, $this->dot->all());
  29. }
  30. /**
  31. * Delete the contents of a given key or keys
  32. *
  33. * @param array|int|string|null $keys
  34. */
  35. public function clear($keys = null)
  36. {
  37. $this->dot->clear($keys);
  38. }
  39. /**
  40. * Flatten an array with the given character as a key delimiter
  41. *
  42. * @param string $delimiter
  43. * @param array|null $items
  44. * @param string $prepend
  45. *
  46. * @return array
  47. */
  48. public function flatten($delimiter = '.', $items = null, $prepend = '')
  49. {
  50. return $this->dot->flatten($delimiter, $items, $prepend);
  51. }
  52. /**
  53. * Return the value of a given key
  54. *
  55. * @param int|string|null $key
  56. * @param mixed $default
  57. *
  58. * @return mixed
  59. */
  60. public function get($key = null, $default = null)
  61. {
  62. return $this->dot->get($key, $default);
  63. }
  64. /**
  65. * Set a given key / value pair or pairs
  66. *
  67. * @param array|int|string $keys
  68. * @param mixed $value
  69. */
  70. public function set($keys, $value = null)
  71. {
  72. $this->dot->set($keys, $value);
  73. }
  74. /**
  75. * Check if a given key or keys are empty
  76. *
  77. * @param array|int|string|null $keys
  78. *
  79. * @return bool
  80. */
  81. public function isEmpty($keys = null)
  82. {
  83. return $this->dot->isEmpty($keys);
  84. }
  85. /**
  86. * Replace all items with a given array as a reference
  87. *
  88. * @param array $items
  89. */
  90. public function setReference(array &$items)
  91. {
  92. $this->dot->setReference($items);
  93. }
  94. /**
  95. * Return the value of a given key or all the values as JSON
  96. *
  97. * @param mixed $key
  98. * @param int $options
  99. *
  100. * @return string
  101. */
  102. public function toJson($key = null, $options = 0)
  103. {
  104. return $this->dot->toJson($key, $options);
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function toArray()
  110. {
  111. return $this->dot->all();
  112. }
  113. /**
  114. * Check if a given key exists
  115. *
  116. * @param int|string $key
  117. *
  118. * @return bool
  119. */
  120. public function offsetExists($key)
  121. {
  122. return $this->dot->has($key);
  123. }
  124. /**
  125. * Return the value of a given key
  126. *
  127. * @param int|string $key
  128. *
  129. * @return mixed
  130. */
  131. public function offsetGet($key)
  132. {
  133. return $this->dot->offsetGet($key);
  134. }
  135. /**
  136. * Set a given value to the given key
  137. *
  138. * @param int|string|null $key
  139. * @param mixed $value
  140. */
  141. public function offsetSet($key, $value)
  142. {
  143. $this->dot->offsetSet($key, $value);
  144. }
  145. /**
  146. * Delete the given key
  147. *
  148. * @param int|string $key
  149. */
  150. public function offsetUnset($key)
  151. {
  152. $this->delete($key);
  153. }
  154. /**
  155. * Delete the given key or keys
  156. *
  157. * @param array|int|string $keys
  158. */
  159. public function delete($keys)
  160. {
  161. $this->dot->delete($keys);
  162. }
  163. /*
  164. * --------------------------------------------------------------
  165. * ArrayAccess interface
  166. * --------------------------------------------------------------
  167. */
  168. /**
  169. * Return the number of items in a given key
  170. *
  171. * @param int|string|null $key
  172. *
  173. * @return int
  174. */
  175. public function count($key = null)
  176. {
  177. return $this->dot->count($key);
  178. }
  179. /**
  180. * Get an iterator for the stored items
  181. *
  182. * @return ArrayIterator
  183. */
  184. public function getIterator()
  185. {
  186. return $this->dot->getIterator();
  187. }
  188. /**
  189. * Return items for JSON serialization
  190. *
  191. * @return array
  192. */
  193. public function jsonSerialize()
  194. {
  195. return $this->dot->jsonSerialize();
  196. }
  197. /**
  198. * @param string $name
  199. *
  200. * @return mixed|null
  201. */
  202. public function __get($name)
  203. {
  204. if (!isset($this->all()[$name])) {
  205. return null;
  206. }
  207. return \json_decode(\json_encode($this->all()))->$name;
  208. }
  209. /*
  210. * --------------------------------------------------------------
  211. * Countable interface
  212. * --------------------------------------------------------------
  213. */
  214. /**
  215. * Return all the stored items
  216. *
  217. * @return array
  218. */
  219. public function all()
  220. {
  221. return $this->dot->all();
  222. }
  223. /**
  224. * @param string $name
  225. * @param mixed $value
  226. */
  227. public function __set($name, $value)
  228. {
  229. $this->add($name, $value);
  230. }
  231. /**
  232. * Set a given key / value pair or pairs
  233. * if the key doesn't exist already
  234. *
  235. * @param array|int|string $keys
  236. * @param mixed $value
  237. */
  238. public function add($keys, $value = null)
  239. {
  240. $this->dot->add($keys, $value);
  241. }
  242. /*
  243. * --------------------------------------------------------------
  244. * ObjectAccess
  245. * --------------------------------------------------------------
  246. */
  247. /**
  248. * @param string $name
  249. *
  250. * @return bool
  251. */
  252. public function __isset($name)
  253. {
  254. return $this->has($name);
  255. }
  256. /**
  257. * Check if a given key or keys exists
  258. *
  259. * @param array|int|string $keys
  260. *
  261. * @return bool
  262. */
  263. public function has($keys)
  264. {
  265. return $this->dot->has($keys);
  266. }
  267. /**
  268. * @param $name
  269. *
  270. * @return void
  271. */
  272. public function __unset($name)
  273. {
  274. $this->delete($name);
  275. }
  276. /**
  277. * @param array $data
  278. */
  279. protected function dot(array $data = [])
  280. {
  281. $this->dot = new Dot($data);
  282. }
  283. }