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