Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Client;
4
 
5
use ArrayAccess;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Traits\Macroable;
8
use LogicException;
9
 
10
class Response implements ArrayAccess
11
{
12
    use Macroable {
13
        __call as macroCall;
14
    }
15
 
16
    /**
17
     * The underlying PSR response.
18
     *
19
     * @var \Psr\Http\Message\ResponseInterface
20
     */
21
    protected $response;
22
 
23
    /**
24
     * The decoded JSON response.
25
     *
26
     * @var array
27
     */
28
    protected $decoded;
29
 
30
    /**
31
     * The request cookies.
32
     *
33
     * @var \GuzzleHttp\Cookie\CookieJar
34
     */
35
    public $cookies;
36
 
37
    /**
38
     * The transfer stats for the request.
39
     *
40
     * @var \GuzzleHttp\TransferStats|null
41
     */
42
    public $transferStats;
43
 
44
    /**
45
     * Create a new response instance.
46
     *
47
     * @param  \Psr\Http\Message\MessageInterface  $response
48
     * @return void
49
     */
50
    public function __construct($response)
51
    {
52
        $this->response = $response;
53
    }
54
 
55
    /**
56
     * Get the body of the response.
57
     *
58
     * @return string
59
     */
60
    public function body()
61
    {
62
        return (string) $this->response->getBody();
63
    }
64
 
65
    /**
66
     * Get the JSON decoded body of the response as an array or scalar value.
67
     *
68
     * @param  string|null  $key
69
     * @param  mixed  $default
70
     * @return mixed
71
     */
72
    public function json($key = null, $default = null)
73
    {
74
        if (! $this->decoded) {
75
            $this->decoded = json_decode($this->body(), true);
76
        }
77
 
78
        if (is_null($key)) {
79
            return $this->decoded;
80
        }
81
 
82
        return data_get($this->decoded, $key, $default);
83
    }
84
 
85
    /**
86
     * Get the JSON decoded body of the response as an object.
87
     *
88
     * @return object|array
89
     */
90
    public function object()
91
    {
92
        return json_decode($this->body(), false);
93
    }
94
 
95
    /**
96
     * Get the JSON decoded body of the response as a collection.
97
     *
98
     * @param  string|null  $key
99
     * @return \Illuminate\Support\Collection
100
     */
101
    public function collect($key = null)
102
    {
103
        return Collection::make($this->json($key));
104
    }
105
 
106
    /**
107
     * Get a header from the response.
108
     *
109
     * @param  string  $header
110
     * @return string
111
     */
112
    public function header(string $header)
113
    {
114
        return $this->response->getHeaderLine($header);
115
    }
116
 
117
    /**
118
     * Get the headers from the response.
119
     *
120
     * @return array
121
     */
122
    public function headers()
123
    {
124
        return $this->response->getHeaders();
125
    }
126
 
127
    /**
128
     * Get the status code of the response.
129
     *
130
     * @return int
131
     */
132
    public function status()
133
    {
134
        return (int) $this->response->getStatusCode();
135
    }
136
 
137
    /**
138
     * Get the reason phrase of the response.
139
     *
140
     * @return string
141
     */
142
    public function reason()
143
    {
144
        return $this->response->getReasonPhrase();
145
    }
146
 
147
    /**
148
     * Get the effective URI of the response.
149
     *
150
     * @return \Psr\Http\Message\UriInterface|null
151
     */
152
    public function effectiveUri()
153
    {
154
        return $this->transferStats?->getEffectiveUri();
155
    }
156
 
157
    /**
158
     * Determine if the request was successful.
159
     *
160
     * @return bool
161
     */
162
    public function successful()
163
    {
164
        return $this->status() >= 200 && $this->status() < 300;
165
    }
166
 
167
    /**
168
     * Determine if the response code was "OK".
169
     *
170
     * @return bool
171
     */
172
    public function ok()
173
    {
174
        return $this->status() === 200;
175
    }
176
 
177
    /**
178
     * Determine if the response was a redirect.
179
     *
180
     * @return bool
181
     */
182
    public function redirect()
183
    {
184
        return $this->status() >= 300 && $this->status() < 400;
185
    }
186
 
187
    /**
188
     * Determine if the response was a 401 "Unauthorized" response.
189
     *
190
     * @return bool
191
     */
192
    public function unauthorized()
193
    {
194
        return $this->status() === 401;
195
    }
196
 
197
    /**
198
     * Determine if the response was a 403 "Forbidden" response.
199
     *
200
     * @return bool
201
     */
202
    public function forbidden()
203
    {
204
        return $this->status() === 403;
205
    }
206
 
207
    /**
208
     * Determine if the response indicates a client or server error occurred.
209
     *
210
     * @return bool
211
     */
212
    public function failed()
213
    {
214
        return $this->serverError() || $this->clientError();
215
    }
216
 
217
    /**
218
     * Determine if the response indicates a client error occurred.
219
     *
220
     * @return bool
221
     */
222
    public function clientError()
223
    {
224
        return $this->status() >= 400 && $this->status() < 500;
225
    }
226
 
227
    /**
228
     * Determine if the response indicates a server error occurred.
229
     *
230
     * @return bool
231
     */
232
    public function serverError()
233
    {
234
        return $this->status() >= 500;
235
    }
236
 
237
    /**
238
     * Execute the given callback if there was a server or client error.
239
     *
240
     * @param  callable  $callback
241
     * @return $this
242
     */
243
    public function onError(callable $callback)
244
    {
245
        if ($this->failed()) {
246
            $callback($this);
247
        }
248
 
249
        return $this;
250
    }
251
 
252
    /**
253
     * Get the response cookies.
254
     *
255
     * @return \GuzzleHttp\Cookie\CookieJar
256
     */
257
    public function cookies()
258
    {
259
        return $this->cookies;
260
    }
261
 
262
    /**
263
     * Get the handler stats of the response.
264
     *
265
     * @return array
266
     */
267
    public function handlerStats()
268
    {
269
        return $this->transferStats?->getHandlerStats() ?? [];
270
    }
271
 
272
    /**
273
     * Close the stream and any underlying resources.
274
     *
275
     * @return $this
276
     */
277
    public function close()
278
    {
279
        $this->response->getBody()->close();
280
 
281
        return $this;
282
    }
283
 
284
    /**
285
     * Get the underlying PSR response for the response.
286
     *
287
     * @return \Psr\Http\Message\ResponseInterface
288
     */
289
    public function toPsrResponse()
290
    {
291
        return $this->response;
292
    }
293
 
294
    /**
295
     * Create an exception if a server or client error occurred.
296
     *
297
     * @return \Illuminate\Http\Client\RequestException|null
298
     */
299
    public function toException()
300
    {
301
        if ($this->failed()) {
302
            return new RequestException($this);
303
        }
304
    }
305
 
306
    /**
307
     * Throw an exception if a server or client error occurred.
308
     *
309
     * @param  \Closure|null  $callback
310
     * @return $this
311
     *
312
     * @throws \Illuminate\Http\Client\RequestException
313
     */
314
    public function throw()
315
    {
316
        $callback = func_get_args()[0] ?? null;
317
 
318
        if ($this->failed()) {
319
            throw tap($this->toException(), function ($exception) use ($callback) {
320
                if ($callback && is_callable($callback)) {
321
                    $callback($this, $exception);
322
                }
323
            });
324
        }
325
 
326
        return $this;
327
    }
328
 
329
    /**
330
     * Throw an exception if a server or client error occurred and the given condition evaluates to true.
331
     *
332
     * @param  \Closure|bool  $condition
333
     * @param  \Closure|null  $throwCallback
334
     * @return $this
335
     *
336
     * @throws \Illuminate\Http\Client\RequestException
337
     */
338
    public function throwIf($condition)
339
    {
340
        return value($condition, $this) ? $this->throw(func_get_args()[1] ?? null) : $this;
341
    }
342
 
343
    /**
344
     * Determine if the given offset exists.
345
     *
346
     * @param  string  $offset
347
     * @return bool
348
     */
349
    public function offsetExists($offset): bool
350
    {
351
        return isset($this->json()[$offset]);
352
    }
353
 
354
    /**
355
     * Get the value for a given offset.
356
     *
357
     * @param  string  $offset
358
     * @return mixed
359
     */
360
    public function offsetGet($offset): mixed
361
    {
362
        return $this->json()[$offset];
363
    }
364
 
365
    /**
366
     * Set the value at the given offset.
367
     *
368
     * @param  string  $offset
369
     * @param  mixed  $value
370
     * @return void
371
     *
372
     * @throws \LogicException
373
     */
374
    public function offsetSet($offset, $value): void
375
    {
376
        throw new LogicException('Response data may not be mutated using array access.');
377
    }
378
 
379
    /**
380
     * Unset the value at the given offset.
381
     *
382
     * @param  string  $offset
383
     * @return void
384
     *
385
     * @throws \LogicException
386
     */
387
    public function offsetUnset($offset): void
388
    {
389
        throw new LogicException('Response data may not be mutated using array access.');
390
    }
391
 
392
    /**
393
     * Get the body of the response.
394
     *
395
     * @return string
396
     */
397
    public function __toString()
398
    {
399
        return $this->body();
400
    }
401
 
402
    /**
403
     * Dynamically proxy other methods to the underlying response.
404
     *
405
     * @param  string  $method
406
     * @param  array  $parameters
407
     * @return mixed
408
     */
409
    public function __call($method, $parameters)
410
    {
411
        return static::hasMacro($method)
412
                    ? $this->macroCall($method, $parameters)
413
                    : $this->response->{$method}(...$parameters);
414
    }
415
}