|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Provides access to headers property of server request, will normalize every requested name for |
|
16
|
|
|
* use convenience. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class HeadersBag extends InputBag |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function has(string $name): bool |
|
24
|
|
|
{ |
|
25
|
|
|
return parent::has($this->normalize($name)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
* |
|
31
|
|
|
* |
|
32
|
|
|
* @param bool|string $implode Implode header lines, false to return header as array. |
|
33
|
|
|
|
|
34
|
|
|
* @return string|array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get(string $name, $default = null, $implode = ',') |
|
37
|
|
|
{ |
|
38
|
|
|
$value = parent::get($this->normalize($name), $default); |
|
39
|
|
|
|
|
40
|
|
|
if (!empty($implode) && is_array($value)) { |
|
41
|
|
|
return implode($implode, $value); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $value; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
* |
|
50
|
|
|
* @param null|string $implode Implode header lines, null to return header as array. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function fetch(array $keys, bool $fill = false, $filler = null, ?string $implode = ',') |
|
53
|
|
|
{ |
|
54
|
|
|
$keys = array_map([$this, 'normalize'], $keys); |
|
55
|
|
|
|
|
56
|
|
|
$values = parent::fetch($keys, $fill, $filler); |
|
57
|
|
|
|
|
58
|
|
|
if (!empty($implode)) { |
|
59
|
|
|
foreach ($values as &$value) { |
|
60
|
|
|
$value = implode($implode, $value); |
|
61
|
|
|
unset($value); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $values; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Normalize header name. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $header |
|
72
|
|
|
|
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function normalize(string $header): string |
|
76
|
|
|
{ |
|
77
|
|
|
return str_replace( |
|
78
|
|
|
' ', |
|
79
|
|
|
'-', |
|
80
|
|
|
ucwords(str_replace('-', ' ', $header)) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|