|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nymfonya\Component\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Nymfonya\Component\Http\Interfaces\HeadersInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Headers implements HeadersInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* headers list |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $headers; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* instanciate |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
13 |
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
13 |
|
$this->headers = []; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* add one header to header list and returns Headers instance |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $key |
|
32
|
|
|
* @param string $content |
|
33
|
|
|
* @return HeadersInterface |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function add(string $key, string $content): HeadersInterface |
|
36
|
|
|
{ |
|
37
|
4 |
|
$this->headers[$key] = $content; |
|
38
|
4 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* add headers from a header list as key value |
|
43
|
|
|
* and returns Headers instance |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $headers |
|
46
|
|
|
* @return HeadersInterface |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function addMany(array $headers): HeadersInterface |
|
49
|
|
|
{ |
|
50
|
3 |
|
foreach ($headers as $k => $v) { |
|
51
|
3 |
|
$this->add($k, $v); |
|
52
|
|
|
} |
|
53
|
3 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* remove key header from header list |
|
58
|
|
|
* if header list key exists |
|
59
|
|
|
* and returns Headers instance |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $key |
|
62
|
|
|
* @return HeadersInterface |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function remove(string $key): HeadersInterface |
|
65
|
|
|
{ |
|
66
|
1 |
|
if (isset($this->headers[$key])) { |
|
67
|
1 |
|
unset($this->headers[$key]); |
|
68
|
|
|
} |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* returns headers map as assoc array |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public function get(): array |
|
78
|
|
|
{ |
|
79
|
4 |
|
return $this->headers; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* returns raw headers as a normal array |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getRaw(): array |
|
88
|
|
|
{ |
|
89
|
1 |
|
return array_map( |
|
90
|
|
|
function ($key, $val) { |
|
91
|
1 |
|
return $key . ': ' . $val; |
|
92
|
1 |
|
}, |
|
93
|
1 |
|
array_keys($this->headers), |
|
94
|
1 |
|
$this->headers |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* send headers and returns Headers instance |
|
100
|
|
|
* |
|
101
|
|
|
* @return HeadersInterface |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function send(): HeadersInterface |
|
104
|
|
|
{ |
|
105
|
1 |
|
$headers = $this->getRaw(); |
|
106
|
1 |
|
$headersCount = count($headers); |
|
107
|
1 |
|
for ($c = 0; $c < $headersCount; $c++) { |
|
108
|
1 |
|
header($headers[$c]); |
|
109
|
|
|
} |
|
110
|
1 |
|
unset($headersCount); |
|
111
|
1 |
|
unset($headers); |
|
112
|
1 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|