1 | <?php |
||
14 | class Headers |
||
15 | { |
||
16 | /** |
||
17 | * List of headers name that should not be normalized. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $ignoreProcessing = [ |
||
22 | "user-agent" => true |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Header data |
||
27 | * |
||
28 | * @var array|null |
||
29 | */ |
||
30 | private $data; |
||
31 | |||
32 | /** |
||
33 | * Constructor |
||
34 | * |
||
35 | * @param array|null $headers |
||
36 | * |
||
37 | */ |
||
38 | 69 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * Add header value |
||
50 | * If the header already exists data are merged, method DOES NOT replace previous value. |
||
51 | * |
||
52 | * @param string $name Name of the header. |
||
53 | * @param mixed $value Value of the header can be scalar data type or string. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | 2 | public function add($name, $value) |
|
58 | { |
||
59 | 2 | $name = $this->normalizeName($name); |
|
60 | 2 | $newValues = is_array($value) ? $value : [$value]; |
|
61 | 2 | $data = []; |
|
62 | 2 | if (isset($this->data[$name])) { |
|
63 | 1 | $data = $this->data[$name]; |
|
64 | 1 | } |
|
65 | |||
66 | 2 | $this->data[$name] = array_merge($data, $newValues); |
|
67 | 2 | } |
|
68 | |||
69 | /** |
||
70 | * Set header value |
||
71 | * If the header already exist value will be replaced. |
||
72 | * |
||
73 | * @param string $name Name of the header. |
||
74 | * @param mixed $value Value of the header can be scalar data type or string. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 13 | public function set($name, $value) |
|
84 | |||
85 | /** |
||
86 | * Get header by provided name |
||
87 | * |
||
88 | * @param string $name Name of the header. |
||
89 | * |
||
90 | * @return mixed value of header |
||
91 | */ |
||
92 | 9 | public function get($name) |
|
102 | |||
103 | /** |
||
104 | * Get header values separated by comma |
||
105 | * |
||
106 | * @param string $name Name of the header. |
||
107 | * |
||
108 | * @return string value of header separated by comma |
||
109 | */ |
||
110 | 2 | public function getLine($name) |
|
116 | |||
117 | /** |
||
118 | * Get all headers |
||
119 | * |
||
120 | * @return array value of all headers |
||
121 | */ |
||
122 | 8 | public function all() |
|
126 | |||
127 | /** |
||
128 | * Remove header by provided name |
||
129 | * |
||
130 | * @param string $name Name of the header. |
||
131 | * |
||
132 | * @return self |
||
133 | */ |
||
134 | 3 | public function remove($name) |
|
143 | |||
144 | /** |
||
145 | * Check if header exist |
||
146 | * |
||
147 | * @param string $name Name of the header. |
||
148 | * |
||
149 | * @return boolean Returns TRUE if exists or FALSE if not. |
||
150 | */ |
||
151 | 16 | public function exists($name) |
|
155 | |||
156 | // ------------ PRIVATE METHODS |
||
157 | |||
158 | /** |
||
159 | * Normalize headers values |
||
160 | * |
||
161 | * @param array $headers Headers for normalization. |
||
162 | * |
||
163 | * @return array Normalized header data. |
||
164 | */ |
||
165 | 69 | private function normalizeHeaders($headers) |
|
175 | |||
176 | /** |
||
177 | * Normalize header value |
||
178 | * |
||
179 | * @param string $name Name of header - some headers should not be normalized. |
||
180 | * @param string $data Header data for normalization. |
||
181 | * |
||
182 | * @return array Normalized value of header |
||
183 | */ |
||
184 | 17 | private function normalizeData($name, $data) |
|
192 | |||
193 | /** |
||
194 | * Normalize header name |
||
195 | * |
||
196 | * @param string $name Name of header for normalization |
||
197 | * |
||
198 | * @return string Normalized name of header |
||
199 | */ |
||
200 | 30 | private function normalizeName($name) |
|
209 | } |
||
210 |