Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Details | Vergleich mit vorheriger | 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
{
399 lars 12
    use Concerns\DeterminesStatusCode, Macroable {
148 lars 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 was a redirect.
169
     *
170
     * @return bool
171
     */
172
    public function redirect()
173
    {
174
        return $this->status() >= 300 && $this->status() < 400;
175
    }
176
 
177
    /**
178
     * Determine if the response indicates a client or server error occurred.
179
     *
180
     * @return bool
181
     */
182
    public function failed()
183
    {
184
        return $this->serverError() || $this->clientError();
185
    }
186
 
187
    /**
188
     * Determine if the response indicates a client error occurred.
189
     *
190
     * @return bool
191
     */
192
    public function clientError()
193
    {
194
        return $this->status() >= 400 && $this->status() < 500;
195
    }
196
 
197
    /**
198
     * Determine if the response indicates a server error occurred.
199
     *
200
     * @return bool
201
     */
202
    public function serverError()
203
    {
204
        return $this->status() >= 500;
205
    }
206
 
207
    /**
208
     * Execute the given callback if there was a server or client error.
209
     *
210
     * @param  callable  $callback
211
     * @return $this
212
     */
213
    public function onError(callable $callback)
214
    {
215
        if ($this->failed()) {
216
            $callback($this);
217
        }
218
 
219
        return $this;
220
    }
221
 
222
    /**
223
     * Get the response cookies.
224
     *
225
     * @return \GuzzleHttp\Cookie\CookieJar
226
     */
227
    public function cookies()
228
    {
229
        return $this->cookies;
230
    }
231
 
232
    /**
233
     * Get the handler stats of the response.
234
     *
235
     * @return array
236
     */
237
    public function handlerStats()
238
    {
239
        return $this->transferStats?->getHandlerStats() ?? [];
240
    }
241
 
242
    /**
243
     * Close the stream and any underlying resources.
244
     *
245
     * @return $this
246
     */
247
    public function close()
248
    {
249
        $this->response->getBody()->close();
250
 
251
        return $this;
252
    }
253
 
254
    /**
255
     * Get the underlying PSR response for the response.
256
     *
257
     * @return \Psr\Http\Message\ResponseInterface
258
     */
259
    public function toPsrResponse()
260
    {
261
        return $this->response;
262
    }
263
 
264
    /**
265
     * Create an exception if a server or client error occurred.
266
     *
267
     * @return \Illuminate\Http\Client\RequestException|null
268
     */
269
    public function toException()
270
    {
271
        if ($this->failed()) {
272
            return new RequestException($this);
273
        }
274
    }
275
 
276
    /**
277
     * Throw an exception if a server or client error occurred.
278
     *
279
     * @param  \Closure|null  $callback
280
     * @return $this
281
     *
282
     * @throws \Illuminate\Http\Client\RequestException
283
     */
284
    public function throw()
285
    {
286
        $callback = func_get_args()[0] ?? null;
287
 
288
        if ($this->failed()) {
289
            throw tap($this->toException(), function ($exception) use ($callback) {
290
                if ($callback && is_callable($callback)) {
291
                    $callback($this, $exception);
292
                }
293
            });
294
        }
295
 
296
        return $this;
297
    }
298
 
299
    /**
300
     * Throw an exception if a server or client error occurred and the given condition evaluates to true.
301
     *
302
     * @param  \Closure|bool  $condition
303
     * @param  \Closure|null  $throwCallback
304
     * @return $this
305
     *
306
     * @throws \Illuminate\Http\Client\RequestException
307
     */
308
    public function throwIf($condition)
309
    {
310
        return value($condition, $this) ? $this->throw(func_get_args()[1] ?? null) : $this;
311
    }
312
 
313
    /**
399 lars 314
     * Throw an exception if the response status code matches the given code.
315
     *
316
     * @param  callable|int  $statusCode
317
     * @return $this
318
     *
319
     * @throws \Illuminate\Http\Client\RequestException
320
     */
321
    public function throwIfStatus($statusCode)
322
    {
323
        if (is_callable($statusCode) &&
324
            $statusCode($this->status(), $this)) {
325
            return $this->throw();
326
        }
327
 
328
        return $this->status() === $statusCode ? $this->throw() : $this;
329
    }
330
 
331
    /**
332
     * Throw an exception unless the response status code matches the given code.
333
     *
334
     * @param  callable|int  $statusCode
335
     * @return $this
336
     *
337
     * @throws \Illuminate\Http\Client\RequestException
338
     */
339
    public function throwUnlessStatus($statusCode)
340
    {
341
        if (is_callable($statusCode) &&
342
            ! $statusCode($this->status(), $this)) {
343
            return $this->throw();
344
        }
345
 
346
        return $this->status() === $statusCode ? $this : $this->throw();
347
    }
348
 
349
    /**
350
     * Throw an exception if the response status code is a 4xx level code.
351
     *
352
     * @return $this
353
     *
354
     * @throws \Illuminate\Http\Client\RequestException
355
     */
356
    public function throwIfClientError()
357
    {
358
        return $this->clientError() ? $this->throw() : $this;
359
    }
360
 
361
    /**
362
     * Throw an exception if the response status code is a 5xx level code.
363
     *
364
     * @return $this
365
     *
366
     * @throws \Illuminate\Http\Client\RequestException
367
     */
368
    public function throwIfServerError()
369
    {
370
        return $this->serverError() ? $this->throw() : $this;
371
    }
372
 
373
    /**
148 lars 374
     * Determine if the given offset exists.
375
     *
376
     * @param  string  $offset
377
     * @return bool
378
     */
379
    public function offsetExists($offset): bool
380
    {
381
        return isset($this->json()[$offset]);
382
    }
383
 
384
    /**
385
     * Get the value for a given offset.
386
     *
387
     * @param  string  $offset
388
     * @return mixed
389
     */
390
    public function offsetGet($offset): mixed
391
    {
392
        return $this->json()[$offset];
393
    }
394
 
395
    /**
396
     * Set the value at the given offset.
397
     *
398
     * @param  string  $offset
399
     * @param  mixed  $value
400
     * @return void
401
     *
402
     * @throws \LogicException
403
     */
404
    public function offsetSet($offset, $value): void
405
    {
406
        throw new LogicException('Response data may not be mutated using array access.');
407
    }
408
 
409
    /**
410
     * Unset the value at the given offset.
411
     *
412
     * @param  string  $offset
413
     * @return void
414
     *
415
     * @throws \LogicException
416
     */
417
    public function offsetUnset($offset): void
418
    {
419
        throw new LogicException('Response data may not be mutated using array access.');
420
    }
421
 
422
    /**
423
     * Get the body of the response.
424
     *
425
     * @return string
426
     */
427
    public function __toString()
428
    {
429
        return $this->body();
430
    }
431
 
432
    /**
433
     * Dynamically proxy other methods to the underlying response.
434
     *
435
     * @param  string  $method
436
     * @param  array  $parameters
437
     * @return mixed
438
     */
439
    public function __call($method, $parameters)
440
    {
441
        return static::hasMacro($method)
442
                    ? $this->macroCall($method, $parameters)
443
                    : $this->response->{$method}(...$parameters);
444
    }
445
}