-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaders.php
More file actions
200 lines (164 loc) · 4.29 KB
/
Headers.php
File metadata and controls
200 lines (164 loc) · 4.29 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
195
196
197
198
199
200
<?php
namespace Gt\Http\Header;
use Countable;
use Gt\TypeSafeGetter\NullableTypeSafeGetter;
use Gt\TypeSafeGetter\TypeSafeGetter;
use Iterator;
/**
* @implements Iterator<int, HeaderLine>
* @SuppressWarnings("TooManyPublicMethods")
*/
class Headers implements Iterator, Countable, TypeSafeGetter {
use NullableTypeSafeGetter;
const NON_COMBINABLE_HEADERS = [
"set-cookie",
];
/** @var HeaderLine[] */
protected array $headerLines = [];
protected int $iteratorIndex;
/** @param array<string, string> $headerArray Associative array of
* headers (key = header name, value = header value).
*/
public function __construct(array $headerArray = []) {
$this->iteratorIndex = 0;
if(!empty($headerArray)) {
$this->fromArray($headerArray);
}
}
/**
* @return array<string, string|array<int, string>> Associative array
* of headers (key = header name, value = header value).
*/
public function asArray(bool $nested = false):array {
$array = [];
foreach($this->headerLines as $header) {
$name = $header->getName();
$nameLower = strtolower($name);
if($nested) {
$array[$name] ??= [];
$array[$name] = array_merge($array[$name], $header->getValues());
continue;
}
if(!array_key_exists($name, $array)) {
$array[$name] = "";
}
if(in_array($nameLower, self::NON_COMBINABLE_HEADERS)) {
if($array[$name] !== "") {
$array[$name] .= "\n";
}
$array[$name] .= $header->getValuesNewlineSeparated();
}
else {
if($array[$name] !== "") {
$array[$name] .= ",";
}
$array[$name] .= $header->getValuesCommaSeparated();
}
}
return $array;
}
/** @param array<string, string|array<int, string>> $headerArray */
public function fromArray(array $headerArray):void {
foreach($headerArray as $key => $value) {
if(!is_array($value)) {
$value = [$value];
}
if(in_array(strtolower($key), self::NON_COMBINABLE_HEADERS)) {
foreach($value as $singleValue) {
array_push($this->headerLines, new HeaderLine($key, $singleValue));
}
}
else {
array_push($this->headerLines, new HeaderLine($key, ...$value));
}
}
}
public function contains(string $name):bool {
foreach($this->headerLines as $line) {
if($line->isNamed($name)) {
return true;
}
}
return false;
}
public function add(string $name, string...$values):void {
if(in_array(strtolower($name), self::NON_COMBINABLE_HEADERS)) {
foreach($values as $value) {
array_push($this->headerLines, new HeaderLine($name, $value));
}
return;
}
$headerLineToAdd = null;
foreach($this->headerLines as $headerLine) {
if(!$headerLine->isNamed($name)) {
continue;
}
$headerLineToAdd = $headerLine;
}
if(is_null($headerLineToAdd)) {
array_push(
$this->headerLines,
new HeaderLine($name, ...$values)
);
}
else {
$headerLineToAdd->addValue(...$values);
}
}
public function set(string $name, string...$value):void {
$this->remove($name);
$this->add($name, ...$value);
}
public function remove(string $name):void {
foreach($this->headerLines as $i => $line) {
if($line->isNamed($name)) {
unset($this->headerLines[$i]);
}
}
}
public function get(string $name):?HeaderLine {
$matchingValues = [];
$headerName = null;
foreach($this->headerLines as $line) {
if($line->isNamed($name)) {
$headerName ??= $line->getName();
$matchingValues = array_merge($matchingValues, $line->getValues());
}
}
if(!$headerName) {
return null;
}
return new HeaderLine($headerName, ...$matchingValues);
}
/** @return array<int, string> */
public function getAll(string $name):array {
$allValues = [];
foreach($this->headerLines as $line) {
if($line->isNamed($name)) {
$allValues = array_merge($allValues, $line->getValues());
}
}
return $allValues;
}
public function getFirst():string {
return $this->headerLines[0] ?? "";
}
public function current():HeaderLine {
return $this->headerLines[$this->iteratorIndex];
}
public function next():void {
$this->iteratorIndex++;
}
public function key():int {
return $this->iteratorIndex;
}
public function valid():bool {
return isset($this->headerLines[$this->iteratorIndex]);
}
public function rewind():void {
$this->iteratorIndex = 0;
}
public function count():int {
return count($this->headerLines);
}
}