1
|
|
|
<?php namespace Comodojo\Dispatcher\Traits; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Common trait to manage request and response headers |
5
|
|
|
* |
6
|
|
|
* @package Comodojo Dispatcher |
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
8
|
|
|
* @author Marco Castiello <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
trait HeadersTrait { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Headers array |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $headers = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get one or all header(s) |
32
|
|
|
* If $header param is not provided, this method will return all the |
33
|
|
|
* registered headers. |
34
|
|
|
* |
35
|
|
|
* Starting from (v 4.1+) this method is CASE INSENSITIVE |
36
|
|
|
* |
37
|
|
|
* @param string $header |
38
|
|
|
* @return string|array|null |
39
|
|
|
*/ |
40
|
5 |
|
public function get($header = null) { |
41
|
|
|
|
42
|
5 |
|
if (is_null($header)) { |
43
|
2 |
|
return $this->headers; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
$index = $this->getIndexCaseInsensitive($header); |
47
|
|
|
|
48
|
5 |
|
return $index === null ? null : $this->headers[$index]; |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get one or all header(s) as a string |
54
|
|
|
* If $header param is not provided, this method will return all the |
55
|
|
|
* registered headers. |
56
|
|
|
* |
57
|
|
|
* Starting from (v 4.1+) this method is CASE INSENSITIVE |
58
|
|
|
* |
59
|
|
|
* @param string $header |
60
|
|
|
* @return string|array|null |
61
|
|
|
*/ |
62
|
6 |
|
public function getAsString($header = null) { |
63
|
|
|
|
64
|
6 |
|
if (is_null($header)) { |
65
|
|
|
|
66
|
4 |
|
return array_map([$this, 'headerToString'], |
67
|
4 |
|
array_keys($this->headers), |
68
|
4 |
|
array_values($this->headers) |
69
|
4 |
|
); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$index = $this->getIndexCaseInsensitive($header); |
74
|
|
|
|
75
|
2 |
|
return $index === null ? null : self::headerToString($index, $this->headers[$index]); |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check if an header is registered |
81
|
|
|
* |
82
|
|
|
* Starting from (v 4.1+) this method is CASE INSENSITIVE |
83
|
|
|
* |
84
|
|
|
* @param string $header |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
3 |
|
public function has($header) { |
88
|
|
|
|
89
|
3 |
|
return $this->getIndexCaseInsensitive($header) === null ? false : true; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set an header |
95
|
|
|
* If $header is an inline header and value is null, this method will split |
96
|
|
|
* it automatically. |
97
|
|
|
* $value can be a string or an array. In the latter case, these values will |
98
|
|
|
* separated by a comma in the string representation of the header. |
99
|
|
|
* |
100
|
|
|
* @param string $header Header name or string representation |
101
|
|
|
* @param mixed $value Header value(s) |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
4 |
|
public function set($header, $value = null) { |
105
|
|
|
|
106
|
4 |
|
if (is_null($value)) { |
107
|
|
|
|
108
|
|
|
$header = explode(":", $header, 2); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->headers[$header[0]] = isset($header[1]) ? trim($header[1]) : ''; |
111
|
|
|
|
112
|
|
|
} else { |
113
|
|
|
|
114
|
4 |
|
$this->headers[$header] = $value; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
return $this; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Delete one or all header(s) |
124
|
|
|
* |
125
|
|
|
* If no argument is provided, all headers will be deleted. |
126
|
|
|
* |
127
|
|
|
* Starting from (v 4.1+) this method is CASE INSENSITIVE |
128
|
|
|
* |
129
|
|
|
* @param string $header |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
3 |
|
public function delete($header = null) { |
133
|
|
|
|
134
|
3 |
|
if (is_null($header)) { |
135
|
|
|
|
136
|
1 |
|
$this->headers = []; |
137
|
|
|
|
138
|
1 |
|
return true; |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
$index = $this->getIndexCaseInsensitive($header); |
143
|
|
|
|
144
|
3 |
|
if ( $index === null ) { |
145
|
1 |
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
unset($this->headers[$index]); |
149
|
2 |
|
return true; |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Merge actual headers with an array of headers |
155
|
|
|
* |
156
|
|
|
* @param string $header |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
1 |
|
public function merge($headers) { |
160
|
|
|
|
161
|
1 |
|
foreach ($headers as $key => $value) { |
162
|
|
|
$this->set($key, $value); |
163
|
1 |
|
} |
164
|
|
|
|
165
|
1 |
|
return $this; |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
5 |
|
private function getIndexCaseInsensitive($header) { |
170
|
|
|
|
171
|
5 |
|
$mapping = array_combine( |
172
|
5 |
|
array_keys(array_change_key_case($this->headers, CASE_LOWER)), |
173
|
5 |
|
array_keys($this->headers) |
174
|
5 |
|
); |
175
|
|
|
|
176
|
5 |
|
$header = strtolower($header); |
177
|
|
|
|
178
|
5 |
|
return array_key_exists($header, $mapping) ? |
179
|
5 |
|
$mapping[$header] : null; |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
private static function headerToString($header, $value) { |
184
|
|
|
|
185
|
3 |
|
return is_array($value) ? |
186
|
3 |
|
(string)("$header: ".implode(',',$value)) : |
|
|
|
|
187
|
3 |
|
(string)("$header: $value"); |
|
|
|
|
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.