Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
90 / 90 |
|
100.00% |
23 / 23 |
CRAP | |
100.00% |
1 / 1 |
Header | |
100.00% |
90 / 90 |
|
100.00% |
23 / 23 |
50 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
parse | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
9 | |||
parseParameter | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
setName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addParameters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addParameter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParametersAsString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
hasParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasParameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setWrap | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getWrap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasWrap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setIndent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getIndent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasIndent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAttachment | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
render | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
10 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Mime\Part; |
15 | |
16 | /** |
17 | * MIME part header class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Mime |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 1.1.0 |
25 | */ |
26 | class Header |
27 | { |
28 | |
29 | /** |
30 | * Header name |
31 | * @var string |
32 | */ |
33 | protected $name = null; |
34 | |
35 | /** |
36 | * Header value |
37 | * @var string|array |
38 | */ |
39 | protected $value = null; |
40 | |
41 | /** |
42 | * Header parameters |
43 | * @var array |
44 | */ |
45 | protected $parameters = []; |
46 | |
47 | /** |
48 | * Header wrap |
49 | * @var int |
50 | */ |
51 | protected $wrap = 0; |
52 | |
53 | /** |
54 | * Header wrap indent |
55 | * @var string |
56 | */ |
57 | protected $indent = "\t"; |
58 | |
59 | /** |
60 | * Constructor |
61 | * |
62 | * Instantiate the header object |
63 | * |
64 | * @param string $name |
65 | * @param string $value |
66 | * @param array $parameters |
67 | */ |
68 | public function __construct($name = null, $value = null, array $parameters = []) |
69 | { |
70 | if (null !== $name) { |
71 | $this->setName($name); |
72 | } |
73 | if (null !== $value) { |
74 | $this->setValue($value); |
75 | } |
76 | if (!empty($parameters)) { |
77 | $this->addParameters($parameters); |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * Parse header |
83 | * |
84 | * @param string $header |
85 | * @return Header |
86 | */ |
87 | public static function parse($header) |
88 | { |
89 | $name = trim(substr($header, 0, strpos($header, ':'))); |
90 | $value = null; |
91 | $parameters = []; |
92 | |
93 | // Handle multiple values |
94 | if (substr_count($header, $name) > 1) { |
95 | $values = array_map('trim', array_filter(explode($name . ':', $header))); |
96 | foreach ($values as $i => $value) { |
97 | if (strpos($value, ';') !== false) { |
98 | $params = array_map('trim', explode(';', trim(substr($value, (strpos($value, ';') + 1))))); |
99 | $values[$i] = trim(substr($value, 0, strpos($value, ';'))); |
100 | foreach ($params as $param) { |
101 | if (strpos($param, '=') !== false) { |
102 | [$paramName, $paramValue] = self::parseParameter($param); |
103 | $parameters[$paramName] = $paramValue; |
104 | } |
105 | } |
106 | } |
107 | } |
108 | return new self($name, $values, $parameters); |
109 | } else { |
110 | if (strpos($header, ';') !== false) { |
111 | $value = substr($header, (strpos($header, ':') + 1)); |
112 | $value = trim(substr($value, 0, strpos($value, ';'))); |
113 | $params = array_map('trim', explode(';', trim(substr($header, (strpos($header, ';') + 1))))); |
114 | foreach ($params as $param) { |
115 | if (strpos($param, '=') !== false) { |
116 | [$paramName, $paramValue] = self::parseParameter($param); |
117 | $parameters[$paramName] = $paramValue; |
118 | } |
119 | } |
120 | } else { |
121 | $value = trim(substr($header, (strpos($header, ':') + 1))); |
122 | } |
123 | |
124 | return new self($name, $value, $parameters); |
125 | } |
126 | } |
127 | |
128 | /** |
129 | * Render the header string |
130 | * |
131 | * @param string $parameter |
132 | * @return array |
133 | */ |
134 | public static function parseParameter($parameter) |
135 | { |
136 | $paramName = substr($parameter, 0, strpos($parameter, '=')); |
137 | $paramValue = substr($parameter, (strpos($parameter, '=')+ 1)); |
138 | if ((substr($paramValue, 0, 1) == '"') && (substr($paramValue, -1) == '"')) { |
139 | $paramValue = substr($paramValue, 1); |
140 | $paramValue = substr($paramValue, 0, -1); |
141 | } |
142 | return [$paramName, $paramValue]; |
143 | } |
144 | |
145 | /** |
146 | * Set the header name |
147 | * |
148 | * @param string $name |
149 | * @return Header |
150 | */ |
151 | public function setName($name) |
152 | { |
153 | $this->name = $name; |
154 | return $this; |
155 | } |
156 | |
157 | /** |
158 | * Get the header name |
159 | * |
160 | * @return string |
161 | */ |
162 | public function getName() |
163 | { |
164 | return $this->name; |
165 | } |
166 | |
167 | /** |
168 | * Set the header value |
169 | * |
170 | * @param string $value |
171 | * @return Header |
172 | */ |
173 | public function setValue($value) |
174 | { |
175 | $this->value = $value; |
176 | return $this; |
177 | } |
178 | |
179 | /** |
180 | * Get the header value |
181 | * |
182 | * @return string|array |
183 | */ |
184 | public function getValue() |
185 | { |
186 | return $this->value; |
187 | } |
188 | |
189 | /** |
190 | * Add the header parameters |
191 | * |
192 | * @param array $parameters |
193 | * @return Header |
194 | */ |
195 | public function addParameters(array $parameters) |
196 | { |
197 | $this->parameters = $parameters; |
198 | return $this; |
199 | } |
200 | |
201 | /** |
202 | * Set a header parameter |
203 | * |
204 | * @param string $name |
205 | * @param string $value |
206 | * @return Header |
207 | */ |
208 | public function addParameter($name, $value) |
209 | { |
210 | $this->parameters[$name] = $value; |
211 | return $this; |
212 | } |
213 | |
214 | /** |
215 | * Get the header parameters |
216 | * |
217 | * @return array |
218 | */ |
219 | public function getParameters() |
220 | { |
221 | return $this->parameters; |
222 | } |
223 | |
224 | /** |
225 | * Get the header parameters as string |
226 | * |
227 | * @return string |
228 | */ |
229 | public function getParametersAsString() |
230 | { |
231 | $parameters = []; |
232 | |
233 | foreach ($this->parameters as $name => $value) { |
234 | if (strpos($value, ' ') !== false) { |
235 | $value = '"' . $value . '"'; |
236 | } |
237 | $parameters[] = $name . '=' . $value; |
238 | } |
239 | |
240 | return implode('; ', $parameters); |
241 | } |
242 | |
243 | /** |
244 | * Get a header parameter |
245 | * |
246 | * @param string $name |
247 | * @return string |
248 | */ |
249 | public function getParameter($name) |
250 | { |
251 | return (isset($this->parameters[$name])) ? $this->parameters[$name] : null; |
252 | } |
253 | |
254 | /** |
255 | * Has header parameters |
256 | * |
257 | * @return boolean |
258 | */ |
259 | public function hasParameters() |
260 | { |
261 | return (count($this->parameters) > 0); |
262 | } |
263 | |
264 | /** |
265 | * Has a header parameter |
266 | * |
267 | * @param string $name |
268 | * @return boolean |
269 | */ |
270 | public function hasParameter($name) |
271 | { |
272 | return (isset($this->parameters[$name])); |
273 | } |
274 | |
275 | /** |
276 | * Set the header wrap |
277 | * |
278 | * @param int $wrap |
279 | * @return Header |
280 | */ |
281 | public function setWrap($wrap) |
282 | { |
283 | $this->wrap = (int)$wrap; |
284 | return $this; |
285 | } |
286 | |
287 | /** |
288 | * Get the header wrap |
289 | * |
290 | * @return int |
291 | */ |
292 | public function getWrap() |
293 | { |
294 | return $this->wrap; |
295 | } |
296 | |
297 | /** |
298 | * Has header wrap |
299 | * |
300 | * @return boolean |
301 | */ |
302 | public function hasWrap() |
303 | { |
304 | return (null !== $this->wrap); |
305 | } |
306 | |
307 | /** |
308 | * Set the header wrap indent |
309 | * |
310 | * @param string $indent |
311 | * @return Header |
312 | */ |
313 | public function setIndent($indent) |
314 | { |
315 | $this->indent = $indent; |
316 | return $this; |
317 | } |
318 | |
319 | /** |
320 | * Get the header wrap indent |
321 | * |
322 | * @return string |
323 | */ |
324 | public function getIndent() |
325 | { |
326 | return $this->indent; |
327 | } |
328 | |
329 | /** |
330 | * Has header wrap indent |
331 | * |
332 | * @return boolean |
333 | */ |
334 | public function hasIndent() |
335 | { |
336 | return (null !== $this->indent); |
337 | } |
338 | |
339 | /** |
340 | * Is the header for an attachment |
341 | * |
342 | * @return boolean |
343 | */ |
344 | public function isAttachment() |
345 | { |
346 | return (($this->name == 'Content-Disposition') && |
347 | (($this->value == 'attachment') || ($this->value == 'inline'))); |
348 | } |
349 | |
350 | /** |
351 | * Render the header string |
352 | * |
353 | * @return string |
354 | */ |
355 | public function render() |
356 | { |
357 | $parameters = []; |
358 | |
359 | if (count($this->parameters) > 0) { |
360 | $parameters = []; |
361 | foreach ($this->parameters as $name => $value) { |
362 | if (strpos($value, ' ') !== false) { |
363 | $value = '"' . $value . '"'; |
364 | } |
365 | $parameters[] = $name . '=' . $value; |
366 | } |
367 | } |
368 | |
369 | if (is_array($this->value)) { |
370 | $headers = []; |
371 | foreach ($this->value as $value) { |
372 | $hdr = $this->name . ': ' . $value; |
373 | |
374 | if (count($parameters) > 0) { |
375 | $hdr .= '; ' . implode('; ', $parameters); |
376 | } |
377 | |
378 | if ((int)$this->wrap !== 0) { |
379 | $hdr = wordwrap($hdr, $this->wrap, "\r\n" . $this->indent); |
380 | } |
381 | $headers[] = $hdr; |
382 | } |
383 | $header = implode("\r\n", $headers); |
384 | } else { |
385 | $header = $this->name . ': ' . $this->value; |
386 | |
387 | if (count($parameters) > 0) { |
388 | $header .= '; ' . implode('; ', $parameters); |
389 | } |
390 | |
391 | if ((int)$this->wrap !== 0) { |
392 | $header = wordwrap($header, $this->wrap, "\r\n" . $this->indent); |
393 | } |
394 | } |
395 | |
396 | return $header; |
397 | } |
398 | |
399 | /** |
400 | * Render the header string |
401 | * |
402 | * @return string |
403 | */ |
404 | public function __toString() |
405 | { |
406 | return $this->render(); |
407 | } |
408 | |
409 | } |