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