123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- namespace AlibabaCloud\Client\Traits;
- use Adbar\Dot;
- use ArrayIterator;
- use JmesPath\Env as JmesPath;
- use AlibabaCloud\Client\Result\Result;
- /**
- * Trait HasDataTrait
- *
- * @package AlibabaCloud\Client\Traits
- * @mixin Result
- */
- trait HasDataTrait
- {
- /**
- * Instance of the Dot.
- *
- * @var Dot
- */
- protected $dot;
- /**
- * @param string $expression
- *
- * @return mixed|null
- */
- public function search($expression)
- {
- return JmesPath::search($expression, $this->dot->all());
- }
- /**
- * Delete the contents of a given key or keys
- *
- * @param array|int|string|null $keys
- */
- public function clear($keys = null)
- {
- $this->dot->clear($keys);
- }
- /**
- * Flatten an array with the given character as a key delimiter
- *
- * @param string $delimiter
- * @param array|null $items
- * @param string $prepend
- *
- * @return array
- */
- public function flatten($delimiter = '.', $items = null, $prepend = '')
- {
- return $this->dot->flatten($delimiter, $items, $prepend);
- }
- /**
- * Return the value of a given key
- *
- * @param int|string|null $key
- * @param mixed $default
- *
- * @return mixed
- */
- public function get($key = null, $default = null)
- {
- return $this->dot->get($key, $default);
- }
- /**
- * Set a given key / value pair or pairs
- *
- * @param array|int|string $keys
- * @param mixed $value
- */
- public function set($keys, $value = null)
- {
- $this->dot->set($keys, $value);
- }
- /**
- * Check if a given key or keys are empty
- *
- * @param array|int|string|null $keys
- *
- * @return bool
- */
- public function isEmpty($keys = null)
- {
- return $this->dot->isEmpty($keys);
- }
- /**
- * Replace all items with a given array as a reference
- *
- * @param array $items
- */
- public function setReference(array &$items)
- {
- $this->dot->setReference($items);
- }
- /**
- * Return the value of a given key or all the values as JSON
- *
- * @param mixed $key
- * @param int $options
- *
- * @return string
- */
- public function toJson($key = null, $options = 0)
- {
- return $this->dot->toJson($key, $options);
- }
- /**
- * @return array
- */
- public function toArray()
- {
- return $this->dot->all();
- }
- /**
- * Check if a given key exists
- *
- * @param int|string $key
- *
- * @return bool
- */
- public function offsetExists($key)
- {
- return $this->dot->has($key);
- }
- /**
- * Return the value of a given key
- *
- * @param int|string $key
- *
- * @return mixed
- */
- public function offsetGet($key)
- {
- return $this->dot->offsetGet($key);
- }
- /**
- * Set a given value to the given key
- *
- * @param int|string|null $key
- * @param mixed $value
- */
- public function offsetSet($key, $value)
- {
- $this->dot->offsetSet($key, $value);
- }
- /**
- * Delete the given key
- *
- * @param int|string $key
- */
- public function offsetUnset($key)
- {
- $this->delete($key);
- }
- /**
- * Delete the given key or keys
- *
- * @param array|int|string $keys
- */
- public function delete($keys)
- {
- $this->dot->delete($keys);
- }
- /*
- * --------------------------------------------------------------
- * ArrayAccess interface
- * --------------------------------------------------------------
- */
- /**
- * Return the number of items in a given key
- *
- * @param int|string|null $key
- *
- * @return int
- */
- public function count($key = null)
- {
- return $this->dot->count($key);
- }
- /**
- * Get an iterator for the stored items
- *
- * @return ArrayIterator
- */
- public function getIterator()
- {
- return $this->dot->getIterator();
- }
- /**
- * Return items for JSON serialization
- *
- * @return array
- */
- public function jsonSerialize()
- {
- return $this->dot->jsonSerialize();
- }
- /**
- * @param string $name
- *
- * @return mixed|null
- */
- public function __get($name)
- {
- if (!isset($this->all()[$name])) {
- return null;
- }
- return \json_decode(\json_encode($this->all()))->$name;
- }
- /*
- * --------------------------------------------------------------
- * Countable interface
- * --------------------------------------------------------------
- */
- /**
- * Return all the stored items
- *
- * @return array
- */
- public function all()
- {
- return $this->dot->all();
- }
- /**
- * @param string $name
- * @param mixed $value
- */
- public function __set($name, $value)
- {
- $this->add($name, $value);
- }
- /**
- * Set a given key / value pair or pairs
- * if the key doesn't exist already
- *
- * @param array|int|string $keys
- * @param mixed $value
- */
- public function add($keys, $value = null)
- {
- $this->dot->add($keys, $value);
- }
- /*
- * --------------------------------------------------------------
- * ObjectAccess
- * --------------------------------------------------------------
- */
- /**
- * @param string $name
- *
- * @return bool
- */
- public function __isset($name)
- {
- return $this->has($name);
- }
- /**
- * Check if a given key or keys exists
- *
- * @param array|int|string $keys
- *
- * @return bool
- */
- public function has($keys)
- {
- return $this->dot->has($keys);
- }
- /**
- * @param $name
- *
- * @return void
- */
- public function __unset($name)
- {
- $this->delete($name);
- }
- /**
- * @param array $data
- */
- protected function dot(array $data = [])
- {
- $this->dot = new Dot($data);
- }
- }
|