Completed
Push — master ( 4fd434...e1a522 )
by Bohuslav
03:35
created

Headers::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Kambo\Http\Message;
3
4
/**
5
 * Headers
6
 *
7
 * This class represents a collection of HTTP headers that is used in HTTP 
8
 * request and response objects.
9
 *
10
 * @package Kambo\Http\Message
11
 * @author  Bohuslav Simek <[email protected]>
12
 * @license MIT
13
 */
14
class Headers
15
{
16
    /**
17
     * List of headers name that should not be normalized.
18
     *
19
     * @var array
20
     */
21
    private $ignoreProcessing = [
22
        "user-agent" => true
23
    ];
24
25
    /**
26
     * Header data
27
     *
28
     * @var array|null
29
     */
30
    private $data;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param array|null $headers
36
     *
37
     */
38 43
    public function __construct(
39
        $headers = null
40
    ) {
41 43
        if (isset($headers)) {
42 16
            $headers = $this->normalizeHeaders($headers);
43 16
        }
44
45 43
        $this->data = $headers;
46 43
    }
47
48
    /**
49
     * Add header value
50
     * If the header already exists data are merged, method DOES NOT replace previous value.
51
     *
52
     * @param string $name  Name of the header.
53
     * @param mixed  $value Value of the header can be scalar data type or string.
54
     *
55
     * @return void
56
     */
57 2
    public function add($name, $value)
58
    {
59 2
        $name      = $this->normalizeName($name);
60 2
        $newValues = is_array($value) ? $value : [$value];
61 2
        $data      = [];
62 2
        if (isset($this->data[$name])) {
63 1
            $data = $this->data[$name];
64 1
        }
65
66 2
        $this->data[$name] = array_merge($data, $newValues);
67
68 2
    }
69
70
    /**
71
     * Set header value
72
     * If the header already exist value will be replaced.
73
     *
74
     * @param string $name  Name of the header.
75
     * @param mixed  $value Value of the header can be scalar data type or string.
76
     *
77
     * @return void
78
     */
79 12
    public function set($name, $value)
80
    {
81 12
        $name = $this->normalizeName($name);
82 12
        $newValues = is_array($value) ? $value : [$value];
83 12
        $this->data[$name] = $newValues;
84 12
    }
85
86
    /**
87
     * Get header by provided name
88
     *
89
     * @param string $name Name of the header.
90
     *   
91
     * @return mixed value of header
92
     */
93 7
    public function get($name)
94
    {
95 7
        $name = $this->normalizeName($name);
96 7
        $data = [];
97 7
        if (isset($this->data[$name])) {
98 7
            $data = $this->data[$name];
99 7
        }
100
101 7
        return $data;
102
    }
103
104
    /**
105
     * Get header values separated by comma
106
     *
107
     * @param string $name Name of the header.
108
     *   
109
     * @return string value of header separated by comma
110
     */
111 2
    public function getLine($name)
112
    {
113 2
        $line = $this->get($name);
114
115 2
        return implode(',', $line);
116
    }
117
118
    /**
119
     * Get all headers
120
     *   
121
     * @return array value of all headers
122
     */
123 8
    public function all()
124
    {
125 8
        return $this->data;
126
    }
127
128
    /**
129
     * Remove header by provided name
130
     *
131
     * @param string $name Name of the header.
132
     *   
133
     * @return self
134
     */
135 3
    public function remove($name)
136
    {
137 3
        $name = $this->normalizeName($name);
138 3
        if (isset($this->data[$name])) {
139 3
            unset($this->data[$name]);
140 3
        }
141
142 3
        return $this;
143
    }
144
145
    /**
146
     * Check if header exist
147
     *
148
     * @param string $name Name of the header.  
149
     *   
150
     * @return boolean Returns TRUE if exists or FALSE if not.
151
     */
152 12
    public function exists($name)
153
    {
154 12
        return isset($this->data[$this->normalizeName($name)]);
155
    }
156
157
    // ------------ PRIVATE METHODS
158
159
    /**
160
     * Normalize headers values
161
     *
162
     * @param array $headers Headers for normalization.
163
     *   
164
     * @return array Normalized header data. 
165
     */
166 16
    private function normalizeHeaders($headers)
167
    {
168 16
        $normalized = [];
169 16
        foreach ($headers as $name => $value) {
170 15
            $name              = $this->normalizeName($name);
171 15
            $normalized[$name] = $this->normalizeData($name, $value);
172 16
        }
173
174 16
        return $normalized;
175
    }
176
177
    /**
178
     * Normalize header value
179
     *
180
     * @param string $name Name of header - some headers should not be normalized.
181
     * @param string $data Header data for normalization.
182
     *  
183
     * @return array Normalized value of header
184
     */
185 15
    private function normalizeData($name, $data)
186
    {
187 15
        if (!isset($this->ignoreProcessing[$name])) {
188 14
            return array_map('trim', explode(',', $data));
189
        } else {
190 12
            return [$data];
191
        }
192
    }
193
194
    /**
195
     * Normalize header name
196
     *
197
     * @param string $name Name of header for normalization
198
     *  
199
     * @return string Normalized name of header
200
     */
201 25
    private function normalizeName($name)
202
    {
203 25
        $name = strtr(strtolower($name), '_', '-');
204 25
        if (strpos($name, 'http-') === 0) {
205 15
            $name = substr($name, 5);
206 15
        }
207
208 25
        return $name;
209
    }
210
}
211