-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientResponse.php
More file actions
194 lines (166 loc) · 4.92 KB
/
Copy pathClientResponse.php
File metadata and controls
194 lines (166 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Zero\Lib\Http;
use Stringable;
final class ClientResponse implements Stringable
{
private int $status;
private string $body;
/** @var array<string, array<int, string>> lower-case header name => values */
private array $headers;
private ?string $error;
private mixed $jsonCache = null;
private bool $jsonResolved = false;
public function __construct(int $status, string $body, array $headers = [], ?string $error = null)
{
$this->status = $status;
$this->body = $body;
$this->error = $error;
$this->headers = self::normalizeHeaders($headers);
}
public function status(): int
{
return $this->status;
}
public function body(): string
{
return $this->body;
}
public function json(?string $key = null, mixed $default = null): mixed
{
if (! $this->jsonResolved) {
$decoded = json_decode($this->body, true);
$this->jsonCache = json_last_error() === JSON_ERROR_NONE ? $decoded : null;
$this->jsonResolved = true;
}
if ($key === null) {
return $this->jsonCache;
}
if (! is_array($this->jsonCache)) {
return $default;
}
$segments = explode('.', $key);
$value = $this->jsonCache;
foreach ($segments as $segment) {
if (is_array($value) && array_key_exists($segment, $value)) {
$value = $value[$segment];
} else {
return $default;
}
}
return $value;
}
public function object(): ?object
{
$decoded = json_decode($this->body);
return json_last_error() === JSON_ERROR_NONE && is_object($decoded) ? $decoded : null;
}
/**
* @return array<string, array<int, string>>
*/
public function headers(): array
{
return $this->headers;
}
public function header(string $name): ?string
{
$key = strtolower($name);
if (! isset($this->headers[$key])) {
return null;
}
return implode(', ', $this->headers[$key]);
}
public function ok(): bool
{
return $this->status === 200;
}
public function successful(): bool
{
return $this->status >= 200 && $this->status < 300;
}
public function redirect(): bool
{
return $this->status >= 300 && $this->status < 400;
}
public function failed(): bool
{
return $this->error !== null || $this->serverError() || $this->clientError();
}
public function clientError(): bool
{
return $this->status >= 400 && $this->status < 500;
}
public function serverError(): bool
{
return $this->status >= 500 && $this->status < 600;
}
public function unauthorized(): bool
{
return $this->status === 401;
}
public function forbidden(): bool
{
return $this->status === 403;
}
public function notFound(): bool
{
return $this->status === 404;
}
public function error(): ?string
{
return $this->error;
}
public function throw(): self
{
if ($this->failed() || $this->error !== null) {
throw new \RuntimeException(
$this->error !== null
? "HTTP request error: {$this->error}"
: "HTTP request failed with status {$this->status}",
$this->status > 0 ? $this->status : 0
);
}
return $this;
}
public function toLegacyObject(): object
{
return (object) [
'ok' => $this->successful() && $this->error === null,
'status' => $this->status,
'body' => $this->body === '' && $this->error !== null ? null : $this->body,
'json' => $this->json(),
'error' => $this->error,
];
}
public function __toString(): string
{
return $this->body;
}
/**
* @param array<int, string>|array<string, string|array<int, string>> $headers
* @return array<string, array<int, string>>
*/
private static function normalizeHeaders(array $headers): array
{
$result = [];
foreach ($headers as $key => $value) {
if (is_int($key)) {
if (! is_string($value)) continue;
if (! str_contains($value, ':')) continue;
[$name, $val] = explode(':', $value, 2);
$name = strtolower(trim($name));
$result[$name][] = trim($val);
} else {
$name = strtolower(trim($key));
if (is_array($value)) {
foreach ($value as $v) {
$result[$name][] = (string) $v;
}
} else {
$result[$name][] = (string) $value;
}
}
}
return $result;
}
}