1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gemz\HttpClient; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class Response |
9
|
|
|
{ |
10
|
|
|
/** @var ResponseInterface */ |
11
|
|
|
protected $response; |
12
|
|
|
|
13
|
|
|
/** @var bool */ |
14
|
|
|
protected $throwErrors; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ResponseInterface $response |
18
|
|
|
* @param bool $throwErrors |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public static function createFromResponse(ResponseInterface $response, $throwErrors = false) |
23
|
|
|
{ |
24
|
|
|
return new self($response, $throwErrors); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ResponseInterface $response |
29
|
|
|
* @param bool $throwErrors |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ResponseInterface $response, $throwErrors = false) |
32
|
|
|
{ |
33
|
|
|
$this->response = $response; |
34
|
|
|
$this->throwErrors = $throwErrors; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return int |
39
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
40
|
|
|
*/ |
41
|
|
|
public function status(): int |
42
|
|
|
{ |
43
|
|
|
return $this->response->getStatusCode(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
49
|
|
|
*/ |
50
|
|
|
public function isSuccess(): bool |
51
|
|
|
{ |
52
|
|
|
return $this->status() >= 200 && $this->status() < 300; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
58
|
|
|
*/ |
59
|
|
|
public function isOk(): bool |
60
|
|
|
{ |
61
|
|
|
return $this->isSuccess(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
67
|
|
|
*/ |
68
|
|
|
public function isRedirect(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->status() >= 300 && $this->status() < 400; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
76
|
|
|
*/ |
77
|
|
|
public function isClientError(): bool |
78
|
|
|
{ |
79
|
|
|
return $this->status() >= 400 && $this->status() < 500; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
85
|
|
|
*/ |
86
|
|
|
public function isServerError(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->status() >= 500; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
*/ |
94
|
|
|
public function response() |
95
|
|
|
{ |
96
|
|
|
return $this->response; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
102
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
103
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
104
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
105
|
|
|
*/ |
106
|
|
|
public function body(): string |
107
|
|
|
{ |
108
|
|
|
return $this->response->getContent($this->throwErrors); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
114
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
115
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
116
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
117
|
|
|
*/ |
118
|
|
|
public function asString(): string |
119
|
|
|
{ |
120
|
|
|
return $this->body(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return mixed |
125
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
126
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
127
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
128
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
129
|
|
|
*/ |
130
|
|
|
public function asObject() |
131
|
|
|
{ |
132
|
|
|
return json_decode($this->body()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return \Illuminate\Support\Collection<mixed> |
|
|
|
|
137
|
|
|
* |
138
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
139
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
140
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
141
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
142
|
|
|
*/ |
143
|
|
|
public function asCollection(): Collection |
144
|
|
|
{ |
145
|
|
|
return collect($this->asArray()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return mixed |
150
|
|
|
* |
151
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
152
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
153
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
154
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
155
|
|
|
*/ |
156
|
|
|
public function asArray() |
157
|
|
|
{ |
158
|
|
|
return json_decode($this->body(), true); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return resource |
163
|
|
|
*/ |
164
|
|
|
public function toStream() |
165
|
|
|
{ |
166
|
|
|
if (method_exists($this->response, 'toStream')) { |
167
|
|
|
return $this->response->toStream($this->throwErrors); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
throw new \BadMethodCallException('method toStream does not exists'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
* |
176
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
177
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
178
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
179
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
180
|
|
|
*/ |
181
|
|
|
public function isJson(): bool |
182
|
|
|
{ |
183
|
|
|
return $this->contentType() == 'application/json'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $header |
188
|
|
|
* |
189
|
|
|
* @return mixed|string |
190
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
191
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
192
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
193
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
194
|
|
|
*/ |
195
|
|
|
public function header(string $header) |
196
|
|
|
{ |
197
|
|
|
if (array_key_exists($header, $this->headers())) { |
198
|
|
|
return $this->headers()[$header]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return ''; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array<mixed> $headers |
206
|
|
|
* |
207
|
|
|
* @return array<String> |
208
|
|
|
*/ |
209
|
|
|
protected function normalizeHeaders(array $headers) |
210
|
|
|
{ |
211
|
|
|
return collect($headers)->transform(function ($item, $key) { |
|
|
|
|
212
|
|
|
return $item[0]; |
213
|
|
|
})->all(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return array<String> |
218
|
|
|
* |
219
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
220
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
221
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
222
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
223
|
|
|
*/ |
224
|
|
|
public function headers(): array |
225
|
|
|
{ |
226
|
|
|
return $this->normalizeHeaders($this->response->getHeaders()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function requestUrl(): string |
233
|
|
|
{ |
234
|
|
|
return $this->info()['url'] ?: ''; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return float |
239
|
|
|
*/ |
240
|
|
|
public function executionTime(): float |
241
|
|
|
{ |
242
|
|
|
return $this->info()['total_time'] ?: 0.0; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return array|mixed|null |
247
|
|
|
*/ |
248
|
|
|
public function customData() |
249
|
|
|
{ |
250
|
|
|
return $this->info()['user_data']; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return array|mixed|null |
255
|
|
|
*/ |
256
|
|
|
public function info() |
257
|
|
|
{ |
258
|
|
|
return $this->response->getInfo(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return mixed|string |
263
|
|
|
* |
264
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
265
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
266
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
267
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
268
|
|
|
*/ |
269
|
|
|
public function contentType() |
270
|
|
|
{ |
271
|
|
|
return $this->header('content-type'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return mixed|string |
276
|
|
|
* |
277
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
278
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
279
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
280
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
281
|
|
|
*/ |
282
|
|
|
public function userAgent() |
283
|
|
|
{ |
284
|
|
|
return $this->header('user-agent'); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.