Response.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. namespace AlibabaCloud\Tea;
  3. use Adbar\Dot;
  4. use ArrayAccess;
  5. use Countable;
  6. use GuzzleHttp\Psr7\Response as PsrResponse;
  7. use GuzzleHttp\TransferStats;
  8. use IteratorAggregate;
  9. use Psr\Http\Message\ResponseInterface;
  10. use Psr\Http\Message\StreamInterface;
  11. /**
  12. * Class Response.
  13. */
  14. class Response extends PsrResponse implements ArrayAccess, IteratorAggregate, Countable
  15. {
  16. public $headers = [];
  17. public $statusCode;
  18. public $statusMessage = '';
  19. /**
  20. * @var TransferStats
  21. */
  22. public static $info;
  23. /**
  24. * @var StreamInterface
  25. */
  26. public $body;
  27. /**
  28. * Instance of the Dot.
  29. *
  30. * @var Dot
  31. */
  32. protected $dot;
  33. /**
  34. * Response constructor.
  35. */
  36. public function __construct(ResponseInterface $response)
  37. {
  38. parent::__construct(
  39. $response->getStatusCode(),
  40. $response->getHeaders(),
  41. $response->getBody(),
  42. $response->getProtocolVersion(),
  43. $response->getReasonPhrase()
  44. );
  45. $this->headers = $response->getHeaders();
  46. $this->body = $response->getBody();
  47. $this->statusCode = $response->getStatusCode();
  48. if ($this->body->isSeekable()) {
  49. $this->body->seek(0);
  50. }
  51. if (Helper::isJson((string) $this->getBody())) {
  52. $this->dot = new Dot($this->toArray());
  53. } else {
  54. $this->dot = new Dot();
  55. }
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function __toString()
  61. {
  62. return (string) $this->getBody();
  63. }
  64. /**
  65. * @param string $name
  66. *
  67. * @return null|mixed
  68. */
  69. public function __get($name)
  70. {
  71. $data = $this->dot->all();
  72. if (!isset($data[$name])) {
  73. return null;
  74. }
  75. return json_decode(json_encode($data))->{$name};
  76. }
  77. /**
  78. * @param string $name
  79. * @param mixed $value
  80. */
  81. public function __set($name, $value)
  82. {
  83. $this->dot->set($name, $value);
  84. }
  85. /**
  86. * @param string $name
  87. *
  88. * @return bool
  89. */
  90. public function __isset($name)
  91. {
  92. return $this->dot->has($name);
  93. }
  94. /**
  95. * @param $offset
  96. */
  97. public function __unset($offset)
  98. {
  99. $this->dot->delete($offset);
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function toArray()
  105. {
  106. return \GuzzleHttp\json_decode((string) $this->getBody(), true);
  107. }
  108. /**
  109. * @param array|int|string $keys
  110. * @param mixed $value
  111. */
  112. public function add($keys, $value = null)
  113. {
  114. return $this->dot->add($keys, $value);
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function all()
  120. {
  121. return $this->dot->all();
  122. }
  123. /**
  124. * @param null|array|int|string $keys
  125. */
  126. public function clear($keys = null)
  127. {
  128. return $this->dot->clear($keys);
  129. }
  130. /**
  131. * @param array|int|string $keys
  132. */
  133. public function delete($keys)
  134. {
  135. return $this->dot->delete($keys);
  136. }
  137. /**
  138. * @param string $delimiter
  139. * @param null|array $items
  140. * @param string $prepend
  141. *
  142. * @return array
  143. */
  144. public function flatten($delimiter = '.', $items = null, $prepend = '')
  145. {
  146. return $this->dot->flatten($delimiter, $items, $prepend);
  147. }
  148. /**
  149. * @param null|int|string $key
  150. * @param mixed $default
  151. *
  152. * @return mixed
  153. */
  154. public function get($key = null, $default = null)
  155. {
  156. return $this->dot->get($key, $default);
  157. }
  158. /**
  159. * @param array|int|string $keys
  160. *
  161. * @return bool
  162. */
  163. public function has($keys)
  164. {
  165. return $this->dot->has($keys);
  166. }
  167. /**
  168. * @param null|array|int|string $keys
  169. *
  170. * @return bool
  171. */
  172. public function isEmpty($keys = null)
  173. {
  174. return $this->dot->isEmpty($keys);
  175. }
  176. /**
  177. * @param array|self|string $key
  178. * @param array|self $value
  179. */
  180. public function merge($key, $value = [])
  181. {
  182. return $this->dot->merge($key, $value);
  183. }
  184. /**
  185. * @param array|self|string $key
  186. * @param array|self $value
  187. */
  188. public function mergeRecursive($key, $value = [])
  189. {
  190. return $this->dot->mergeRecursive($key, $value);
  191. }
  192. /**
  193. * @param array|self|string $key
  194. * @param array|self $value
  195. */
  196. public function mergeRecursiveDistinct($key, $value = [])
  197. {
  198. return $this->dot->mergeRecursiveDistinct($key, $value);
  199. }
  200. /**
  201. * @param null|int|string $key
  202. * @param mixed $default
  203. *
  204. * @return mixed
  205. */
  206. public function pull($key = null, $default = null)
  207. {
  208. return $this->dot->pull($key, $default);
  209. }
  210. /**
  211. * @param null|int|string $key
  212. * @param mixed $value
  213. *
  214. * @return mixed
  215. */
  216. public function push($key = null, $value = null)
  217. {
  218. return $this->dot->push($key, $value);
  219. }
  220. /**
  221. * Replace all values or values within the given key
  222. * with an array or Dot object.
  223. *
  224. * @param array|self|string $key
  225. * @param array|self $value
  226. */
  227. public function replace($key, $value = [])
  228. {
  229. return $this->dot->replace($key, $value);
  230. }
  231. /**
  232. * Set a given key / value pair or pairs.
  233. *
  234. * @param array|int|string $keys
  235. * @param mixed $value
  236. */
  237. public function set($keys, $value = null)
  238. {
  239. return $this->dot->set($keys, $value);
  240. }
  241. /**
  242. * Replace all items with a given array.
  243. *
  244. * @param mixed $items
  245. */
  246. public function setArray($items)
  247. {
  248. return $this->dot->setArray($items);
  249. }
  250. /**
  251. * Replace all items with a given array as a reference.
  252. */
  253. public function setReference(array &$items)
  254. {
  255. return $this->dot->setReference($items);
  256. }
  257. /**
  258. * Return the value of a given key or all the values as JSON.
  259. *
  260. * @param mixed $key
  261. * @param int $options
  262. *
  263. * @return string
  264. */
  265. public function toJson($key = null, $options = 0)
  266. {
  267. return $this->dot->toJson($key, $options);
  268. }
  269. /**
  270. * Retrieve an external iterator.
  271. */
  272. #[\ReturnTypeWillChange]
  273. public function getIterator()
  274. {
  275. return $this->dot->getIterator();
  276. }
  277. /**
  278. * Whether a offset exists.
  279. *
  280. * @param $offset
  281. *
  282. * @return bool
  283. */
  284. #[\ReturnTypeWillChange]
  285. public function offsetExists($offset)
  286. {
  287. return $this->dot->offsetExists($offset);
  288. }
  289. /**
  290. * Offset to retrieve.
  291. *
  292. * @param $offset
  293. *
  294. * @return mixed
  295. */
  296. #[\ReturnTypeWillChange]
  297. public function offsetGet($offset)
  298. {
  299. return $this->dot->offsetGet($offset);
  300. }
  301. /**
  302. * Offset to set.
  303. *
  304. * @param $offset
  305. * @param $value
  306. */
  307. #[\ReturnTypeWillChange]
  308. public function offsetSet($offset, $value)
  309. {
  310. $this->dot->offsetSet($offset, $value);
  311. }
  312. /**
  313. * Offset to unset.
  314. *
  315. * @param $offset
  316. */
  317. #[\ReturnTypeWillChange]
  318. public function offsetUnset($offset)
  319. {
  320. $this->dot->offsetUnset($offset);
  321. }
  322. /**
  323. * Count elements of an object.
  324. *
  325. * @param null $key
  326. *
  327. * @return int
  328. */
  329. #[\ReturnTypeWillChange]
  330. public function count($key = null)
  331. {
  332. return $this->dot->count($key);
  333. }
  334. }