HeaderSet::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Resume;
4
5
final class HeaderSet implements \IteratorAggregate
6
{
7
    private $keyMap;
8
    private $values;
9
10
    public function __construct(array $headers = [])
11
    {
12
        foreach ($headers as $name => $value) {
13
            $this->setHeader($name, $value);
14
        }
15
    }
16
17
    public function getIterator(): \Iterator
18
    {
19
        return new \ArrayIterator($this->values);
20
    }
21
22
    public function setHeader(string $name, string $value)
23
    {
24
        $key = \strtolower($name);
25
        $name = $this->keyMap[$key] ?? $name;
26
27
        $this->values[$name] = $value;
28
        $this->keyMap[$key] = $name;
29
    }
30
31
    public function getHeader(string $name)
32
    {
33
        return $this->values[$this->keyMap[\strtolower($name)]] ?? null;
34
    }
35
36
    public function containsHeader(string $name): bool
37
    {
38
        return isset($this->values[$this->keyMap[\strtolower($name)]]);
39
    }
40
41
    public function removeHeader(string $name)
42
    {
43
        $key = \strtolower($name);
44
        $name = $this->keyMap[$key] ?? $name;
45
46
        unset($this->values[$name], $this->keyMap[$key]);
47
    }
48
}
49