1 | <?php |
||
24 | final class Response |
||
25 | { |
||
26 | /** |
||
27 | * The parsed response body. |
||
28 | * |
||
29 | * @var mixed |
||
30 | */ |
||
31 | public $body; |
||
32 | |||
33 | /** |
||
34 | * The response status code. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | private $statusCode; |
||
39 | |||
40 | /** |
||
41 | * The raw response body. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $rawBody; |
||
46 | |||
47 | /** |
||
48 | * The HTTP headers. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $headers; |
||
53 | |||
54 | /** |
||
55 | * The processed result. |
||
56 | * |
||
57 | * @var mixed |
||
58 | */ |
||
59 | private $result; |
||
60 | |||
61 | /** |
||
62 | * The request object. |
||
63 | * |
||
64 | * @var Request |
||
65 | */ |
||
66 | private $request; |
||
67 | |||
68 | /** |
||
69 | * The constructor. |
||
70 | * |
||
71 | * @param Request $request The request object. |
||
72 | * @param int $statusCode The status code. |
||
73 | * @param string $rawBody The raw response body. |
||
74 | * @param array $headers A key => value representation of response headers. |
||
75 | */ |
||
76 | 22 | public function __construct(Request $request, $statusCode, $rawBody, array $headers) |
|
84 | |||
85 | /** |
||
86 | * Checks if the response is okay. |
||
87 | * |
||
88 | * @return bool Whether the response is okay. |
||
89 | */ |
||
90 | 22 | public function ok() |
|
97 | |||
98 | /** |
||
99 | * Get the HTTP status code. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getStatusCode() |
||
104 | { |
||
105 | return $this->statusCode; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get the error message. |
||
110 | * |
||
111 | * @return null|string |
||
112 | */ |
||
113 | public function getError() |
||
117 | |||
118 | /** |
||
119 | * Checks if the response is rate-limited. |
||
120 | * |
||
121 | * @return bool Whether the response is rate-limited. |
||
122 | */ |
||
123 | 22 | public function rateLimited() |
|
127 | |||
128 | /** |
||
129 | * Parse the response json. |
||
130 | * |
||
131 | * @param string $rawBody The raw response body. |
||
132 | * @param bool $toArray Return as array? |
||
133 | * |
||
134 | * @throws MalformedJson |
||
135 | * |
||
136 | * @return mixed The parsed json. |
||
137 | */ |
||
138 | 22 | private function parseJson($rawBody, $toArray = false) |
|
147 | |||
148 | /** |
||
149 | * Get the processed result. |
||
150 | * |
||
151 | * @return mixed The processed result. |
||
152 | */ |
||
153 | 20 | public function result() |
|
157 | |||
158 | /** |
||
159 | * Set the processed result. |
||
160 | * |
||
161 | * @param mixed $result The result. |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | 20 | public function setResult($result) |
|
171 | |||
172 | /** |
||
173 | * Get the request object. |
||
174 | * |
||
175 | * @return Request |
||
176 | */ |
||
177 | public function getRequest() |
||
181 | |||
182 | /** |
||
183 | * Get the HTTP Headers. |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getHeaders() |
||
191 | |||
192 | /** |
||
193 | * Get a specific HTTP header. |
||
194 | * |
||
195 | * @param string $header |
||
196 | * |
||
197 | * @return string|null The header value. |
||
198 | */ |
||
199 | public function getHeader($header) |
||
205 | |||
206 | /** |
||
207 | * Get the rate-limit. |
||
208 | * |
||
209 | * @return int|null The rate-limit. |
||
210 | */ |
||
211 | public function getRateLimit() |
||
217 | |||
218 | /** |
||
219 | * Get the remaining requests before reaching the rate-limit. |
||
220 | * |
||
221 | * @return int|null The remaining requests. |
||
222 | */ |
||
223 | public function getRemainingRequests() |
||
229 | } |
||
230 |