1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
4
|
|
|
* Copyright (C) ${YEAR} 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; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class Headers implements \ArrayAccess |
24
|
|
|
{ |
25
|
|
|
const CONTENT_TYPE_TEXT_PLAIN = 'text/plain'; |
26
|
|
|
const CONTENT_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'; |
27
|
|
|
const CONTENT_TYPE_FORM_URL_ENCODED = 'application/x-www-form-urlencoded'; |
28
|
|
|
const CONTENT_TYPE_FORM_JSON = 'application/json'; |
29
|
|
|
|
30
|
|
|
const CONTENT_TYPE_PATTERN_JSON = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; |
31
|
|
|
|
32
|
|
|
const CONTENT_TYPE = 'Content-Type'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $headers; |
38
|
|
|
|
39
|
12 |
|
public function __construct(array $headers = []) |
40
|
|
|
{ |
41
|
12 |
|
$this->headers = self::normaliseHeaders($headers); |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whether a offset exists |
46
|
|
|
* @link https://php.net/manual/en/arrayaccess.offsetexists.php |
47
|
|
|
* @param mixed $offset <p> |
48
|
|
|
* An offset to check for. |
49
|
|
|
* </p> |
50
|
|
|
* @return boolean true on success or false on failure. |
51
|
|
|
* </p> |
52
|
|
|
* <p> |
53
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
54
|
|
|
* @since 5.0.0 |
55
|
|
|
*/ |
56
|
5 |
|
public function offsetExists($offset) |
57
|
|
|
{ |
58
|
5 |
|
return isset($this->headers[self::normalizeHeaderKey($offset)]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Offset to retrieve |
63
|
|
|
* @link https://php.net/manual/en/arrayaccess.offsetget.php |
64
|
|
|
* @param mixed $offset <p> |
65
|
|
|
* The offset to retrieve. |
66
|
|
|
* </p> |
67
|
|
|
* @return mixed Can return all value types. |
68
|
|
|
* @since 5.0.0 |
69
|
|
|
*/ |
70
|
12 |
|
public function offsetGet($offset) |
71
|
|
|
{ |
72
|
12 |
|
return $this->headers[self::normalizeHeaderKey($offset)] ?? null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Offset to set |
77
|
|
|
* @link https://php.net/manual/en/arrayaccess.offsetset.php |
78
|
|
|
* @param mixed $offset <p> |
79
|
|
|
* The offset to assign the value to. |
80
|
|
|
* </p> |
81
|
|
|
* @param mixed $value <p> |
82
|
|
|
* The value to set. |
83
|
|
|
* </p> |
84
|
|
|
* @return void |
85
|
|
|
* @since 5.0.0 |
86
|
|
|
*/ |
87
|
8 |
|
public function offsetSet($offset, $value) |
88
|
|
|
{ |
89
|
8 |
|
$this->headers[self::normalizeHeaderKey($offset)] = $value; |
90
|
8 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Offset to unset |
94
|
|
|
* @link https://php.net/manual/en/arrayaccess.offsetunset.php |
95
|
|
|
* @param mixed $offset <p> |
96
|
|
|
* The offset to unset. |
97
|
|
|
* </p> |
98
|
|
|
* @return void |
99
|
|
|
* @since 5.0.0 |
100
|
|
|
*/ |
101
|
|
|
public function offsetUnset($offset) |
102
|
|
|
{ |
103
|
|
|
unset($this->headers[self::normalizeHeaderKey($offset)]); |
104
|
|
|
} |
105
|
|
|
|
106
|
12 |
|
public function all() |
107
|
|
|
{ |
108
|
12 |
|
return $this->headers; |
109
|
|
|
} |
110
|
|
|
|
111
|
12 |
|
public function replace(array $headers) |
112
|
|
|
{ |
113
|
12 |
|
return new self(array_replace($this->headers, self::normaliseHeaders($headers))); |
114
|
|
|
} |
115
|
|
|
|
116
|
12 |
|
public function toHttp(): array |
117
|
|
|
{ |
118
|
|
|
return array_map(function ($k, $v) { |
119
|
10 |
|
return $k . ':' . $v; |
120
|
12 |
|
}, array_keys($this->headers), $this->headers); |
121
|
|
|
} |
122
|
|
|
|
123
|
12 |
|
public static function normalizeHeaderKey(string $key) { |
124
|
12 |
|
return ucwords($key, '-'); |
125
|
|
|
} |
126
|
|
|
|
127
|
12 |
|
public static function normaliseHeaders(array $headers) { |
128
|
|
|
return self::array_map_assoc(function ($k, $v) { |
129
|
12 |
|
return [self::normalizeHeaderKey($k) => $v]; |
130
|
12 |
|
}, $headers); |
131
|
|
|
} |
132
|
|
|
|
133
|
12 |
|
public static function array_map_assoc(callable $f, array $a) |
134
|
|
|
{ |
135
|
|
|
return array_reduce(array_map($f, array_keys($a), $a), function (array $acc, array $a) { |
136
|
12 |
|
return $acc + $a; |
137
|
12 |
|
}, []); |
138
|
|
|
} |
139
|
|
|
} |