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 | * @throws MalformedJson |
||
77 | */ |
||
78 | 24 | public function __construct(Request $request, $statusCode, $rawBody, array $headers) |
|
86 | |||
87 | /** |
||
88 | * Checks if the response is okay. |
||
89 | * |
||
90 | * @return bool Whether the response is okay. |
||
91 | */ |
||
92 | 24 | public function ok() |
|
100 | |||
101 | /** |
||
102 | * Get the HTTP status code. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | public function getStatusCode() |
||
107 | { |
||
108 | return $this->statusCode; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get the error message. |
||
113 | * |
||
114 | * @return null|string |
||
115 | */ |
||
116 | public function getError() |
||
120 | |||
121 | /** |
||
122 | * Checks if the response is rate-limited. |
||
123 | * |
||
124 | * @return bool Whether the response is rate-limited. |
||
125 | */ |
||
126 | 24 | public function rateLimited() |
|
130 | |||
131 | /** |
||
132 | * Parse the response json. |
||
133 | * |
||
134 | * @param string $rawBody The raw response body. |
||
135 | * @param bool $toArray Return as array? |
||
136 | * |
||
137 | * @throws MalformedJson |
||
138 | * |
||
139 | * @return mixed The parsed json. |
||
140 | */ |
||
141 | 24 | private function parseJson($rawBody, $toArray = false) |
|
150 | |||
151 | /** |
||
152 | * Get the processed result. |
||
153 | * |
||
154 | * @return mixed The processed result. |
||
155 | */ |
||
156 | 12 | public function result() |
|
160 | |||
161 | /** |
||
162 | * Set the processed result. |
||
163 | * |
||
164 | * @param mixed $result The result. |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | 10 | public function setResult($result) |
|
174 | |||
175 | /** |
||
176 | * Get the request object. |
||
177 | * |
||
178 | * @return Request |
||
179 | */ |
||
180 | public function getRequest() |
||
184 | |||
185 | /** |
||
186 | * Get the HTTP Headers. |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getHeaders() |
||
194 | |||
195 | /** |
||
196 | * Get a specific HTTP header. |
||
197 | * |
||
198 | * @param string $header |
||
199 | * |
||
200 | * @return string|null The header value. |
||
201 | */ |
||
202 | public function getHeader($header) |
||
213 | |||
214 | /** |
||
215 | * Get the rate-limit. |
||
216 | * |
||
217 | * @return int|null The rate-limit. |
||
218 | */ |
||
219 | public function getRateLimit() |
||
225 | |||
226 | /** |
||
227 | * Get the remaining requests before reaching the rate-limit. |
||
228 | * |
||
229 | * @return int|null The remaining requests. |
||
230 | */ |
||
231 | public function getRemainingRequests() |
||
237 | } |
||
238 |