Passed
Push — master ( 616f10...3db985 )
by Alexandre
02:45
created

Headers::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) 2018 by Alexandre Le Borgne <[email protected]>.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace PhpGuard\Curl\Collection;
21
22
class Headers implements \ArrayAccess
23
{
24
    const CONTENT_TYPE_TEXT_PLAIN = 'text/plain';
25
    const CONTENT_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
26
    const CONTENT_TYPE_FORM_URL_ENCODED = 'application/x-www-form-urlencoded';
27
    const CONTENT_TYPE_FORM_JSON = 'application/json';
28
29
    const CONTENT_TYPE_PATTERN_JSON = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
30
31
    const CONTENT_TYPE = 'Content-Type';
32
33
    /**
34
     * @var array
35
     */
36
    private $headers;
37
38 13
    public function __construct(array $headers = [])
39
    {
40 13
        $this->headers = self::normaliseHeaders($headers);
41 13
    }
42
43
    /**
44
     * Whether a offset exists.
45
     *
46
     * @see https://php.net/manual/en/arrayaccess.offsetexists.php
47
     *
48
     * @param mixed $offset <p>
49
     *                      An offset to check for.
50
     *                      </p>
51
     *
52
     * @return bool true on success or false on failure.
53
     *              </p>
54
     *              <p>
55
     *              The return value will be casted to boolean if non-boolean was returned
56
     *
57
     * @since 5.0.0
58
     */
59 6
    public function offsetExists($offset)
60
    {
61 6
        return isset($this->headers[self::normalizeHeaderKey($offset)]);
62
    }
63
64
    /**
65
     * Offset to retrieve.
66
     *
67
     * @see https://php.net/manual/en/arrayaccess.offsetget.php
68
     *
69
     * @param mixed $offset <p>
70
     *                      The offset to retrieve.
71
     *                      </p>
72
     *
73
     * @return mixed can return all value types
74
     *
75
     * @since 5.0.0
76
     */
77 13
    public function offsetGet($offset)
78
    {
79 13
        return $this->headers[self::normalizeHeaderKey($offset)] ?? null;
80
    }
81
82
    /**
83
     * Offset to set.
84
     *
85
     * @see https://php.net/manual/en/arrayaccess.offsetset.php
86
     *
87
     * @param mixed $offset <p>
88
     *                      The offset to assign the value to.
89
     *                      </p>
90
     * @param mixed $value  <p>
91
     *                      The value to set.
92
     *                      </p>
93
     *
94
     * @since 5.0.0
95
     */
96 9
    public function offsetSet($offset, $value)
97
    {
98 9
        $this->headers[self::normalizeHeaderKey($offset)] = $value;
99 9
    }
100
101
    /**
102
     * Offset to unset.
103
     *
104
     * @see https://php.net/manual/en/arrayaccess.offsetunset.php
105
     *
106
     * @param mixed $offset <p>
107
     *                      The offset to unset.
108
     *                      </p>
109
     *
110
     * @since 5.0.0
111
     */
112
    public function offsetUnset($offset)
113
    {
114
        unset($this->headers[self::normalizeHeaderKey($offset)]);
115
    }
116
117 13
    public function all()
118
    {
119 13
        return $this->headers;
120
    }
121
122 13
    public function replace(array $headers)
123
    {
124 13
        return new self(array_replace($this->headers, self::normaliseHeaders($headers)));
125
    }
126
127 13
    public function toHttp(): array
128
    {
129
        return array_map(function ($k, $v) {
130 11
            return $k.':'.$v;
131 13
        }, array_keys($this->headers), $this->headers);
132
    }
133
134 13
    public static function normalizeHeaderKey(string $key)
135
    {
136 13
        return ucwords($key, '-');
137
    }
138
139 13
    public static function normaliseHeaders(array $headers)
140
    {
141
        return self::array_map_assoc(function ($k, $v) {
142 13
            return [self::normalizeHeaderKey($k) => $v];
143 13
        }, $headers);
144
    }
145
146 13
    public static function array_map_assoc(callable $f, array $a)
147
    {
148
        return array_reduce(array_map($f, array_keys($a), $a), function (array $acc, array $a) {
149 13
            return $acc + $a;
150 13
        }, []);
151
    }
152
}
153