Response.php
313 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Wraps wp_remote_request results with JSON decoding, status checks, and dot-notation body access. |
| 5 | * Implements ArrayAccess for convenient response data retrieval. |
| 6 | * Provides ok, failed, and json helper methods for API integrations. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Http\Client |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Http\Client; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use ArrayAccess; |
| 16 | use Kirki\Framework\Concerns\DeepGettable; |
| 17 | use Exception; |
| 18 | use function Kirki\Framework\collection; |
| 19 | \defined('ABSPATH') || exit; |
| 20 | class Response implements ArrayAccess |
| 21 | { |
| 22 | use DeepGettable; |
| 23 | /** |
| 24 | * The raw response data. |
| 25 | * |
| 26 | * @var array|\WP_Error |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected $response; |
| 31 | /** |
| 32 | * The decoded JSON response. |
| 33 | * |
| 34 | * @var array|null |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | protected $decoded; |
| 39 | /** |
| 40 | * The response cookies. |
| 41 | * |
| 42 | * @var array|null |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | protected $cookies; |
| 47 | /** |
| 48 | * Create a new response instance. |
| 49 | * |
| 50 | * @param mixed $response The response instance. |
| 51 | * |
| 52 | * @return void |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | */ |
| 56 | public function __construct($response) |
| 57 | { |
| 58 | $this->response = $response; |
| 59 | } |
| 60 | /** |
| 61 | * Get the body of the response. |
| 62 | * |
| 63 | * @return string |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public function body() |
| 68 | { |
| 69 | return wp_remote_retrieve_body($this->response); |
| 70 | } |
| 71 | /** |
| 72 | * Get the JSON decoded body of the response as an associative array. |
| 73 | * |
| 74 | * @param mixed $key The key. |
| 75 | * @param mixed $default The default. |
| 76 | * |
| 77 | * @return mixed |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | public function json($key = null, $default = null) |
| 82 | { |
| 83 | if (!$this->decoded) { |
| 84 | $this->decoded = \json_decode($this->body(), \true); |
| 85 | } |
| 86 | if (\is_null($key)) { |
| 87 | return $this->decoded; |
| 88 | } |
| 89 | return $this->deep_get($this->decoded, $key, $default); |
| 90 | } |
| 91 | /** |
| 92 | * Get the JSON decoded body of the response as an object. |
| 93 | * |
| 94 | * @return object|null |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | public function object() |
| 99 | { |
| 100 | return \json_decode($this->body(), \false); |
| 101 | } |
| 102 | /** |
| 103 | * Get the JSON decoded body of the response as a collection. |
| 104 | * |
| 105 | * @param mixed $key The key. |
| 106 | * |
| 107 | * @return \Framework\Collections\Collection |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | public function collect($key = null) |
| 112 | { |
| 113 | return collection($this->json($key)); |
| 114 | } |
| 115 | /** |
| 116 | * Get a header from the response. |
| 117 | * |
| 118 | * @param mixed $name The name. |
| 119 | * |
| 120 | * @return string |
| 121 | * |
| 122 | * @since 1.0.0 |
| 123 | */ |
| 124 | public function header($name) |
| 125 | { |
| 126 | return wp_remote_retrieve_header($this->response, $name); |
| 127 | } |
| 128 | /** |
| 129 | * Get all headers from the response. |
| 130 | * |
| 131 | * @return array |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public function headers() |
| 136 | { |
| 137 | return wp_remote_retrieve_headers($this->response)->getAll(); |
| 138 | } |
| 139 | /** |
| 140 | * Get the cookies from the response. |
| 141 | * |
| 142 | * @return array |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | public function cookies() |
| 147 | { |
| 148 | return wp_remote_retrieve_cookies($this->response); |
| 149 | } |
| 150 | /** |
| 151 | * Get the status code of the response. |
| 152 | * |
| 153 | * @return int |
| 154 | * |
| 155 | * @since 1.0.0 |
| 156 | */ |
| 157 | public function status() |
| 158 | { |
| 159 | return (int) wp_remote_retrieve_response_code($this->response); |
| 160 | } |
| 161 | /** |
| 162 | * Get the reason phrase of the response. |
| 163 | * |
| 164 | * @return string |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | public function reason() |
| 169 | { |
| 170 | return wp_remote_retrieve_response_message($this->response); |
| 171 | } |
| 172 | /** |
| 173 | * Determine if the response status code was successful (2xx). |
| 174 | * |
| 175 | * @return bool |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function successful() |
| 180 | { |
| 181 | return $this->status() >= 200 && $this->status() < 300; |
| 182 | } |
| 183 | /** |
| 184 | * Determine if the response status code was a redirection (3xx). |
| 185 | * |
| 186 | * @return bool |
| 187 | * |
| 188 | * @since 1.0.0 |
| 189 | */ |
| 190 | public function redirected() |
| 191 | { |
| 192 | return $this->status() >= 300 && $this->status() < 400; |
| 193 | } |
| 194 | /** |
| 195 | * Determine if the response status code was a client error (4xx). |
| 196 | * |
| 197 | * @return bool |
| 198 | * |
| 199 | * @since 1.0.0 |
| 200 | */ |
| 201 | public function client_error() |
| 202 | { |
| 203 | return $this->status() >= 400 && $this->status() < 500; |
| 204 | } |
| 205 | /** |
| 206 | * Determine if the response status code was a server error (5xx). |
| 207 | * |
| 208 | * @return bool |
| 209 | * |
| 210 | * @since 1.0.0 |
| 211 | */ |
| 212 | public function server_error() |
| 213 | { |
| 214 | return $this->status() >= 500; |
| 215 | } |
| 216 | /** |
| 217 | * Determine if the response status code was a client or server error. |
| 218 | * |
| 219 | * @return bool |
| 220 | * |
| 221 | * @since 1.0.0 |
| 222 | */ |
| 223 | public function failed() |
| 224 | { |
| 225 | return $this->server_error() || $this->client_error(); |
| 226 | } |
| 227 | /** |
| 228 | * Execute the given callback if the response has a failed status code. |
| 229 | * |
| 230 | * @param callable $callback The callback to invoke. |
| 231 | * |
| 232 | * @return $this |
| 233 | * |
| 234 | * @since 1.0.0 |
| 235 | */ |
| 236 | public function on_error(callable $callback) |
| 237 | { |
| 238 | if ($this->failed()) { |
| 239 | $callback($this); |
| 240 | } |
| 241 | return $this; |
| 242 | } |
| 243 | /** |
| 244 | * Determine if the given offset exists in the JSON response. |
| 245 | * |
| 246 | * @param mixed $offset The offset. |
| 247 | * |
| 248 | * @return bool |
| 249 | * |
| 250 | * @since 1.0.0 |
| 251 | */ |
| 252 | public function offsetExists($offset) : bool |
| 253 | { |
| 254 | return isset($this->json()[$offset]); |
| 255 | } |
| 256 | /** |
| 257 | * Get the value at the given offset from the JSON response. |
| 258 | * |
| 259 | * @param mixed $offset The offset. |
| 260 | * |
| 261 | * @return mixed |
| 262 | * |
| 263 | * @since 1.0.0 |
| 264 | */ |
| 265 | #[\ReturnTypeWillChange] |
| 266 | public function offsetGet($offset) |
| 267 | { |
| 268 | return $this->json()[$offset]; |
| 269 | } |
| 270 | /** |
| 271 | * Set the value at the given offset. |
| 272 | * |
| 273 | * @param mixed $offset The offset. |
| 274 | * @param mixed $value The value. |
| 275 | * |
| 276 | * @return void |
| 277 | * |
| 278 | * @throws \Exception |
| 279 | * |
| 280 | * @since 1.0.0 |
| 281 | */ |
| 282 | public function offsetSet($offset, $value) : void |
| 283 | { |
| 284 | throw new Exception('Response data may not be mutated using array access.'); |
| 285 | } |
| 286 | /** |
| 287 | * Unset the value at the given offset. |
| 288 | * |
| 289 | * @param mixed $offset The offset. |
| 290 | * |
| 291 | * @return void |
| 292 | * |
| 293 | * @throws \Exception |
| 294 | * |
| 295 | * @since 1.0.0 |
| 296 | */ |
| 297 | public function offsetUnset($offset) : void |
| 298 | { |
| 299 | throw new Exception('Response data may not be mutated using array access.'); |
| 300 | } |
| 301 | /** |
| 302 | * Get the body of the response as a string. |
| 303 | * |
| 304 | * @return string |
| 305 | * |
| 306 | * @since 1.0.0 |
| 307 | */ |
| 308 | public function __toString() |
| 309 | { |
| 310 | return $this->body(); |
| 311 | } |
| 312 | } |
| 313 |