-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathResponse.php
151 lines (123 loc) · 3.47 KB
/
Response.php
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
<?php
namespace Overtrue\CosClient\Http;
use JetBrains\PhpStorm\Pure;
use Overtrue\CosClient\Support\XML;
use Psr\Http\Message\ResponseInterface;
class Response extends \GuzzleHttp\Psr7\Response implements \ArrayAccess, \JsonSerializable
{
protected ?array $arrayResult = null;
public function __construct(ResponseInterface $response)
{
parent::__construct(
$response->getStatusCode(),
$response->getHeaders(),
$response->getBody(),
$response->getProtocolVersion(),
$response->getReasonPhrase()
);
}
public function toArray()
{
if (! \is_null($this->arrayResult)) {
return $this->arrayResult;
}
$contents = $this->getContents();
if (empty($contents)) {
return $this->arrayResult = null;
}
return $this->arrayResult = $this->isXML() ? XML::toArray($contents) : \json_decode($contents, true);
}
public function toObject(): ?object
{
return \json_decode(\json_encode($this->toArray()));
}
public function isXML(): bool
{
return \strpos($this->getHeaderLine('content-type'), 'xml') > 0;
}
public function jsonSerialize(): mixed
{
try {
return $this->toArray();
} catch (\Exception $e) {
return [];
}
}
public function offsetExists(mixed $offset): bool
{
return \array_key_exists($offset, $this->toArray());
}
public function offsetGet(mixed $offset): mixed
{
return $this->toArray()[$offset] ?? null;
}
public function offsetSet(mixed $offset, mixed $value): void
{
}
public function offsetUnset(mixed $offset): void
{
}
public static function create(
$status = 200,
array $headers = [],
$body = null,
$version = '1.1',
$reason = null
): Response {
return new self(new \GuzzleHttp\Psr7\Response($status, $headers, $body, $version, $reason));
}
public function toString(): string
{
return $this->getContents();
}
public function getContents(): string
{
$this->getBody()->rewind();
return $this->getBody()->getContents();
}
#[Pure]
final public function isInformational(): bool
{
return $this->getStatusCode() >= 100 && $this->getStatusCode() < 200;
}
#[Pure]
final public function isSuccessful(): bool
{
return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300;
}
#[Pure]
final public function isRedirection(): bool
{
return $this->getStatusCode() >= 300 && $this->getStatusCode() < 400;
}
#[Pure]
final public function isClientError(): bool
{
return $this->getStatusCode() >= 400 && $this->getStatusCode() < 500;
}
#[Pure]
final public function isServerError(): bool
{
return $this->getStatusCode() >= 500 && $this->getStatusCode() < 600;
}
#[Pure]
final public function isOk(): bool
{
return $this->getStatusCode() === 200;
}
#[Pure]
final public function isForbidden(): bool
{
return $this->getStatusCode() === 403;
}
#[Pure]
final public function isNotFound(): bool
{
return $this->getStatusCode() === 404;
}
#[Pure]
final public function isEmpty(): bool
{
return \in_array($this->getStatusCode(), [204, 304]) || empty($this->getContents());
}
}