-
Notifications
You must be signed in to change notification settings - Fork 203
/
Response.php
178 lines (152 loc) · 4.36 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
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
<?php
declare(strict_types=1);
namespace VCR;
use VCR\Util\Assertion;
/**
* Encapsulates a HTTP response.
*/
class Response
{
protected int $statusCode;
protected string $statusMessage = '';
/**
* @var array<string,string>
*/
protected array $headers = [];
protected ?string $body;
/**
* @var array<string,mixed>
*/
protected array $curlInfo = [];
protected mixed $httpVersion = null;
/**
* @param string|array<string, string> $status
* @param array<string,string> $headers
* @param array<string,mixed> $curlInfo
*/
final public function __construct($status, array $headers = [], ?string $body = null, array $curlInfo = [])
{
$this->setStatus($status);
$this->headers = $headers;
$this->body = $body;
$this->curlInfo = $curlInfo;
}
/**
* Returns an array representation of this Response.
*
* @return array<string,mixed> array representation of this Request
*/
public function toArray(): array
{
$body = $this->getBody();
// Base64 encode when binary
if (
null !== $this->getContentType()
&& (
str_contains($this->getContentType(), 'application/x-gzip')
|| 'binary' == $this->getHeader('Content-Transfer-Encoding')
)
) {
$body = base64_encode($body);
}
return array_filter(
[
'status' => [
'code' => $this->statusCode,
'message' => $this->statusMessage,
],
'headers' => $this->getHeaders(),
'body' => $body,
'curl_info' => $this->curlInfo,
]
);
}
/**
* Creates a new Response from a specified array.
*
* @param array<string,mixed> $response array representation of a Response
*
* @return Response A new Response from a specified array
*/
public static function fromArray(array $response): self
{
$body = $response['body'] ?? null;
$gzip = isset($response['headers']['Content-Type'])
&& str_contains($response['headers']['Content-Type'], 'application/x-gzip');
$binary = isset($response['headers']['Content-Transfer-Encoding'])
&& 'binary' == $response['headers']['Content-Transfer-Encoding'];
// Base64 decode when binary
if ($gzip || $binary) {
$body = base64_decode($response['body']);
}
return new static(
$response['status'] ?? 200,
$response['headers'] ?? [],
$body,
$response['curl_info'] ?? []
);
}
public function getBody(): string
{
return $this->body ?: '';
}
/**
* @return array<string,mixed>|mixed|null
*/
public function getCurlInfo(?string $option = null): mixed
{
if (empty($option)) {
return $this->curlInfo;
}
if (!isset($this->curlInfo[$option])) {
return null;
}
return $this->curlInfo[$option];
}
/**
* @return array<string,string>
*/
public function getHeaders(): array
{
return $this->headers;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function getContentType(): ?string
{
return $this->getHeader('Content-Type');
}
public function getHeader(string $key): ?string
{
if (!isset($this->headers[$key])) {
return null;
}
return $this->headers[$key];
}
public function getHttpVersion(): mixed
{
return $this->httpVersion;
}
public function getStatusMessage(): string
{
return $this->statusMessage;
}
/**
* @param string|array<string,mixed> $status
*/
protected function setStatus($status): void
{
if (\is_array($status)) {
$this->statusCode = (int) $status['code'];
$this->statusMessage = $status['message'];
if (!empty($status['http_version'])) {
$this->httpVersion = $status['http_version'];
}
} else {
Assertion::numeric($status, 'Response status must be either an array or a number.');
$this->statusCode = (int) $status;
}
}
}