Completed
Branch master (b52e58)
by Pierre
03:02 queued 37s
created

Headers::getRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace App\Http;
4
5
use \App\Http\Interfaces\IHeaders;
6
7
class Headers implements IHeaders
8
{
9
10
    /**
11
     * headers list
12
     *
13
     * @var array
14
     */
15
    protected $headers;
16
17
    /**
18
     * instanciate
19
     */
20
    public function __construct()
21
    {
22
        $this->headers = [];
23
    }
24
25
    /**
26
     * add one header to header list and returns Headers instance
27
     *
28
     * @param string $key
29
     * @return Headers
30
     */
31
    public function add(string $key, string $content): Headers
32
    {
33
        $this->headers[$key] = $content;
34
        return $this;
35
    }
36
37
    /**
38
     * add headers from a header list as key value
39
     * and returns Headers instance
40
     *
41
     * @param string $key
42
     * @return Headers
43
     */
44
    public function addMany(array $headers): Headers
45
    {
46
        foreach ($headers as $k => $v) {
47
            $this->add($k, $v);
48
        }
49
        return $this;
50
    }
51
52
    /**
53
     * remove key header from header list
54
     * if header list key exists
55
     * and returns Headers instance
56
     *
57
     * @param string $key
58
     * @return Headers
59
     */
60
    public function remove(string $key): Headers
61
    {
62
        if (isset($this->headers[$key])) {
63
            unset($this->headers[$key]);
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * returns headers map as assoc array
70
     *
71
     * @return array
72
     */
73
    public function get(): array
74
    {
75
        return $this->headers;
76
    }
77
78
    /**
79
     * returns raw headers as a normal array
80
     *
81
     * @return array
82
     */
83
    public function getRaw(): array
84
    {
85
        return array_map(
86
            function ($key, $val) {
87
                return $key . ': ' . $val;
88
            },
89
            array_keys($this->headers),
90
            $this->headers
91
        );
92
    }
93
94
    /**
95
     * send headers and returns Headers instance
96
     *
97
     * @return Headers
98
     */
99
    public function send(): Headers
100
    {
101
        $headers = $this->getRaw();
102
        $headersCount = count($headers);
103
        for ($c = 0; $c < $headersCount; $c++) {
104
            header($headers[$c]);
105
        }
106
        unset($headersCount);
107
        unset($headers);
108
        return $this;
109
    }
110
}
111