1 | <?php |
||
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() |
||
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() |
||
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() |
||
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) |
|
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 = '') |
|
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) |
|
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) |
|
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) |
||
158 | |||
159 | /** |
||
160 | * Removes all headers. |
||
161 | */ |
||
162 | public function removeAll() |
||
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) |
||
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) |
||
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) |
||
236 | } |
||
237 |