HttpHeadersBag::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * This file is a part of Woketo package.
4
 *
5
 * (c) Nekland <[email protected]>
6
 *
7
 * For the full license, take a look to the LICENSE file
8
 * on the root directory of this project
9
 */
10
11
namespace Nekland\Woketo\Http;
12
13
14
/**
15
 * Class HttpHeadersBag
16
 *
17
 * @internal
18
 */
19
class HttpHeadersBag implements \ArrayAccess, \Iterator
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private $headers;
25
26
    /**
27
     * Is used by the iterator feature to store a possible "false" value when the loop over the array is finished.
28
     *
29
     * @var mixed
30
     */
31
    private $next;
32
33 26
    public function __construct(array $headers = null)
34
    {
35 26
        $this->headers = $headers ?: [];
36 26
    }
37
38
    /**
39
     * @param string $name
40
     * @param mixed  $value
41
     * @return self
42
     */
43 3
    public function set(string $name, $value)
44
    {
45 3
        $this->headers[$name] = $value;
46
47 3
        return $this;
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param mixed  $default
53
     * @return mixed
54
     */
55 27
    public function get(string $name, $default = null)
56
    {
57 27
        $headersLower = \array_change_key_case($this->headers);
58 27
        $name = \strtolower($name);
59
60 27
        return isset($headersLower[$name]) ? $headersLower[$name] : $default;
61
    }
62
63
    /**
64
     * @param string $name
65
     * @param mixed  $value
66
     * @return self
67
     */
68 22
    public function add(string $name, $value)
69
    {
70 22
        if (!empty($this->headers[$name])) {
71 2
            if (!\is_array($this->headers[$name])){
72 2
                $this->headers[$name] = [$this->headers[$name]];
73
            }
74 2
            $this->headers[$name][] = $value;
75
        } else {
76 22
            $this->headers[$name] = $value;
77
        }
78
        
79 22
        return $this;
80
    }
81
82
    /**
83
     * @param string $name
84
     */
85
    public function remove(string $name)
86
    {
87
        unset($this->headers[$name]);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 10
    public function offsetExists($offset)
94
    {
95 10
        return isset($this->headers[$offset]);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 17
    public function offsetGet($offset)
102
    {
103 17
        return $this->get($offset);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function offsetSet($offset, $value)
110
    {
111
        throw new \Exception('You must use either add or set method to update the header bag.');
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function offsetUnset($offset)
118
    {
119
        $this->remove($offset);
120
    }
121
122 14
    public function current()
123
    {
124 14
        return $this->next ?: \current($this->headers);
125
    }
126
127 14
    public function next()
128
    {
129 14
        $this->next = \next($this->headers);
130 14
    }
131
132 14
    public function key()
133
    {
134 14
        return \key($this->headers);
135
    }
136
137 14
    public function valid()
138
    {
139 14
        return $this->next !== false;
140
    }
141
142 14
    public function rewind()
143
    {
144 14
        $this->current = 0;
0 ignored issues
show
Bug introduced by
The property current does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
145 14
    }
146
}
147