Completed
Push — master ( fc2b8b...a482e0 )
by Sebastian
03:04
created

HeaderCollection::addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Kartenmacherei\RestFramework\Request\Header;
3
4
class HeaderCollection
5
{
6
    /**
7
     * @var array
8
     */
9
    private $headers = [];
10
11
    /**
12
     * @return HeaderCollection
13
     */
14
    public static function fromSuperGlobals(): HeaderCollection
15
    {
16
        $collection = new self();
17
        foreach ($_SERVER as $name => $value) {
18
            if (strpos($name, 'HTTP_') === false) {
19
                continue;
20
            }
21
            $collection->addHeader(new Header($name, $value));
22
        }
23
        return $collection;
24
    }
25
26
    /**
27
     * @param Header $header
28
     */
29
    private function addHeader(Header $header)
30
    {
31
        $this->headers[$header->getName()] = $header;
32
    }
33
34
    /**
35
     * @param string $name
36
     * @return bool
37
     */
38
    public function has(string $name)
39
    {
40
        return array_key_exists($name, $this->headers);
41
    }
42
43
    /**
44
     * @param string $name
45
     * @return Header
46
     * @throws HeaderException
47
     */
48
    public function get(string $name)
49
    {
50
        if (!$this->has($name)) {
51
            throw new HeaderException(sprintf('Header %s not found', $name));
52
        }
53
        return $this->headers[$name];
54
    }
55
}
56