1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Object; |
12
|
|
|
use ArrayIterator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HeaderCollection is used by [[Response]] to maintain the currently registered HTTP headers. |
16
|
|
|
* |
17
|
|
|
* @property int $count The number of headers in the collection. This property is read-only. |
18
|
|
|
* @property ArrayIterator $iterator An iterator for traversing the headers in the collection. This property |
19
|
|
|
* is read-only. |
20
|
|
|
* |
21
|
|
|
* @author Qiang Xue <[email protected]> |
22
|
|
|
* @since 2.0 |
23
|
|
|
*/ |
24
|
|
|
class HeaderCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array the headers in this collection (indexed by the header names) |
28
|
|
|
*/ |
29
|
|
|
private $_headers = []; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an iterator for traversing the headers in the collection. |
34
|
|
|
* This method is required by the SPL interface [[\IteratorAggregate]]. |
35
|
|
|
* It will be implicitly called when you use `foreach` to traverse the collection. |
36
|
|
|
* @return ArrayIterator an iterator for traversing the headers in the collection. |
37
|
|
|
*/ |
38
|
|
|
public function getIterator() |
39
|
|
|
{ |
40
|
|
|
return new ArrayIterator($this->_headers); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the number of headers in the collection. |
45
|
|
|
* This method is required by the SPL `Countable` interface. |
46
|
|
|
* It will be implicitly called when you use `count($collection)`. |
47
|
|
|
* @return int the number of headers in the collection. |
48
|
|
|
*/ |
49
|
|
|
public function count() |
50
|
|
|
{ |
51
|
|
|
return $this->getCount(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the number of headers in the collection. |
56
|
|
|
* @return int the number of headers in the collection. |
57
|
|
|
*/ |
58
|
|
|
public function getCount() |
59
|
|
|
{ |
60
|
|
|
return count($this->_headers); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the named header(s). |
65
|
|
|
* @param string $name the name of the header to return |
66
|
|
|
* @param mixed $default the value to return in case the named header does not exist |
67
|
|
|
* @param bool $first whether to only return the first header of the specified name. |
68
|
|
|
* If false, all headers of the specified name will be returned. |
69
|
|
|
* @return string|array the named header(s). If `$first` is true, a string will be returned; |
70
|
|
|
* If `$first` is false, an array will be returned. |
71
|
|
|
*/ |
72
|
11 |
|
public function get($name, $default = null, $first = true) |
73
|
|
|
{ |
74
|
11 |
|
$name = strtolower($name); |
75
|
11 |
|
if (isset($this->_headers[$name])) { |
76
|
9 |
|
return $first ? reset($this->_headers[$name]) : $this->_headers[$name]; |
77
|
|
|
} else { |
78
|
2 |
|
return $default; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Adds a new header. |
84
|
|
|
* If there is already a header with the same name, it will be replaced. |
85
|
|
|
* @param string $name the name of the header |
86
|
|
|
* @param string $value the value of the header |
87
|
|
|
* @return $this the collection object itself |
88
|
|
|
*/ |
89
|
61 |
|
public function set($name, $value = '') |
90
|
|
|
{ |
91
|
61 |
|
$name = strtolower($name); |
92
|
61 |
|
$this->_headers[$name] = (array) $value; |
93
|
|
|
|
94
|
61 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Adds a new header. |
99
|
|
|
* If there is already a header with the same name, the new one will |
100
|
|
|
* be appended to it instead of replacing it. |
101
|
|
|
* @param string $name the name of the header |
102
|
|
|
* @param string $value the value of the header |
103
|
|
|
* @return $this the collection object itself |
104
|
|
|
*/ |
105
|
1 |
|
public function add($name, $value) |
106
|
|
|
{ |
107
|
1 |
|
$name = strtolower($name); |
108
|
1 |
|
$this->_headers[$name][] = $value; |
109
|
|
|
|
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sets a new header only if it does not exist yet. |
115
|
|
|
* If there is already a header with the same name, the new one will be ignored. |
116
|
|
|
* @param string $name the name of the header |
117
|
|
|
* @param string $value the value of the header |
118
|
|
|
* @return $this the collection object itself |
119
|
|
|
*/ |
120
|
4 |
|
public function setDefault($name, $value) |
121
|
|
|
{ |
122
|
4 |
|
$name = strtolower($name); |
123
|
4 |
|
if (empty($this->_headers[$name])) { |
124
|
4 |
|
$this->_headers[$name][] = $value; |
125
|
4 |
|
} |
126
|
|
|
|
127
|
4 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns a value indicating whether the named header exists. |
132
|
|
|
* @param string $name the name of the header |
133
|
|
|
* @return bool whether the named header exists |
134
|
|
|
*/ |
135
|
1 |
|
public function has($name) |
136
|
|
|
{ |
137
|
1 |
|
$name = strtolower($name); |
138
|
|
|
|
139
|
1 |
|
return isset($this->_headers[$name]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Removes a header. |
144
|
|
|
* @param string $name the name of the header to be removed. |
145
|
|
|
* @return array the value of the removed header. Null is returned if the header does not exist. |
146
|
|
|
*/ |
147
|
|
|
public function remove($name) |
148
|
|
|
{ |
149
|
|
|
$name = strtolower($name); |
150
|
|
|
if (isset($this->_headers[$name])) { |
151
|
|
|
$value = $this->_headers[$name]; |
152
|
|
|
unset($this->_headers[$name]); |
153
|
|
|
return $value; |
154
|
|
|
} else { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Removes all headers. |
161
|
|
|
*/ |
162
|
|
|
public function removeAll() |
163
|
|
|
{ |
164
|
|
|
$this->_headers = []; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns the collection as a PHP array. |
169
|
|
|
* @return array the array representation of the collection. |
170
|
|
|
* The array keys are header names, and the array values are the corresponding header values. |
171
|
|
|
*/ |
172
|
1 |
|
public function toArray() |
173
|
|
|
{ |
174
|
1 |
|
return $this->_headers; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Populates the header collection from an array. |
179
|
|
|
* @param array $array the headers to populate from |
180
|
|
|
* @since 2.0.3 |
181
|
|
|
*/ |
182
|
1 |
|
public function fromArray(array $array) |
183
|
|
|
{ |
184
|
1 |
|
$this->_headers = $array; |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns whether there is a header with the specified name. |
189
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
190
|
|
|
* It is implicitly called when you use something like `isset($collection[$name])`. |
191
|
|
|
* @param string $name the header name |
192
|
|
|
* @return bool whether the named header exists |
193
|
|
|
*/ |
194
|
1 |
|
public function offsetExists($name) |
195
|
|
|
{ |
196
|
1 |
|
return $this->has($name); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the header with the specified name. |
201
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
202
|
|
|
* It is implicitly called when you use something like `$header = $collection[$name];`. |
203
|
|
|
* This is equivalent to [[get()]]. |
204
|
|
|
* @param string $name the header name |
205
|
|
|
* @return string the header value with the specified name, null if the named header does not exist. |
206
|
|
|
*/ |
207
|
|
|
public function offsetGet($name) |
208
|
|
|
{ |
209
|
|
|
return $this->get($name); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Adds the header to the collection. |
214
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
215
|
|
|
* It is implicitly called when you use something like `$collection[$name] = $header;`. |
216
|
|
|
* This is equivalent to [[add()]]. |
217
|
|
|
* @param string $name the header name |
218
|
|
|
* @param string $value the header value to be added |
219
|
|
|
*/ |
220
|
|
|
public function offsetSet($name, $value) |
221
|
|
|
{ |
222
|
|
|
$this->set($name, $value); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Removes the named header. |
227
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
228
|
|
|
* It is implicitly called when you use something like `unset($collection[$name])`. |
229
|
|
|
* This is equivalent to [[remove()]]. |
230
|
|
|
* @param string $name the header name |
231
|
|
|
*/ |
232
|
|
|
public function offsetUnset($name) |
233
|
|
|
{ |
234
|
|
|
$this->remove($name); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|