Subversion-Projekte lars-tiefland.laravel_shop

Revision

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