1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Http\Abstracts; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Kernel\Http\Message\Stream; |
19
|
|
|
use O2System\Psr\Http\Message\MessageInterface; |
20
|
|
|
use O2System\Psr\Http\Message\StreamInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Message |
24
|
|
|
* |
25
|
|
|
* @package O2System\Kernel\Http |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractMessage implements MessageInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Message Protocol |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $protocol = '1.1'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Message Headers |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $headers = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Message Body |
45
|
|
|
* |
46
|
|
|
* @var Stream |
47
|
|
|
*/ |
48
|
|
|
protected $body; |
49
|
|
|
|
50
|
|
|
// ------------------------------------------------------------------------ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Message::getProtocolVersion |
54
|
|
|
* |
55
|
|
|
* Retrieves the HTTP protocol version as a string. |
56
|
|
|
* |
57
|
|
|
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
58
|
|
|
* |
59
|
|
|
* @return string HTTP protocol version. |
60
|
|
|
*/ |
61
|
|
|
public function getProtocolVersion() |
62
|
|
|
{ |
63
|
|
|
return $this->protocol; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// ------------------------------------------------------------------------ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Message::withProtocolVersion |
70
|
|
|
* |
71
|
|
|
* Return an instance with the specified HTTP protocol version. |
72
|
|
|
* |
73
|
|
|
* The version string MUST contain only the HTTP version number (e.g., |
74
|
|
|
* "1.1", "1.0"). |
75
|
|
|
* |
76
|
|
|
* This method MUST be implemented in such a way as to retain the |
77
|
|
|
* immutability of the message, and MUST return an instance that has the |
78
|
|
|
* new protocol version. |
79
|
|
|
* |
80
|
|
|
* @param string $version HTTP protocol version |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
|
|
public function withProtocolVersion($version) |
85
|
|
|
{ |
86
|
|
|
if (in_array($version, ['1.0', '1.1', '2'])) { |
87
|
|
|
$message = clone $this; |
88
|
|
|
$message->protocol = $version; |
89
|
|
|
|
90
|
|
|
return $message; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// ------------------------------------------------------------------------ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Message::getHeaders |
100
|
|
|
* |
101
|
|
|
* Retrieves all message header values. |
102
|
|
|
* |
103
|
|
|
* The keys represent the header name as it will be sent over the wire, and |
104
|
|
|
* each value is an array of strings associated with the header. |
105
|
|
|
* |
106
|
|
|
* // Represent the headers as a string |
107
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
108
|
|
|
* echo $name . ': ' . implode(', ', $values); |
109
|
|
|
* } |
110
|
|
|
* |
111
|
|
|
* // Emit headers iteratively: |
112
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
113
|
|
|
* foreach ($values as $value) { |
114
|
|
|
* header(sprintf('%s: %s', $name, $value), false); |
115
|
|
|
* } |
116
|
|
|
* } |
117
|
|
|
* |
118
|
|
|
* While header names are not case-sensitive, getHeaders() will preserve the |
119
|
|
|
* exact case in which headers were originally specified. |
120
|
|
|
* |
121
|
|
|
* @return string[][] Returns an associative array of the message's headers. |
122
|
|
|
* Each key MUST be a header name, and each value MUST be an array of |
123
|
|
|
* strings for that header. |
124
|
|
|
*/ |
125
|
|
|
public function getHeaders() |
126
|
|
|
{ |
127
|
|
|
return $this->headers; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// ------------------------------------------------------------------------ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Message::hasHeader |
134
|
|
|
* |
135
|
|
|
* Checks if a header exists by the given case-insensitive name. |
136
|
|
|
* |
137
|
|
|
* @param string $name Case-insensitive header field name. |
138
|
|
|
* |
139
|
|
|
* @return bool Returns true if any header names match the given header |
140
|
|
|
* name using a case-insensitive string comparison. Returns false if |
141
|
|
|
* no matching header name is found in the message. |
142
|
|
|
*/ |
143
|
|
|
public function hasHeader($name) |
144
|
|
|
{ |
145
|
|
|
return (bool)isset($this->headers[ $name ]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// ------------------------------------------------------------------------ |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Message::getHeaderLine |
152
|
|
|
* |
153
|
|
|
* Retrieves a comma-separated string of the values for a single header. |
154
|
|
|
* |
155
|
|
|
* This method returns all of the header values of the given |
156
|
|
|
* case-insensitive header name as a string concatenated together using |
157
|
|
|
* a comma. |
158
|
|
|
* |
159
|
|
|
* NOTE: Not all header values may be appropriately represented using |
160
|
|
|
* comma concatenation. For such headers, use getHeader() instead |
161
|
|
|
* and supply your own delimiter when concatenating. |
162
|
|
|
* |
163
|
|
|
* If the header does not appear in the message, this method MUST return |
164
|
|
|
* an empty string. |
165
|
|
|
* |
166
|
|
|
* @param string $name Case-insensitive header field name. |
167
|
|
|
* |
168
|
|
|
* @return string A string of values as provided for the given header |
169
|
|
|
* concatenated together using a comma. If the header does not appear in |
170
|
|
|
* the message, this method MUST return an empty string. |
171
|
|
|
*/ |
172
|
|
|
public function getHeaderLine($name) |
173
|
|
|
{ |
174
|
|
|
if (isset($this->headers[ $name ])) { |
175
|
|
|
$this->headers[ $name ]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return ''; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// ------------------------------------------------------------------------ |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Message::withAddedHeader |
185
|
|
|
* |
186
|
|
|
* Return an instance with the specified header appended with the given value. |
187
|
|
|
* |
188
|
|
|
* Existing values for the specified header will be maintained. The new |
189
|
|
|
* value(s) will be appended to the existing list. If the header did not |
190
|
|
|
* exist previously, it will be added. |
191
|
|
|
* |
192
|
|
|
* This method MUST be implemented in such a way as to retain the |
193
|
|
|
* immutability of the message, and MUST return an instance that has the |
194
|
|
|
* new header and/or value. |
195
|
|
|
* |
196
|
|
|
* @param string $name Case-insensitive header field name to add. |
197
|
|
|
* @param string|string[] $value Header value(s). |
198
|
|
|
* |
199
|
|
|
* @return static |
200
|
|
|
* @throws \InvalidArgumentException for invalid header names. |
201
|
|
|
* @throws \InvalidArgumentException for invalid header values. |
202
|
|
|
*/ |
203
|
|
|
public function withAddedHeader($name, $value) |
204
|
|
|
{ |
205
|
|
|
$lines = $this->getHeader($name); |
206
|
|
|
$value = array_map('trim', explode(',', $value)); |
|
|
|
|
207
|
|
|
|
208
|
|
|
$lines = array_merge($lines, $value); |
209
|
|
|
|
210
|
|
|
return $this->withHeader($name, implode(', ', $lines)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// ------------------------------------------------------------------------ |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Message::getHeader |
217
|
|
|
* |
218
|
|
|
* Retrieves a message header value by the given case-insensitive name. |
219
|
|
|
* |
220
|
|
|
* This method returns an array of all the header values of the given |
221
|
|
|
* case-insensitive header name. |
222
|
|
|
* |
223
|
|
|
* If the header does not appear in the message, this method MUST return an |
224
|
|
|
* empty array. |
225
|
|
|
* |
226
|
|
|
* @param string $name Case-insensitive header field name. |
227
|
|
|
* |
228
|
|
|
* @return string[] An array of string values as provided for the given |
229
|
|
|
* header. If the header does not appear in the message, this method MUST |
230
|
|
|
* return an empty array. |
231
|
|
|
*/ |
232
|
|
|
public function getHeader($name) |
233
|
|
|
{ |
234
|
|
|
$lines = []; |
235
|
|
|
|
236
|
|
|
if (isset($this->headers[ $name ])) { |
237
|
|
|
$lines = array_map('trim', explode(',', $this->headers[ $name ])); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $lines; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// ------------------------------------------------------------------------ |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Message::withHeader |
247
|
|
|
* |
248
|
|
|
* Return an instance with the provided value replacing the specified header. |
249
|
|
|
* |
250
|
|
|
* While header names are case-insensitive, the casing of the header will |
251
|
|
|
* be preserved by this function, and returned from getHeaders(). |
252
|
|
|
* |
253
|
|
|
* This method MUST be implemented in such a way as to retain the |
254
|
|
|
* immutability of the message, and MUST return an instance that has the |
255
|
|
|
* new and/or updated header and value. |
256
|
|
|
* |
257
|
|
|
* @param string $name Case-insensitive header field name. |
258
|
|
|
* @param string|string[] $value Header value(s). |
259
|
|
|
* |
260
|
|
|
* @return static |
261
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
262
|
|
|
*/ |
263
|
|
|
public function withHeader($name, $value) |
264
|
|
|
{ |
265
|
|
|
$message = clone $this; |
266
|
|
|
$message->headers[ $name ] = $value; |
267
|
|
|
|
268
|
|
|
return $message; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// ------------------------------------------------------------------------ |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Message::withoutHeader |
275
|
|
|
* |
276
|
|
|
* Return an instance without the specified header. |
277
|
|
|
* |
278
|
|
|
* Header resolution MUST be done without case-sensitivity. |
279
|
|
|
* |
280
|
|
|
* This method MUST be implemented in such a way as to retain the |
281
|
|
|
* immutability of the message, and MUST return an instance that removes |
282
|
|
|
* the named header. |
283
|
|
|
* |
284
|
|
|
* @param string $name Case-insensitive header field name to remove. |
285
|
|
|
* |
286
|
|
|
* @return static |
287
|
|
|
*/ |
288
|
|
|
public function withoutHeader($name) |
289
|
|
|
{ |
290
|
|
|
$message = clone $this; |
291
|
|
|
|
292
|
|
|
if (isset($message->headers[ $name ])) { |
293
|
|
|
unset($message->headers[ $name ]); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $message; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
// ------------------------------------------------------------------------ |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Message::getBody |
303
|
|
|
* |
304
|
|
|
* Gets the body of the message. |
305
|
|
|
* |
306
|
|
|
* @return StreamInterface Returns the body as a stream. |
307
|
|
|
*/ |
308
|
|
|
public function &getBody() |
309
|
|
|
{ |
310
|
|
|
if (empty($this->body)) { |
311
|
|
|
$this->body = new Stream(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $this->body; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// ------------------------------------------------------------------------ |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Message::withBody |
321
|
|
|
* |
322
|
|
|
* Return an instance with the specified message body. |
323
|
|
|
* |
324
|
|
|
* The body MUST be a StreamInterface object. |
325
|
|
|
* |
326
|
|
|
* This method MUST be implemented in such a way as to retain the |
327
|
|
|
* immutability of the message, and MUST return a new instance that has the |
328
|
|
|
* new body stream. |
329
|
|
|
* |
330
|
|
|
* @param StreamInterface $body Body. |
331
|
|
|
* |
332
|
|
|
* @return static |
333
|
|
|
* @throws \InvalidArgumentException When the body is not valid. |
334
|
|
|
*/ |
335
|
|
|
public function withBody(StreamInterface $body) |
336
|
|
|
{ |
337
|
|
|
$message = clone $this; |
338
|
|
|
$message->body = $body; |
|
|
|
|
339
|
|
|
|
340
|
|
|
return $message; |
341
|
|
|
} |
342
|
|
|
} |