| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Http; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Response |
| 7 |
* |
| 8 |
* Handles HTTP responses for HTTP Client class, |
| 9 |
* providing methods to access response data. |
| 10 |
*/ |
| 11 |
class Response implements \JsonSerializable |
| 12 |
{ |
| 13 |
/** |
| 14 |
* The HTTP response data. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $response; |
| 19 |
|
| 20 |
/** |
| 21 |
* Create a new Response instance. |
| 22 |
* |
| 23 |
* @param array $response The response data. |
| 24 |
*/ |
| 25 |
public function __construct($response) |
| 26 |
{ |
| 27 |
$this->response = $response; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Convert the response to an array. |
| 32 |
* |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function toArray() |
| 36 |
{ |
| 37 |
return $this->response; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Convert the response object to an array. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
#[\ReturnTypeWillChange] |
| 46 |
public function jsonSerialize() |
| 47 |
{ |
| 48 |
return $this->toArray(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the HTTP response object. |
| 53 |
* |
| 54 |
* @return mixed |
| 55 |
*/ |
| 56 |
public function getHttpResponseObject() |
| 57 |
{ |
| 58 |
return $this->response['http_response']; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Determine if the response is successful (2xx status code). |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public function isOkay() |
| 67 |
{ |
| 68 |
return str_starts_with($this->getCode(), '2'); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Determine if the response is a redirect (3xx status code). |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function isRedirect() |
| 77 |
{ |
| 78 |
$code = $this->getCode(); |
| 79 |
|
| 80 |
$isRedirect = in_array( |
| 81 |
$code, [300, 301, 302, 303, 307], true |
| 82 |
) || ($code > 307 && $code < 400); |
| 83 |
|
| 84 |
return $isRedirect || $this->getHttpResponseObject() |
| 85 |
->get_response_object() |
| 86 |
->redirects > 0; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Throw an exception based on the response status code. |
| 91 |
* |
| 92 |
* @throws \WpOrg\Requests\Exception |
| 93 |
*/ |
| 94 |
public function throw() |
| 95 |
{ |
| 96 |
$class = sprintf( |
| 97 |
'WpOrg\Requests\Exception\Http\Status%d', $this->getCode() |
| 98 |
); |
| 99 |
|
| 100 |
if (!class_exists($class)) { |
| 101 |
$class = 'WpOrg\Requests\Exception\Http\Status\Http'; |
| 102 |
} |
| 103 |
|
| 104 |
throw new $class; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Throw an exception if the given callback returns true. |
| 109 |
* |
| 110 |
* @param callable $callback |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function throwIf(callable $callback) |
| 114 |
{ |
| 115 |
if ($callback($this)) { |
| 116 |
return $this->throw(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the HTTP response status code. |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function getCode() |
| 126 |
{ |
| 127 |
return wp_remote_retrieve_response_code($this->response); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the HTTP response status code (alias for getCode). |
| 132 |
* |
| 133 |
* @return int |
| 134 |
*/ |
| 135 |
public function getStatus() |
| 136 |
{ |
| 137 |
return $this->getCode(); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get the HTTP response status message. |
| 142 |
* |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
public function getMessage() |
| 146 |
{ |
| 147 |
return wp_remote_retrieve_response_message($this->response); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the HTTP response body. |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
public function getBody() |
| 156 |
{ |
| 157 |
return wp_remote_retrieve_body($this->response); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Determine if the response body is JSON. |
| 162 |
* |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
public function isJson() |
| 166 |
{ |
| 167 |
$header = $this->getHeader('content-type'); |
| 168 |
return str_contains($header, 'application/json'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the decoded JSON body. |
| 173 |
* |
| 174 |
* @return array|null |
| 175 |
*/ |
| 176 |
public function getJson() |
| 177 |
{ |
| 178 |
if ($this->isJson()) { |
| 179 |
return json_decode($this->getBody(), true); |
| 180 |
} |
| 181 |
|
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get the headers from the response. |
| 187 |
* |
| 188 |
* @return array |
| 189 |
*/ |
| 190 |
public function getHeaders() |
| 191 |
{ |
| 192 |
return wp_remote_retrieve_headers($this->response); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get a specific header from the response. |
| 197 |
* |
| 198 |
* @param string $key The header key. |
| 199 |
* @return string|null |
| 200 |
*/ |
| 201 |
public function getHeader($key) |
| 202 |
{ |
| 203 |
return wp_remote_retrieve_header($this->response, $key); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get the cookies from the response. |
| 208 |
* |
| 209 |
* @return array |
| 210 |
*/ |
| 211 |
public function getCookies() |
| 212 |
{ |
| 213 |
return wp_remote_retrieve_cookies($this->response); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get a specific cookie from the response. |
| 218 |
* |
| 219 |
* @param string $name The cookie name. |
| 220 |
* @param bool $isObject Whether to return the \WP_Http_Cookie object. |
| 221 |
* @return mixed |
| 222 |
*/ |
| 223 |
public function getCookie($name, $isObject = false) |
| 224 |
{ |
| 225 |
if ($isObject) { |
| 226 |
// Return the \WP_Http_Cookie object |
| 227 |
return wp_remote_retrieve_cookie($this->response, $name); |
| 228 |
} |
| 229 |
|
| 230 |
return wp_remote_retrieve_cookie_value($this->response, $name); |
| 231 |
} |
| 232 |
} |
| 233 |
|