1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jenky\Hermes; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
6
|
|
|
use Illuminate\Support\Traits\Macroable; |
7
|
|
|
use Jenky\Hermes\Contracts\HttpResponseHandler; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
class Response extends GuzzleResponse implements HttpResponseHandler |
11
|
|
|
{ |
12
|
|
|
use Macroable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create new response handler instance. |
16
|
|
|
* |
17
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
18
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
19
|
|
|
*/ |
20
|
|
|
public static function create(ResponseInterface $response): ResponseInterface |
21
|
|
|
{ |
22
|
|
|
$instance = new static( |
23
|
|
|
$response->getStatusCode(), |
24
|
|
|
$response->getHeaders(), |
25
|
|
|
$response->getBody(), |
26
|
|
|
$response->getProtocolVersion(), |
27
|
|
|
$response->getReasonPhrase() |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$instance->transform(); |
31
|
|
|
|
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Transform the response body to native type. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
protected function transform() |
41
|
|
|
{ |
42
|
|
|
// |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve a header from the request. |
47
|
|
|
* |
48
|
|
|
* @param string|null $key |
49
|
|
|
* @param string|array|null $default |
50
|
|
|
* @return string|array|null |
51
|
|
|
*/ |
52
|
|
|
public function header($header = null, $default = null) |
53
|
|
|
{ |
54
|
|
|
if ($header) { |
55
|
|
|
return $this->getHeader($header)[0] ?? $default; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return array_map(function ($values) { |
59
|
|
|
return $values[0] ?? null; |
60
|
|
|
}, $this->getHeaders()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine that request is successful. |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function isSuccessful(): bool |
69
|
|
|
{ |
70
|
|
|
$statusCode = $this->getStatusCode(); |
71
|
|
|
|
72
|
|
|
return ($statusCode >= 200 && $statusCode < 300) || $statusCode == 304; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Determine that request is error. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isError(): bool |
81
|
|
|
{ |
82
|
|
|
return ! $this->isSuccessful(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Checks if HTTP Status code is Information (1xx). |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function isInformational(): bool |
91
|
|
|
{ |
92
|
|
|
return $this->getStatusCode() < 200; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Checks if HTTP Status code is a Redirect (3xx). |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function isRedirect(): bool |
101
|
|
|
{ |
102
|
|
|
$statusCode = $this->getStatusCode(); |
103
|
|
|
|
104
|
|
|
return $statusCode >= 300 && $statusCode < 400; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks if HTTP Status code is a Client Error (4xx). |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isClientError(): bool |
113
|
|
|
{ |
114
|
|
|
$statusCode = $this->getStatusCode(); |
115
|
|
|
|
116
|
|
|
return $statusCode >= 400 && $statusCode < 500; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checks if HTTP Status code is a Server Error (4xx). |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isServerError(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->getStatusCode() >= 500; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|