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
|
9 |
|
public function getHeaders() |
62
|
|
|
{ |
63
|
9 |
|
return $this->headers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
* |
69
|
|
|
* @param string $name Case-insensitive header field name. |
70
|
|
|
* @return bool Returns true if any header names match the given header |
71
|
|
|
* name using a case-insensitive string comparison. Returns false if |
72
|
|
|
* no matching header name is found in the message. |
73
|
|
|
*/ |
74
|
9 |
|
public function hasHeader($name) |
75
|
|
|
{ |
76
|
9 |
|
return array_key_exists(strtolower($name), $this->getHeaders()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* |
82
|
|
|
* @param string $name Case-insensitive header field name. |
83
|
|
|
* @return string[] An array of string values as provided for the given |
84
|
|
|
* header. If the header does not appear in the message, this method MUST |
85
|
|
|
* return an empty array. |
86
|
|
|
*/ |
87
|
9 |
|
public function getHeader($name) |
88
|
|
|
{ |
89
|
9 |
|
$name = strtolower($name); |
90
|
|
|
|
91
|
9 |
|
return $this->hasHeader($name) ? $this->headers[$name] : []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
* |
97
|
|
|
* @param string $name Case-insensitive header field name. |
98
|
|
|
* @return string A string of values as provided for the given header |
99
|
|
|
* concatenated together using a comma. If the header does not appear in |
100
|
|
|
* the message, this method MUST return an empty string. |
101
|
|
|
*/ |
102
|
1 |
|
public function getHeaderLine($name) |
103
|
|
|
{ |
104
|
1 |
|
return implode(', ', $this->getHeader($name)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
* |
110
|
|
|
* @param string $name Case-insensitive header field name. |
111
|
|
|
* @param string|string[] $value Header value(s). |
112
|
|
|
* @return self |
113
|
|
|
* @throws InvalidArgumentException for invalid header names or values. |
114
|
|
|
*/ |
115
|
6 |
|
public function withHeader($name, $value) |
116
|
|
|
{ |
117
|
6 |
|
if (array_key_exists($name, $this->headers) && in_array($value, $this->headers[$name])) { |
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
$instance = clone $this; |
122
|
6 |
|
$instance->headers[$name] = array_filter((array)$value); |
123
|
6 |
|
return $instance; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
* |
129
|
|
|
* @param string $name Case-insensitive header field name to add. |
130
|
|
|
* @param string|string[] $value Header value(s). |
131
|
|
|
* @return self |
132
|
|
|
* @throws InvalidArgumentException for invalid header names or values. |
133
|
|
|
*/ |
134
|
1 |
|
public function withAddedHeader($name, $value) |
135
|
|
|
{ |
136
|
1 |
|
$instance = clone $this; |
137
|
1 |
|
$instance->headers[$name][] = $value; |
138
|
1 |
|
return $instance; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
* |
144
|
|
|
* @param string $name Case-insensitive header field name to remove. |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
1 |
|
public function withoutHeader($name) |
148
|
|
|
{ |
149
|
1 |
|
$request = new static($this); |
|
|
|
|
150
|
1 |
|
unset($request->headers[$name]); |
151
|
1 |
|
return $request; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
* |
157
|
|
|
* @return StreamInterface Returns the body as a stream. |
158
|
|
|
*/ |
159
|
8 |
|
public function getBody() |
160
|
|
|
{ |
161
|
8 |
|
return $this->stream; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
* |
167
|
|
|
* @param StreamInterface $body Body. |
168
|
|
|
* @return self |
169
|
|
|
* @throws InvalidArgumentException When the body is not valid. |
170
|
|
|
*/ |
171
|
13 |
|
public function withBody(StreamInterface $body) |
172
|
|
|
{ |
173
|
13 |
|
return $this->_clone('stream', $body); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $key |
178
|
|
|
* @param $value |
179
|
|
|
* @return static |
180
|
|
|
*/ |
181
|
41 |
|
protected function _clone($key, $value) |
182
|
|
|
{ |
183
|
41 |
|
if ($this->$key === $value) { |
184
|
7 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
41 |
|
$instance = clone $this; |
188
|
41 |
|
$instance->$key = $value; |
189
|
41 |
|
return $instance; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.