Passed
Push — master ( 55de86...60c91e )
by Alexander
21:16
created

HeaderCollection::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.125

Importance

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