1
|
|
|
<?php |
2
|
|
|
namespace Fyuze\Http\Message; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Psr\Http\Message\MessageInterface; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
|
8
|
|
|
class Message implements MessageInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $protocol; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $headers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var StreamInterface |
25
|
|
|
*/ |
26
|
|
|
protected $stream; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
* |
31
|
|
|
* @return string HTTP protocol version. |
32
|
|
|
*/ |
33
|
1 |
|
public function getProtocolVersion() |
34
|
|
|
{ |
35
|
1 |
|
return $this->protocol; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* |
41
|
|
|
* @param string $version HTTP protocol version |
42
|
|
|
* @return self |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
3 |
|
public function withProtocolVersion($version) |
46
|
|
|
{ |
47
|
3 |
|
if (in_array($version, ['1.0', '1.1', '2.0']) === false) { |
48
|
1 |
|
throw new InvalidArgumentException('You may only use a valid http protocol version, %d provided', $version); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
return $this->_clone('protocol', $version); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @return array Returns an associative array of the message's headers. Each |
58
|
|
|
* key MUST be a header name, and each value MUST be an array of strings |
59
|
|
|
* for that header. |
60
|
|
|
*/ |
61
|
11 |
|
public function getHeaders() |
62
|
|
|
{ |
63
|
11 |
|
$headers = []; |
64
|
|
|
|
65
|
11 |
|
foreach($this->headers as $name => $values) { |
66
|
10 |
|
$headers[strtolower($name)] = $values; |
67
|
|
|
} |
68
|
|
|
|
69
|
11 |
|
return $headers; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @param string $name Case-insensitive header field name. |
76
|
|
|
* @return bool Returns true if any header names match the given header |
77
|
|
|
* name using a case-insensitive string comparison. Returns false if |
78
|
|
|
* no matching header name is found in the message. |
79
|
|
|
*/ |
80
|
11 |
|
public function hasHeader($name) |
81
|
|
|
{ |
82
|
11 |
|
return array_key_exists( |
83
|
11 |
|
strtolower($name), |
84
|
11 |
|
$this->getHeaders() |
85
|
11 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* |
91
|
|
|
* @param string $name Case-insensitive header field name. |
92
|
|
|
* @return string[] An array of string values as provided for the given |
93
|
|
|
* header. If the header does not appear in the message, this method MUST |
94
|
|
|
* return an empty array. |
95
|
|
|
*/ |
96
|
10 |
|
public function getHeader($name) |
97
|
|
|
{ |
98
|
10 |
|
$name = strtolower($name); |
99
|
|
|
|
100
|
10 |
|
return $this->hasHeader($name) ? $this->headers[$name] : []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
* |
106
|
|
|
* @param string $name Case-insensitive header field name. |
107
|
|
|
* @return string A string of values as provided for the given header |
108
|
|
|
* concatenated together using a comma. If the header does not appear in |
109
|
|
|
* the message, this method MUST return an empty string. |
110
|
|
|
*/ |
111
|
2 |
|
public function getHeaderLine($name) |
112
|
|
|
{ |
113
|
2 |
|
return implode(', ', $this->getHeader($name)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
* |
119
|
|
|
* @param string $name Case-insensitive header field name. |
120
|
|
|
* @param string|string[] $value Header value(s). |
121
|
|
|
* @return self |
122
|
|
|
* @throws InvalidArgumentException for invalid header names or values. |
123
|
|
|
*/ |
124
|
8 |
|
public function withHeader($name, $value) |
125
|
|
|
{ |
126
|
8 |
|
if (array_key_exists($name, $this->headers) && in_array($value, $this->headers[$name])) { |
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
$instance = clone $this; |
131
|
8 |
|
$instance->headers[$name] = array_filter((array)$value); |
132
|
8 |
|
return $instance; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
* |
138
|
|
|
* @param string $name Case-insensitive header field name to add. |
139
|
|
|
* @param string|string[] $value Header value(s). |
140
|
|
|
* @return self |
141
|
|
|
* @throws InvalidArgumentException for invalid header names or values. |
142
|
|
|
*/ |
143
|
1 |
|
public function withAddedHeader($name, $value) |
144
|
|
|
{ |
145
|
1 |
|
$instance = clone $this; |
146
|
1 |
|
$instance->headers[$name][] = $value; |
147
|
1 |
|
return $instance; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
* |
153
|
|
|
* @param string $name Case-insensitive header field name to remove. |
154
|
|
|
* @return self |
155
|
|
|
*/ |
156
|
1 |
|
public function withoutHeader($name) |
157
|
|
|
{ |
158
|
1 |
|
$request = new static($this); |
|
|
|
|
159
|
1 |
|
unset($request->headers[$name]); |
160
|
1 |
|
return $request; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
* |
166
|
|
|
* @return StreamInterface Returns the body as a stream. |
167
|
|
|
*/ |
168
|
9 |
|
public function getBody() |
169
|
|
|
{ |
170
|
9 |
|
return $this->stream; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
* |
176
|
|
|
* @param StreamInterface $body Body. |
177
|
|
|
* @return self |
178
|
|
|
* @throws InvalidArgumentException When the body is not valid. |
179
|
|
|
*/ |
180
|
14 |
|
public function withBody(StreamInterface $body) |
181
|
|
|
{ |
182
|
14 |
|
return $this->_clone('stream', $body); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $key |
187
|
|
|
* @param $value |
188
|
|
|
* @return static |
189
|
|
|
*/ |
190
|
43 |
|
protected function _clone($key, $value) |
191
|
|
|
{ |
192
|
43 |
|
if ($this->$key === $value) { |
193
|
1 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
43 |
|
$instance = clone $this; |
197
|
43 |
|
$instance->$key = $value; |
198
|
43 |
|
return $instance; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|