-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeaderTrait.php
227 lines (204 loc) · 6.42 KB
/
HeaderTrait.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php declare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
use Koded\Http\Interfaces\HttpStatus;
trait HeaderTrait
{
/**
* @var array Message headers.
*/
protected array $headers = [];
/**
* @var array Used for case-insensitivity header name checks
*/
protected array $headersMap = [];
public function getHeaders(): array
{
return $this->headers;
}
/**
* @param string $name
*
* @return array<int, string>
*/
public function getHeader($name): array
{
if (false === isset($this->headersMap[$name = \strtolower($name)])) {
return [];
}
$value = $this->headers[$this->headersMap[$name]];
if (\is_string($value)) {
return empty($value) ? [] : [$value];
}
$header = [];
foreach ($value as $v) {
$header[] = \join(',', (array)$v);
}
return $header;
}
public function getHeaderLine($name): string
{
return \join(',', $this->getHeader($name));
}
public function withHeader($name, $value): static
{
$instance = clone $this;
$name = $instance->normalizeHeaderName($name);
$instance->headersMap[\strtolower($name)] = $name;
$instance->headers[$name] = $this->normalizeHeaderValue($name, $value);
return $instance;
}
public function withHeaders(array $headers): static
{
$instance = clone $this;
foreach ($headers as $name => $value) {
$instance->normalizeHeader($name, $value, false);
}
return $instance;
}
public function withoutHeader($name): static
{
$instance = clone $this;
$name = \strtolower($name);
if (isset($instance->headersMap[$name])) {
unset(
$instance->headers[$this->headersMap[$name]],
$instance->headersMap[$name]
);
}
return $instance;
}
public function withAddedHeader($name, $value): static
{
$instance = clone $this;
$name = $instance->normalizeHeaderName($name);
$value = $instance->normalizeHeaderValue($name, $value);
if (isset($instance->headersMap[$header = \strtolower($name)])) {
$header = $instance->headersMap[$header];
$instance->headers[$header] = \array_unique(
@\array_merge_recursive($instance->headers[$header], $value)
);
} else {
$instance->headersMap[$header] = $name;
$instance->headers[$name] = $value;
}
return $instance;
}
public function hasHeader($name): bool
{
return \array_key_exists(\strtolower($name), $this->headersMap);
}
public function replaceHeaders(array $headers): static
{
$instance = clone $this;
$instance->headers = $instance->headersMap = [];
foreach ($headers as $name => $value) {
$instance->normalizeHeader($name, $value, false);
}
return $instance;
}
/**
* Transforms the nested headers as a flatten array.
* This method is not part of the PSR-7.
*
* @return array<int, string>
*/
public function getFlattenedHeaders(): array
{
$flattenHeaders = [];
foreach ($this->headers as $name => $value) {
$flattenHeaders[] = $name . ':' . \join(',', (array)$value);
}
return $flattenHeaders;
}
public function getCanonicalizedHeaders(array $names = []): string
{
if (empty($names)) {
$names = \array_keys($this->headers);
}
if (!$headers = \array_reduce($names, function($list, $name) {
$name = \str_replace('_', '-', $name);
$list[] = \strtolower($name) . ':' . \join(',', $this->getHeader($name));
return $list;
})) {
return '';
}
\sort($headers);
return \join("\n", $headers);
}
/**
* @param string $name
* @param string|string[] $value
* @param bool $skipKey
*
* @return void
*/
protected function normalizeHeader(string $name, array|string $value, bool $skipKey): void
{
$name = \str_replace(["\r", "\n", "\t"], '', \trim($name));
if (false === $skipKey) {
$name = \ucwords(\str_replace('_', '-', \strtolower($name)), '-');
}
$this->headersMap[\strtolower($name)] = $name;
$this->headers[$name] = $this->normalizeHeaderValue($name, $value);
}
/**
* @param array $headers Associative headers array
*
* @return static
*/
protected function setHeaders(array $headers): static
{
try {
foreach ($headers as $name => $value) {
$this->normalizeHeader($name, $value, false);
}
return $this;
} catch (\TypeError $e) {
throw new \InvalidArgumentException($e->getMessage(), HttpStatus::BAD_REQUEST, $e);
}
}
/**
* @param string $name
*
* @return string Normalized header name
*/
protected function normalizeHeaderName(string $name): string
{
$name = \str_replace(["\r", "\n", "\t"], '', \trim($name));
if ('' !== $name) {
return $name;
}
throw new \InvalidArgumentException('Empty header name', HttpStatus::BAD_REQUEST);
}
/**
* @param string $name
* @param string|string[] $value
*
* @return array
*/
protected function normalizeHeaderValue(string $name, mixed $value): array
{
$value = (array)$value;
try {
if (empty($value = \array_map(fn($v): string => \trim(\preg_replace('/\s+/', ' ', $v)), $value))) {
throw new \InvalidArgumentException(
\sprintf('The value for header "%s" cannot be empty', $name),
HttpStatus::BAD_REQUEST);
}
return $value;
} catch (\TypeError $e) {
throw new \InvalidArgumentException(
\sprintf('Invalid value for header "%s", expects a string or array of strings', $name),
HttpStatus::BAD_REQUEST, $e);
}
}
}