Response.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace yzh52521\EasyHttp;
  3. use ArrayAccess;
  4. use LogicException;
  5. class Response implements ArrayAccess
  6. {
  7. protected $response;
  8. /**
  9. * The decoded JSON response.
  10. *
  11. * @var array
  12. */
  13. protected $decoded;
  14. public function __construct($response)
  15. {
  16. $this->response = $response;
  17. }
  18. /**
  19. * Get the body of the response.
  20. * @return string
  21. */
  22. public function body()
  23. {
  24. return (string)$this->response->getBody();
  25. }
  26. /**
  27. * Get the Array decoded body of the response.
  28. * @return array|mixed
  29. */
  30. public function array()
  31. {
  32. if (!$this->decoded) {
  33. $this->decoded = json_decode( (string)$this->response->getBody(),true );
  34. }
  35. return $this->decoded;
  36. }
  37. /**
  38. * Get the JSON decoded body of the response.
  39. * @return object|mixed
  40. */
  41. public function json()
  42. {
  43. if (!$this->decoded) {
  44. $this->decoded = json_decode( (string)$this->response->getBody() );
  45. }
  46. return $this->decoded;
  47. }
  48. /**
  49. * Get a header from the response.
  50. * @param string $header
  51. * @return mixed
  52. */
  53. public function header(string $header)
  54. {
  55. return $this->response->getHeaderLine( $header );
  56. }
  57. /**
  58. * Get the headers from the response.
  59. * @return mixed
  60. */
  61. public function headers()
  62. {
  63. return $this->mapWithKeys( $this->response->getHeaders(),function ($v,$k) {
  64. return [$k => $v];
  65. } )->response;
  66. }
  67. /**
  68. * Get the status code of the response.
  69. * @return int
  70. */
  71. public function status()
  72. {
  73. return (int)$this->response->getStatusCode();
  74. }
  75. /**
  76. * Determine if the request was successful.
  77. * @return bool
  78. */
  79. public function successful()
  80. {
  81. return $this->status() >= 200 && $this->status() < 300;
  82. }
  83. /**
  84. * Determine if the response code was "OK".
  85. * @return bool
  86. */
  87. public function ok()
  88. {
  89. return $this->status() === 200;
  90. }
  91. /**
  92. * Determine if the response was a redirect.
  93. * @return bool
  94. */
  95. public function redirect()
  96. {
  97. return $this->status() >= 300 && $this->status() < 400;
  98. }
  99. /**
  100. * Determine if the response indicates a client error occurred.
  101. * @return bool
  102. */
  103. public function clientError()
  104. {
  105. return $this->status() >= 400 && $this->status() < 500;
  106. }
  107. /**
  108. * Determine if the response indicates a server error occurred.
  109. * @return bool
  110. */
  111. public function serverError()
  112. {
  113. return $this->status() >= 500;
  114. }
  115. /**
  116. * Determine if the given offset exists.
  117. *
  118. * @param string $offset
  119. * @return mixed
  120. */
  121. #[\ReturnTypeWillChange]
  122. public function offsetExists($offset)
  123. {
  124. return array_key_exists( $offset,$this->json() );
  125. }
  126. /**
  127. * Get the value for a given offset.
  128. *
  129. * @param string $offset
  130. * @return mixed
  131. */
  132. #[\ReturnTypeWillChange]
  133. public function offsetGet($offset)
  134. {
  135. return $this->json()[$offset];
  136. }
  137. /**
  138. * Set the value at the given offset.
  139. *
  140. * @param string $offset
  141. * @param mixed $value
  142. * @return void
  143. *
  144. * @throws \LogicException
  145. */
  146. #[\ReturnTypeWillChange]
  147. public function offsetSet($offset,$value)
  148. {
  149. throw new LogicException( 'Response data may not be mutated using array access.' );
  150. }
  151. /**
  152. * Unset the value at the given offset.
  153. *
  154. * @param string $offset
  155. * @return void
  156. *
  157. * @throws \LogicException
  158. */
  159. #[\ReturnTypeWillChange]
  160. public function offsetUnset($offset)
  161. {
  162. throw new LogicException( 'Response data may not be mutated using array access.' );
  163. }
  164. /**
  165. * Get the body of the response.
  166. *
  167. * @return string
  168. */
  169. public function __toString()
  170. {
  171. return $this->body();
  172. }
  173. protected function mapWithKeys($items,callable $callback)
  174. {
  175. $result = [];
  176. foreach ( $items as $key => $value ) {
  177. $assoc = $callback( $value,$key );
  178. foreach ( $assoc as $mapKey => $mapValue ) {
  179. $result[$mapKey] = $mapValue;
  180. }
  181. }
  182. return new static( $result );
  183. }
  184. }