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