1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace miBadger\Http; |
11
|
|
|
|
12
|
|
|
use Psr\Http\Message\MessageInterface; |
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The message class. |
17
|
|
|
* |
18
|
|
|
* @see http://www.php-fig.org/psr/psr-7/ |
19
|
|
|
* @since 1.1.0 |
20
|
|
|
*/ |
21
|
|
|
class Message implements MessageInterface |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_VERSION = '1.1'; |
24
|
|
|
const VERSION_DELIMITER = 'HTTP/'; |
25
|
|
|
const HEADER_DELIMITER = ': '; |
26
|
|
|
const HEADER_VALUE_DELIMITER = ','; |
27
|
|
|
|
28
|
|
|
/** @var string The version. */ |
29
|
|
|
private $version; |
30
|
|
|
|
31
|
|
|
/** @var array The header names. */ |
32
|
|
|
private $headerNames; |
33
|
|
|
|
34
|
|
|
/** @var array The headers. */ |
35
|
|
|
private $headers; |
36
|
|
|
|
37
|
|
|
/** @var StreamInterface The body. */ |
38
|
|
|
private $body; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Construct a Message object with the given version, headers & body. |
42
|
|
|
* |
43
|
|
|
* @param string $version = self::DEFAULT_VERSION |
44
|
|
|
* @param array $headers = [] |
45
|
|
|
* @param StreamInterface|null $body = null |
46
|
|
|
*/ |
47
|
46 |
|
public function __construct($version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
48
|
|
|
{ |
49
|
46 |
|
if ($body === null) { |
50
|
9 |
|
$body = new Stream(fopen('php://temp', 'r+')); |
51
|
|
|
} |
52
|
|
|
|
53
|
46 |
|
$this->setProtocolVersion($version); |
54
|
46 |
|
$this->setHeaders($headers); |
55
|
46 |
|
$this->setBody($body); |
56
|
46 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
3 |
|
public function getProtocolVersion() |
62
|
|
|
{ |
63
|
3 |
|
return $this->version; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the protocol version. |
68
|
|
|
* |
69
|
|
|
* @param string $version |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
46 |
|
private function setProtocolVersion($version) |
73
|
|
|
{ |
74
|
46 |
|
$this->version = $version; |
75
|
|
|
|
76
|
46 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function withProtocolVersion($version) |
83
|
|
|
{ |
84
|
1 |
|
$result = clone $this; |
85
|
|
|
|
86
|
1 |
|
return $result->setProtocolVersion($version); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
2 |
|
public function getHeaders() |
93
|
|
|
{ |
94
|
2 |
|
return $this->headers; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the headers. |
99
|
|
|
* |
100
|
|
|
* @param array $headers |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
46 |
|
private function setHeaders(array $headers) |
104
|
|
|
{ |
105
|
46 |
|
$this->headerNames = []; |
106
|
46 |
|
$this->headers = []; |
107
|
|
|
|
108
|
46 |
|
foreach ($headers as $name => $value) { |
109
|
14 |
|
$this->setHeader($name, $value); |
110
|
|
|
} |
111
|
|
|
|
112
|
46 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
15 |
|
public function hasHeader($name) |
119
|
|
|
{ |
120
|
15 |
|
return array_key_exists(strtolower($name), $this->headerNames); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
14 |
|
public function getHeader($name) |
127
|
|
|
{ |
128
|
14 |
|
if (!$this->hasHeader($name)) { |
129
|
3 |
|
return []; |
130
|
|
|
} |
131
|
|
|
|
132
|
12 |
|
return $this->headers[$this->headerNames[strtolower($name)]]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
1 |
|
public function getHeaderLine($name) |
139
|
|
|
{ |
140
|
1 |
|
if (!$this->hasHeader($name)) { |
141
|
1 |
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return implode(',', $this->getHeader($name)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the header. |
149
|
|
|
* |
150
|
|
|
* @param string $name |
151
|
|
|
* @param string|string[] $value |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
21 |
|
protected function setHeader($name, $value) |
155
|
|
|
{ |
156
|
21 |
|
if (!is_array($value)) { |
157
|
7 |
|
$value = [$value]; |
158
|
|
|
} |
159
|
|
|
|
160
|
21 |
|
$this->headerNames[strtolower($name)] = $name; |
161
|
21 |
|
array_merge($this->headers[$name] = $value); |
162
|
|
|
|
163
|
21 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
8 |
|
public function withHeader($name, $value) |
170
|
|
|
{ |
171
|
8 |
|
$result = clone $this; |
172
|
|
|
|
173
|
8 |
|
return $result->setHeader($name, $value); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add the header. |
178
|
|
|
* |
179
|
|
|
* @param string $name |
180
|
|
|
* @param string|string[] $value |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
3 |
|
private function addHeader($name, $value) |
184
|
|
|
{ |
185
|
3 |
|
if (!$this->hasHeader($name)) { |
186
|
1 |
|
return $this->setHeader($name, $value); |
187
|
|
|
} |
188
|
|
|
|
189
|
2 |
|
if (!is_array($value)) { |
190
|
1 |
|
$value = [$value]; |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
foreach ($value as $element) { |
194
|
2 |
|
$this->headers[$this->headerNames[strtolower($name)]][] = $element; |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
3 |
|
public function withAddedHeader($name, $value) |
204
|
|
|
{ |
205
|
3 |
|
$result = clone $this; |
206
|
|
|
|
207
|
3 |
|
return $result->addHeader($name, $value); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Remove the header. |
212
|
|
|
* |
213
|
|
|
* @param string $name |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
1 |
|
private function removeHeader($name) |
217
|
|
|
{ |
218
|
1 |
|
if ($this->hasHeader($name)) { |
219
|
1 |
|
$normalized = strtolower($name); |
220
|
|
|
|
221
|
1 |
|
unset($this->headers[$this->headerNames[$normalized]], $this->headerNames[$normalized]); |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
1 |
|
public function withoutHeader($name) |
231
|
|
|
{ |
232
|
1 |
|
$result = clone $this; |
233
|
|
|
|
234
|
1 |
|
return $result->removeHeader($name); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
5 |
|
public function getBody() |
241
|
|
|
{ |
242
|
5 |
|
return $this->body; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Sets the body. |
247
|
|
|
* |
248
|
|
|
* @param StreamInterface $body |
249
|
|
|
* @return $this |
250
|
|
|
*/ |
251
|
46 |
|
private function setBody(StreamInterface $body) |
252
|
|
|
{ |
253
|
46 |
|
$this->body = $body; |
254
|
|
|
|
255
|
46 |
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
3 |
|
public function withBody(StreamInterface $body) |
262
|
|
|
{ |
263
|
3 |
|
$result = clone $this; |
264
|
|
|
|
265
|
3 |
|
return $result->setBody($body); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|