Headers::normalizeName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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 69
    public function __construct(
39
        $headers = null
40
    ) {
41 69
        if (isset($headers)) {
42 69
            $headers = $this->normalizeHeaders($headers);
43 69
        }
44
45 69
        $this->data = $headers;
46 69
    }
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 2
    }
68
69
    /**
70
     * Set header value
71
     * If the header already exist value will be replaced.
72
     *
73
     * @param string $name  Name of the header.
74
     * @param mixed  $value Value of the header can be scalar data type or string.
75
     *
76
     * @return void
77
     */
78 13
    public function set($name, $value)
79
    {
80 13
        $name = $this->normalizeName($name);
81 13
        $newValues = is_array($value) ? $value : [$value];
82 13
        $this->data[$name] = $newValues;
83 13
    }
84
85
    /**
86
     * Get header by provided name
87
     *
88
     * @param string $name Name of the header.
89
     *   
90
     * @return mixed value of header
91
     */
92 9
    public function get($name)
93
    {
94 9
        $name = $this->normalizeName($name);
95 9
        $data = [];
96 9
        if (isset($this->data[$name])) {
97 9
            $data = $this->data[$name];
98 9
        }
99
100 9
        return $data;
101
    }
102
103
    /**
104
     * Get header values separated by comma
105
     *
106
     * @param string $name Name of the header.
107
     *   
108
     * @return string value of header separated by comma
109
     */
110 2
    public function getLine($name)
111
    {
112 2
        $line = $this->get($name);
113
114 2
        return implode(',', $line);
115
    }
116
117
    /**
118
     * Get all headers
119
     *   
120
     * @return array value of all headers
121
     */
122 8
    public function all()
123
    {
124 8
        return $this->data;
125
    }
126
127
    /**
128
     * Remove header by provided name
129
     *
130
     * @param string $name Name of the header.
131
     *   
132
     * @return self
133
     */
134 3
    public function remove($name)
135
    {
136 3
        $name = $this->normalizeName($name);
137 3
        if (isset($this->data[$name])) {
138 3
            unset($this->data[$name]);
139 3
        }
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * Check if header exist
146
     *
147
     * @param string $name Name of the header.  
148
     *   
149
     * @return boolean Returns TRUE if exists or FALSE if not.
150
     */
151 16
    public function exists($name)
152
    {
153 16
        return isset($this->data[$this->normalizeName($name)]);
154
    }
155
156
    // ------------ PRIVATE METHODS
157
158
    /**
159
     * Normalize headers values
160
     *
161
     * @param array $headers Headers for normalization.
162
     *   
163
     * @return array Normalized header data. 
164
     */
165 69
    private function normalizeHeaders($headers)
166
    {
167 69
        $normalized = [];
168 69
        foreach ($headers as $name => $value) {
169 17
            $name              = $this->normalizeName($name);
170 17
            $normalized[$name] = $this->normalizeData($name, $value);
171 69
        }
172
173 69
        return $normalized;
174
    }
175
176
    /**
177
     * Normalize header value
178
     *
179
     * @param string $name Name of header - some headers should not be normalized.
180
     * @param string $data Header data for normalization.
181
     *  
182
     * @return array Normalized value of header
183
     */
184 17
    private function normalizeData($name, $data)
185
    {
186 17
        if (!isset($this->ignoreProcessing[$name])) {
187 16
            return array_map('trim', explode(',', $data));
188
        } else {
189 12
            return [$data];
190
        }
191
    }
192
193
    /**
194
     * Normalize header name
195
     *
196
     * @param string $name Name of header for normalization
197
     *  
198
     * @return string Normalized name of header
199
     */
200 30
    private function normalizeName($name)
201
    {
202 30
        $name = strtr(strtolower($name), '_', '-');
203 30
        if (strpos($name, 'http-') === 0) {
204 15
            $name = substr($name, 5);
205 15
        }
206
207 30
        return $name;
208
    }
209
}
210