Completed
Push — master ( b7e494...990da0 )
by Sebastian
02:09
created

HeaderCollection::__construct()   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
     * @param Header[] $headers
13
     */
14
    public function __construct(array $headers)
15
    {
16
        $this->headers = $headers;
17
    }
18
19
    /**
20
     * @return HeaderCollection
21
     */
22
    public static function fromSuperGlobals(): HeaderCollection
23
    {
24
        $headers = [];
25
26
        foreach ($_SERVER as $name => $value) {
27
            if (strpos($name, 'HTTP_') === false) {
28
                continue;
29
            }
30
            $headers[] = new Header($name, $value);
31
        }
32
33
        $collection = new self($headers);
34
        return $collection;
35
    }
36
    
37
    /**
38
     * @param string $name
39
     * @return bool
40
     */
41
    public function has(string $name)
42
    {
43
        return array_key_exists($name, $this->headers);
44
    }
45
46
    /**
47
     * @param string $name
48
     * @return Header
49
     * @throws HeaderException
50
     */
51
    public function get(string $name)
52
    {
53
        if (!$this->has($name)) {
54
            throw new HeaderException(sprintf('Header %s not found', $name));
55
        }
56
        return $this->headers[$name];
57
    }
58
}
59