@@ -25,11 +25,11 @@ |
||
| 25 | 25 | class RequestFactory implements RequestFactoryInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function createRequest(string $method, $uri) : RequestInterface |
|
| 32 | - { |
|
| 33 | - return new Request($method, $uri); |
|
| 34 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function createRequest(string $method, $uri) : RequestInterface |
|
| 32 | + { |
|
| 33 | + return new Request($method, $uri); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -39,325 +39,325 @@ |
||
| 39 | 39 | class Message implements MessageInterface |
| 40 | 40 | { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The message protocol version |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $protocolVersion = '1.1'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The message headers |
|
| 51 | - * |
|
| 52 | - * @var array<string, array<string>> |
|
| 53 | - */ |
|
| 54 | - protected $headers = []; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The message body |
|
| 58 | - * |
|
| 59 | - * @var StreamInterface|null |
|
| 60 | - */ |
|
| 61 | - protected $body = null; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Constructor of the class |
|
| 65 | - * |
|
| 66 | - * @param array<string, string|array<string>>|null $headers |
|
| 67 | - * @param StreamInterface|null $body |
|
| 68 | - * @param string|null $protocolVersion |
|
| 69 | - * |
|
| 70 | - * @throws InvalidArgumentException |
|
| 71 | - */ |
|
| 72 | - public function __construct( |
|
| 73 | - ?array $headers = null, |
|
| 74 | - ?StreamInterface $body = null, |
|
| 75 | - ?string $protocolVersion = null |
|
| 76 | - ) { |
|
| 77 | - if (isset($protocolVersion)) { |
|
| 78 | - $this->setProtocolVersion($protocolVersion); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if (isset($headers)) { |
|
| 82 | - foreach ($headers as $name => $value) { |
|
| 83 | - $this->addHeader($name, $value); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if (isset($body)) { |
|
| 88 | - $this->body = $body; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - */ |
|
| 95 | - public function getProtocolVersion() : string |
|
| 96 | - { |
|
| 97 | - return $this->protocolVersion; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * {@inheritdoc} |
|
| 102 | - * |
|
| 103 | - * @throws InvalidArgumentException |
|
| 104 | - */ |
|
| 105 | - public function withProtocolVersion($version) : MessageInterface |
|
| 106 | - { |
|
| 107 | - $clone = clone $this; |
|
| 108 | - $clone->setProtocolVersion($version); |
|
| 109 | - |
|
| 110 | - return $clone; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - */ |
|
| 116 | - public function getHeaders() : array |
|
| 117 | - { |
|
| 118 | - return $this->headers; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * {@inheritdoc} |
|
| 123 | - */ |
|
| 124 | - public function hasHeader($name) : bool |
|
| 125 | - { |
|
| 126 | - $name = $this->normalizeHeaderName($name); |
|
| 127 | - |
|
| 128 | - return ! empty($this->headers[$name]); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * {@inheritdoc} |
|
| 133 | - */ |
|
| 134 | - public function getHeader($name) : array |
|
| 135 | - { |
|
| 136 | - $name = $this->normalizeHeaderName($name); |
|
| 137 | - |
|
| 138 | - return $this->headers[$name] ?? []; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * {@inheritdoc} |
|
| 143 | - */ |
|
| 144 | - public function getHeaderLine($name) : string |
|
| 145 | - { |
|
| 146 | - $name = $this->normalizeHeaderName($name); |
|
| 147 | - |
|
| 148 | - if (empty($this->headers[$name])) { |
|
| 149 | - return ''; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return join(', ', $this->headers[$name]); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * {@inheritdoc} |
|
| 157 | - * |
|
| 158 | - * @throws InvalidArgumentException |
|
| 159 | - */ |
|
| 160 | - public function withHeader($name, $value) : MessageInterface |
|
| 161 | - { |
|
| 162 | - $clone = clone $this; |
|
| 163 | - $clone->addHeader($name, $value); |
|
| 164 | - |
|
| 165 | - return $clone; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * {@inheritdoc} |
|
| 170 | - * |
|
| 171 | - * @throws InvalidArgumentException |
|
| 172 | - */ |
|
| 173 | - public function withAddedHeader($name, $value) : MessageInterface |
|
| 174 | - { |
|
| 175 | - $clone = clone $this; |
|
| 176 | - $clone->addHeader($name, $value, false); |
|
| 177 | - |
|
| 178 | - return $clone; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * {@inheritdoc} |
|
| 183 | - */ |
|
| 184 | - public function withoutHeader($name) : MessageInterface |
|
| 185 | - { |
|
| 186 | - $name = $this->normalizeHeaderName($name); |
|
| 187 | - |
|
| 188 | - $clone = clone $this; |
|
| 189 | - |
|
| 190 | - unset($clone->headers[$name]); |
|
| 191 | - |
|
| 192 | - return $clone; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * {@inheritdoc} |
|
| 197 | - */ |
|
| 198 | - public function getBody() : StreamInterface |
|
| 199 | - { |
|
| 200 | - if (null === $this->body) { |
|
| 201 | - $this->body = (new StreamFactory)->createStream(); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $this->body; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * {@inheritdoc} |
|
| 209 | - */ |
|
| 210 | - public function withBody(StreamInterface $body) : MessageInterface |
|
| 211 | - { |
|
| 212 | - $clone = clone $this; |
|
| 213 | - $clone->body = $body; |
|
| 214 | - |
|
| 215 | - return $clone; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Sets the given protocol version to the message |
|
| 220 | - * |
|
| 221 | - * @param string $version |
|
| 222 | - * |
|
| 223 | - * @return void |
|
| 224 | - * |
|
| 225 | - * @throws InvalidArgumentException |
|
| 226 | - */ |
|
| 227 | - protected function setProtocolVersion($version) : void |
|
| 228 | - { |
|
| 229 | - $this->validateProtocolVersion($version); |
|
| 230 | - |
|
| 231 | - $this->protocolVersion = $version; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Adds the given header field to the message |
|
| 236 | - * |
|
| 237 | - * @param string $name |
|
| 238 | - * @param string|array<string> $value |
|
| 239 | - * @param bool $replace |
|
| 240 | - * |
|
| 241 | - * @return void |
|
| 242 | - * |
|
| 243 | - * @throws InvalidArgumentException |
|
| 244 | - */ |
|
| 245 | - protected function addHeader($name, $value, bool $replace = true) : void |
|
| 246 | - { |
|
| 247 | - $this->validateHeaderName($name); |
|
| 248 | - $this->validateHeaderValue($value); |
|
| 249 | - |
|
| 250 | - $name = $this->normalizeHeaderName($name); |
|
| 251 | - $value = (array) $value; |
|
| 252 | - |
|
| 253 | - if ($replace) { |
|
| 254 | - $this->headers[$name] = $value; |
|
| 255 | - return; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - foreach ($value as $item) { |
|
| 259 | - $this->headers[$name][] = $item; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Validates the given protocol version |
|
| 265 | - * |
|
| 266 | - * @param mixed $version |
|
| 267 | - * |
|
| 268 | - * @return void |
|
| 269 | - * |
|
| 270 | - * @throws InvalidArgumentException |
|
| 271 | - * |
|
| 272 | - * @link https://tools.ietf.org/html/rfc7230#section-2.6 |
|
| 273 | - * @link https://tools.ietf.org/html/rfc7540 |
|
| 274 | - */ |
|
| 275 | - protected function validateProtocolVersion($version) : void |
|
| 276 | - { |
|
| 277 | - // allowed protocol versions: |
|
| 278 | - static $allowed = [ |
|
| 279 | - '1.0' => 1, |
|
| 280 | - '1.1' => 1, |
|
| 281 | - '2.0' => 1, |
|
| 282 | - '2' => 1, |
|
| 283 | - ]; |
|
| 284 | - |
|
| 285 | - if (!is_string($version)) { |
|
| 286 | - throw new InvalidArgumentException('Protocol version must be a string'); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if (!isset($allowed[$version])) { |
|
| 290 | - throw new InvalidArgumentException(sprintf( |
|
| 291 | - 'The protocol version "%s" is not valid. ' . |
|
| 292 | - 'Allowed only: 1.0, 1.1 or 2{.0}', |
|
| 293 | - $version |
|
| 294 | - )); |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Validates the given header name |
|
| 300 | - * |
|
| 301 | - * @param mixed $name |
|
| 302 | - * |
|
| 303 | - * @return void |
|
| 304 | - * |
|
| 305 | - * @throws InvalidArgumentException |
|
| 306 | - * |
|
| 307 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 308 | - */ |
|
| 309 | - protected function validateHeaderName($name) : void |
|
| 310 | - { |
|
| 311 | - if (!is_string($name)) { |
|
| 312 | - throw new InvalidArgumentException('Header name must be a string'); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { |
|
| 316 | - throw new InvalidArgumentException(sprintf('The header name "%s" is not valid', $name)); |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Validates the given header value |
|
| 322 | - * |
|
| 323 | - * @param mixed $value |
|
| 324 | - * |
|
| 325 | - * @return void |
|
| 326 | - * |
|
| 327 | - * @throws InvalidArgumentException |
|
| 328 | - * |
|
| 329 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 330 | - */ |
|
| 331 | - protected function validateHeaderValue($value) : void |
|
| 332 | - { |
|
| 333 | - $items = (array) $value; |
|
| 334 | - |
|
| 335 | - if ([] === $items) { |
|
| 336 | - throw new InvalidArgumentException('Header value must be a string or a non-empty array'); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - foreach ($items as $item) { |
|
| 340 | - if (!is_string($item)) { |
|
| 341 | - throw new InvalidArgumentException('Header value must be a string or an array with strings only'); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $item)) { |
|
| 345 | - throw new InvalidArgumentException(sprintf('The header value "%s" is not valid', $item)); |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Normalizes the given header name |
|
| 352 | - * |
|
| 353 | - * @param string $name |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - * |
|
| 357 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 358 | - */ |
|
| 359 | - protected function normalizeHeaderName($name) : string |
|
| 360 | - { |
|
| 361 | - return ucwords(strtolower($name), '-'); |
|
| 362 | - } |
|
| 42 | + /** |
|
| 43 | + * The message protocol version |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $protocolVersion = '1.1'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The message headers |
|
| 51 | + * |
|
| 52 | + * @var array<string, array<string>> |
|
| 53 | + */ |
|
| 54 | + protected $headers = []; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The message body |
|
| 58 | + * |
|
| 59 | + * @var StreamInterface|null |
|
| 60 | + */ |
|
| 61 | + protected $body = null; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Constructor of the class |
|
| 65 | + * |
|
| 66 | + * @param array<string, string|array<string>>|null $headers |
|
| 67 | + * @param StreamInterface|null $body |
|
| 68 | + * @param string|null $protocolVersion |
|
| 69 | + * |
|
| 70 | + * @throws InvalidArgumentException |
|
| 71 | + */ |
|
| 72 | + public function __construct( |
|
| 73 | + ?array $headers = null, |
|
| 74 | + ?StreamInterface $body = null, |
|
| 75 | + ?string $protocolVersion = null |
|
| 76 | + ) { |
|
| 77 | + if (isset($protocolVersion)) { |
|
| 78 | + $this->setProtocolVersion($protocolVersion); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if (isset($headers)) { |
|
| 82 | + foreach ($headers as $name => $value) { |
|
| 83 | + $this->addHeader($name, $value); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if (isset($body)) { |
|
| 88 | + $this->body = $body; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + */ |
|
| 95 | + public function getProtocolVersion() : string |
|
| 96 | + { |
|
| 97 | + return $this->protocolVersion; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * {@inheritdoc} |
|
| 102 | + * |
|
| 103 | + * @throws InvalidArgumentException |
|
| 104 | + */ |
|
| 105 | + public function withProtocolVersion($version) : MessageInterface |
|
| 106 | + { |
|
| 107 | + $clone = clone $this; |
|
| 108 | + $clone->setProtocolVersion($version); |
|
| 109 | + |
|
| 110 | + return $clone; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + */ |
|
| 116 | + public function getHeaders() : array |
|
| 117 | + { |
|
| 118 | + return $this->headers; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * {@inheritdoc} |
|
| 123 | + */ |
|
| 124 | + public function hasHeader($name) : bool |
|
| 125 | + { |
|
| 126 | + $name = $this->normalizeHeaderName($name); |
|
| 127 | + |
|
| 128 | + return ! empty($this->headers[$name]); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * {@inheritdoc} |
|
| 133 | + */ |
|
| 134 | + public function getHeader($name) : array |
|
| 135 | + { |
|
| 136 | + $name = $this->normalizeHeaderName($name); |
|
| 137 | + |
|
| 138 | + return $this->headers[$name] ?? []; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * {@inheritdoc} |
|
| 143 | + */ |
|
| 144 | + public function getHeaderLine($name) : string |
|
| 145 | + { |
|
| 146 | + $name = $this->normalizeHeaderName($name); |
|
| 147 | + |
|
| 148 | + if (empty($this->headers[$name])) { |
|
| 149 | + return ''; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return join(', ', $this->headers[$name]); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * {@inheritdoc} |
|
| 157 | + * |
|
| 158 | + * @throws InvalidArgumentException |
|
| 159 | + */ |
|
| 160 | + public function withHeader($name, $value) : MessageInterface |
|
| 161 | + { |
|
| 162 | + $clone = clone $this; |
|
| 163 | + $clone->addHeader($name, $value); |
|
| 164 | + |
|
| 165 | + return $clone; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * {@inheritdoc} |
|
| 170 | + * |
|
| 171 | + * @throws InvalidArgumentException |
|
| 172 | + */ |
|
| 173 | + public function withAddedHeader($name, $value) : MessageInterface |
|
| 174 | + { |
|
| 175 | + $clone = clone $this; |
|
| 176 | + $clone->addHeader($name, $value, false); |
|
| 177 | + |
|
| 178 | + return $clone; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * {@inheritdoc} |
|
| 183 | + */ |
|
| 184 | + public function withoutHeader($name) : MessageInterface |
|
| 185 | + { |
|
| 186 | + $name = $this->normalizeHeaderName($name); |
|
| 187 | + |
|
| 188 | + $clone = clone $this; |
|
| 189 | + |
|
| 190 | + unset($clone->headers[$name]); |
|
| 191 | + |
|
| 192 | + return $clone; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * {@inheritdoc} |
|
| 197 | + */ |
|
| 198 | + public function getBody() : StreamInterface |
|
| 199 | + { |
|
| 200 | + if (null === $this->body) { |
|
| 201 | + $this->body = (new StreamFactory)->createStream(); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $this->body; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * {@inheritdoc} |
|
| 209 | + */ |
|
| 210 | + public function withBody(StreamInterface $body) : MessageInterface |
|
| 211 | + { |
|
| 212 | + $clone = clone $this; |
|
| 213 | + $clone->body = $body; |
|
| 214 | + |
|
| 215 | + return $clone; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Sets the given protocol version to the message |
|
| 220 | + * |
|
| 221 | + * @param string $version |
|
| 222 | + * |
|
| 223 | + * @return void |
|
| 224 | + * |
|
| 225 | + * @throws InvalidArgumentException |
|
| 226 | + */ |
|
| 227 | + protected function setProtocolVersion($version) : void |
|
| 228 | + { |
|
| 229 | + $this->validateProtocolVersion($version); |
|
| 230 | + |
|
| 231 | + $this->protocolVersion = $version; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Adds the given header field to the message |
|
| 236 | + * |
|
| 237 | + * @param string $name |
|
| 238 | + * @param string|array<string> $value |
|
| 239 | + * @param bool $replace |
|
| 240 | + * |
|
| 241 | + * @return void |
|
| 242 | + * |
|
| 243 | + * @throws InvalidArgumentException |
|
| 244 | + */ |
|
| 245 | + protected function addHeader($name, $value, bool $replace = true) : void |
|
| 246 | + { |
|
| 247 | + $this->validateHeaderName($name); |
|
| 248 | + $this->validateHeaderValue($value); |
|
| 249 | + |
|
| 250 | + $name = $this->normalizeHeaderName($name); |
|
| 251 | + $value = (array) $value; |
|
| 252 | + |
|
| 253 | + if ($replace) { |
|
| 254 | + $this->headers[$name] = $value; |
|
| 255 | + return; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + foreach ($value as $item) { |
|
| 259 | + $this->headers[$name][] = $item; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Validates the given protocol version |
|
| 265 | + * |
|
| 266 | + * @param mixed $version |
|
| 267 | + * |
|
| 268 | + * @return void |
|
| 269 | + * |
|
| 270 | + * @throws InvalidArgumentException |
|
| 271 | + * |
|
| 272 | + * @link https://tools.ietf.org/html/rfc7230#section-2.6 |
|
| 273 | + * @link https://tools.ietf.org/html/rfc7540 |
|
| 274 | + */ |
|
| 275 | + protected function validateProtocolVersion($version) : void |
|
| 276 | + { |
|
| 277 | + // allowed protocol versions: |
|
| 278 | + static $allowed = [ |
|
| 279 | + '1.0' => 1, |
|
| 280 | + '1.1' => 1, |
|
| 281 | + '2.0' => 1, |
|
| 282 | + '2' => 1, |
|
| 283 | + ]; |
|
| 284 | + |
|
| 285 | + if (!is_string($version)) { |
|
| 286 | + throw new InvalidArgumentException('Protocol version must be a string'); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if (!isset($allowed[$version])) { |
|
| 290 | + throw new InvalidArgumentException(sprintf( |
|
| 291 | + 'The protocol version "%s" is not valid. ' . |
|
| 292 | + 'Allowed only: 1.0, 1.1 or 2{.0}', |
|
| 293 | + $version |
|
| 294 | + )); |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Validates the given header name |
|
| 300 | + * |
|
| 301 | + * @param mixed $name |
|
| 302 | + * |
|
| 303 | + * @return void |
|
| 304 | + * |
|
| 305 | + * @throws InvalidArgumentException |
|
| 306 | + * |
|
| 307 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 308 | + */ |
|
| 309 | + protected function validateHeaderName($name) : void |
|
| 310 | + { |
|
| 311 | + if (!is_string($name)) { |
|
| 312 | + throw new InvalidArgumentException('Header name must be a string'); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { |
|
| 316 | + throw new InvalidArgumentException(sprintf('The header name "%s" is not valid', $name)); |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Validates the given header value |
|
| 322 | + * |
|
| 323 | + * @param mixed $value |
|
| 324 | + * |
|
| 325 | + * @return void |
|
| 326 | + * |
|
| 327 | + * @throws InvalidArgumentException |
|
| 328 | + * |
|
| 329 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 330 | + */ |
|
| 331 | + protected function validateHeaderValue($value) : void |
|
| 332 | + { |
|
| 333 | + $items = (array) $value; |
|
| 334 | + |
|
| 335 | + if ([] === $items) { |
|
| 336 | + throw new InvalidArgumentException('Header value must be a string or a non-empty array'); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + foreach ($items as $item) { |
|
| 340 | + if (!is_string($item)) { |
|
| 341 | + throw new InvalidArgumentException('Header value must be a string or an array with strings only'); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $item)) { |
|
| 345 | + throw new InvalidArgumentException(sprintf('The header value "%s" is not valid', $item)); |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Normalizes the given header name |
|
| 352 | + * |
|
| 353 | + * @param string $name |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + * |
|
| 357 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 358 | + */ |
|
| 359 | + protected function normalizeHeaderName($name) : string |
|
| 360 | + { |
|
| 361 | + return ucwords(strtolower($name), '-'); |
|
| 362 | + } |
|
| 363 | 363 | } |
@@ -125,7 +125,7 @@ |
||
| 125 | 125 | { |
| 126 | 126 | $name = $this->normalizeHeaderName($name); |
| 127 | 127 | |
| 128 | - return ! empty($this->headers[$name]); |
|
| 128 | + return !empty($this->headers[$name]); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -37,142 +37,142 @@ |
||
| 37 | 37 | class Response extends Message implements ResponseInterface, StatusCodeInterface |
| 38 | 38 | { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The response status code |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - protected $statusCode = self::STATUS_OK; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The response reason phrase |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $reasonPhrase = REASON_PHRASES[self::STATUS_OK]; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Constrictor of the class |
|
| 56 | - * |
|
| 57 | - * @param int|null $statusCode |
|
| 58 | - * @param string|null $reasonPhrase |
|
| 59 | - * @param array<string, string|array<string>>|null $headers |
|
| 60 | - * @param StreamInterface|null $body |
|
| 61 | - * @param string|null $protocolVersion |
|
| 62 | - * |
|
| 63 | - * @throws InvalidArgumentException |
|
| 64 | - */ |
|
| 65 | - public function __construct( |
|
| 66 | - ?int $statusCode = null, |
|
| 67 | - ?string $reasonPhrase = null, |
|
| 68 | - ?array $headers = null, |
|
| 69 | - ?StreamInterface $body = null, |
|
| 70 | - ?string $protocolVersion = null |
|
| 71 | - ) { |
|
| 72 | - parent::__construct( |
|
| 73 | - $headers, |
|
| 74 | - $body, |
|
| 75 | - $protocolVersion |
|
| 76 | - ); |
|
| 77 | - |
|
| 78 | - if (isset($statusCode)) { |
|
| 79 | - $this->setStatus($statusCode, $reasonPhrase ?? ''); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function getStatusCode() : int |
|
| 87 | - { |
|
| 88 | - return $this->statusCode; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function getReasonPhrase() : string |
|
| 95 | - { |
|
| 96 | - return $this->reasonPhrase; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * {@inheritdoc} |
|
| 101 | - * |
|
| 102 | - * @throws InvalidArgumentException |
|
| 103 | - */ |
|
| 104 | - public function withStatus($statusCode, $reasonPhrase = '') : ResponseInterface |
|
| 105 | - { |
|
| 106 | - $clone = clone $this; |
|
| 107 | - $clone->setStatus($statusCode, $reasonPhrase); |
|
| 108 | - |
|
| 109 | - return $clone; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Sets the given status to the response |
|
| 114 | - * |
|
| 115 | - * @param int $statusCode |
|
| 116 | - * @param string $reasonPhrase |
|
| 117 | - * |
|
| 118 | - * @return void |
|
| 119 | - * |
|
| 120 | - * @throws InvalidArgumentException |
|
| 121 | - */ |
|
| 122 | - protected function setStatus($statusCode, $reasonPhrase) : void |
|
| 123 | - { |
|
| 124 | - $this->validateStatusCode($statusCode); |
|
| 125 | - $this->validateReasonPhrase($reasonPhrase); |
|
| 126 | - |
|
| 127 | - if ('' === $reasonPhrase) { |
|
| 128 | - $reasonPhrase = REASON_PHRASES[$statusCode] ?? 'Unknown Status Code'; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $this->statusCode = $statusCode; |
|
| 132 | - $this->reasonPhrase = $reasonPhrase; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Validates the given status-code |
|
| 137 | - * |
|
| 138 | - * @param mixed $statusCode |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - * |
|
| 142 | - * @throws InvalidArgumentException |
|
| 143 | - * |
|
| 144 | - * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 145 | - */ |
|
| 146 | - protected function validateStatusCode($statusCode) : void |
|
| 147 | - { |
|
| 148 | - if (!is_int($statusCode)) { |
|
| 149 | - throw new InvalidArgumentException('HTTP status-code must be an integer'); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if (! ($statusCode >= 100 && $statusCode <= 599)) { |
|
| 153 | - throw new InvalidArgumentException(sprintf('The status-code "%d" is not valid', $statusCode)); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Validates the given reason-phrase |
|
| 159 | - * |
|
| 160 | - * @param mixed $reasonPhrase |
|
| 161 | - * |
|
| 162 | - * @return void |
|
| 163 | - * |
|
| 164 | - * @throws InvalidArgumentException |
|
| 165 | - * |
|
| 166 | - * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 167 | - */ |
|
| 168 | - protected function validateReasonPhrase($reasonPhrase) : void |
|
| 169 | - { |
|
| 170 | - if (!is_string($reasonPhrase)) { |
|
| 171 | - throw new InvalidArgumentException('HTTP reason-phrase must be a string'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $reasonPhrase)) { |
|
| 175 | - throw new InvalidArgumentException(sprintf('The reason-phrase "%s" is not valid', $reasonPhrase)); |
|
| 176 | - } |
|
| 177 | - } |
|
| 40 | + /** |
|
| 41 | + * The response status code |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + protected $statusCode = self::STATUS_OK; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The response reason phrase |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $reasonPhrase = REASON_PHRASES[self::STATUS_OK]; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Constrictor of the class |
|
| 56 | + * |
|
| 57 | + * @param int|null $statusCode |
|
| 58 | + * @param string|null $reasonPhrase |
|
| 59 | + * @param array<string, string|array<string>>|null $headers |
|
| 60 | + * @param StreamInterface|null $body |
|
| 61 | + * @param string|null $protocolVersion |
|
| 62 | + * |
|
| 63 | + * @throws InvalidArgumentException |
|
| 64 | + */ |
|
| 65 | + public function __construct( |
|
| 66 | + ?int $statusCode = null, |
|
| 67 | + ?string $reasonPhrase = null, |
|
| 68 | + ?array $headers = null, |
|
| 69 | + ?StreamInterface $body = null, |
|
| 70 | + ?string $protocolVersion = null |
|
| 71 | + ) { |
|
| 72 | + parent::__construct( |
|
| 73 | + $headers, |
|
| 74 | + $body, |
|
| 75 | + $protocolVersion |
|
| 76 | + ); |
|
| 77 | + |
|
| 78 | + if (isset($statusCode)) { |
|
| 79 | + $this->setStatus($statusCode, $reasonPhrase ?? ''); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function getStatusCode() : int |
|
| 87 | + { |
|
| 88 | + return $this->statusCode; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function getReasonPhrase() : string |
|
| 95 | + { |
|
| 96 | + return $this->reasonPhrase; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * {@inheritdoc} |
|
| 101 | + * |
|
| 102 | + * @throws InvalidArgumentException |
|
| 103 | + */ |
|
| 104 | + public function withStatus($statusCode, $reasonPhrase = '') : ResponseInterface |
|
| 105 | + { |
|
| 106 | + $clone = clone $this; |
|
| 107 | + $clone->setStatus($statusCode, $reasonPhrase); |
|
| 108 | + |
|
| 109 | + return $clone; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Sets the given status to the response |
|
| 114 | + * |
|
| 115 | + * @param int $statusCode |
|
| 116 | + * @param string $reasonPhrase |
|
| 117 | + * |
|
| 118 | + * @return void |
|
| 119 | + * |
|
| 120 | + * @throws InvalidArgumentException |
|
| 121 | + */ |
|
| 122 | + protected function setStatus($statusCode, $reasonPhrase) : void |
|
| 123 | + { |
|
| 124 | + $this->validateStatusCode($statusCode); |
|
| 125 | + $this->validateReasonPhrase($reasonPhrase); |
|
| 126 | + |
|
| 127 | + if ('' === $reasonPhrase) { |
|
| 128 | + $reasonPhrase = REASON_PHRASES[$statusCode] ?? 'Unknown Status Code'; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $this->statusCode = $statusCode; |
|
| 132 | + $this->reasonPhrase = $reasonPhrase; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Validates the given status-code |
|
| 137 | + * |
|
| 138 | + * @param mixed $statusCode |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + * |
|
| 142 | + * @throws InvalidArgumentException |
|
| 143 | + * |
|
| 144 | + * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 145 | + */ |
|
| 146 | + protected function validateStatusCode($statusCode) : void |
|
| 147 | + { |
|
| 148 | + if (!is_int($statusCode)) { |
|
| 149 | + throw new InvalidArgumentException('HTTP status-code must be an integer'); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if (! ($statusCode >= 100 && $statusCode <= 599)) { |
|
| 153 | + throw new InvalidArgumentException(sprintf('The status-code "%d" is not valid', $statusCode)); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Validates the given reason-phrase |
|
| 159 | + * |
|
| 160 | + * @param mixed $reasonPhrase |
|
| 161 | + * |
|
| 162 | + * @return void |
|
| 163 | + * |
|
| 164 | + * @throws InvalidArgumentException |
|
| 165 | + * |
|
| 166 | + * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 167 | + */ |
|
| 168 | + protected function validateReasonPhrase($reasonPhrase) : void |
|
| 169 | + { |
|
| 170 | + if (!is_string($reasonPhrase)) { |
|
| 171 | + throw new InvalidArgumentException('HTTP reason-phrase must be a string'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $reasonPhrase)) { |
|
| 175 | + throw new InvalidArgumentException(sprintf('The reason-phrase "%s" is not valid', $reasonPhrase)); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | } |
@@ -149,7 +149,7 @@ |
||
| 149 | 149 | throw new InvalidArgumentException('HTTP status-code must be an integer'); |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - if (! ($statusCode >= 100 && $statusCode <= 599)) { |
|
| 152 | + if (!($statusCode >= 100 && $statusCode <= 599)) { |
|
| 153 | 153 | throw new InvalidArgumentException(sprintf('The status-code "%d" is not valid', $statusCode)); |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -40,254 +40,254 @@ |
||
| 40 | 40 | class Request extends Message implements RequestInterface, RequestMethodInterface |
| 41 | 41 | { |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * The request method (aka verb) |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $method = self::METHOD_GET; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * The request target |
|
| 52 | - * |
|
| 53 | - * @var string|null |
|
| 54 | - */ |
|
| 55 | - protected $requestTarget = null; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The request URI |
|
| 59 | - * |
|
| 60 | - * @var UriInterface|null |
|
| 61 | - */ |
|
| 62 | - protected $uri = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Constructor of the class |
|
| 66 | - * |
|
| 67 | - * @param string|null $method |
|
| 68 | - * @param string|UriInterface|null $uri |
|
| 69 | - * @param array<string, string|array<string>>|null $headers |
|
| 70 | - * @param StreamInterface|null $body |
|
| 71 | - * @param string|null $requestTarget |
|
| 72 | - * @param string|null $protocolVersion |
|
| 73 | - * |
|
| 74 | - * @throws InvalidArgumentException |
|
| 75 | - */ |
|
| 76 | - public function __construct( |
|
| 77 | - ?string $method = null, |
|
| 78 | - $uri = null, |
|
| 79 | - ?array $headers = null, |
|
| 80 | - ?StreamInterface $body = null, |
|
| 81 | - ?string $requestTarget = null, |
|
| 82 | - ?string $protocolVersion = null |
|
| 83 | - ) { |
|
| 84 | - parent::__construct( |
|
| 85 | - $headers, |
|
| 86 | - $body, |
|
| 87 | - $protocolVersion |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - if (isset($method)) { |
|
| 91 | - $this->setMethod($method); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (isset($requestTarget)) { |
|
| 95 | - $this->setRequestTarget($requestTarget); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if (isset($uri)) { |
|
| 99 | - $this->setUri($uri); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * {@inheritdoc} |
|
| 105 | - */ |
|
| 106 | - public function getMethod() : string |
|
| 107 | - { |
|
| 108 | - return $this->method; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * {@inheritdoc} |
|
| 113 | - * |
|
| 114 | - * @throws InvalidArgumentException |
|
| 115 | - */ |
|
| 116 | - public function withMethod($method) : RequestInterface |
|
| 117 | - { |
|
| 118 | - $clone = clone $this; |
|
| 119 | - $clone->setMethod($method); |
|
| 120 | - |
|
| 121 | - return $clone; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * {@inheritdoc} |
|
| 126 | - */ |
|
| 127 | - public function getRequestTarget() : string |
|
| 128 | - { |
|
| 129 | - if (isset($this->requestTarget)) { |
|
| 130 | - return $this->requestTarget; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $uri = $this->getUri(); |
|
| 134 | - |
|
| 135 | - // https://tools.ietf.org/html/rfc7230#section-5.3.1 |
|
| 136 | - // https://tools.ietf.org/html/rfc7230#section-2.7 |
|
| 137 | - // |
|
| 138 | - // origin-form = absolute-path [ "?" query ] |
|
| 139 | - // absolute-path = 1*( "/" segment ) |
|
| 140 | - if (0 <> strncmp($uri->getPath(), '/', 1)) { |
|
| 141 | - return '/'; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $requestTarget = $uri->getPath(); |
|
| 145 | - if ('' !== $uri->getQuery()) { |
|
| 146 | - $requestTarget .= '?' . $uri->getQuery(); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $requestTarget; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * {@inheritdoc} |
|
| 154 | - * |
|
| 155 | - * @throws InvalidArgumentException |
|
| 156 | - */ |
|
| 157 | - public function withRequestTarget($requestTarget) : RequestInterface |
|
| 158 | - { |
|
| 159 | - $clone = clone $this; |
|
| 160 | - $clone->setRequestTarget($requestTarget); |
|
| 161 | - |
|
| 162 | - return $clone; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * {@inheritdoc} |
|
| 167 | - */ |
|
| 168 | - public function getUri() : UriInterface |
|
| 169 | - { |
|
| 170 | - if (null === $this->uri) { |
|
| 171 | - $this->uri = (new UriFactory)->createUri(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $this->uri; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * {@inheritdoc} |
|
| 179 | - */ |
|
| 180 | - public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface |
|
| 181 | - { |
|
| 182 | - $clone = clone $this; |
|
| 183 | - $clone->setUri($uri, $preserveHost); |
|
| 184 | - |
|
| 185 | - return $clone; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Sets the given method to the request |
|
| 190 | - * |
|
| 191 | - * @param string $method |
|
| 192 | - * |
|
| 193 | - * @return void |
|
| 194 | - * |
|
| 195 | - * @throws InvalidArgumentException |
|
| 196 | - */ |
|
| 197 | - protected function setMethod($method) : void |
|
| 198 | - { |
|
| 199 | - $this->validateMethod($method); |
|
| 200 | - |
|
| 201 | - $this->method = strtoupper($method); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Sets the given request-target to the request |
|
| 206 | - * |
|
| 207 | - * @param string $requestTarget |
|
| 208 | - * |
|
| 209 | - * @return void |
|
| 210 | - * |
|
| 211 | - * @throws InvalidArgumentException |
|
| 212 | - */ |
|
| 213 | - protected function setRequestTarget($requestTarget) : void |
|
| 214 | - { |
|
| 215 | - $this->validateRequestTarget($requestTarget); |
|
| 216 | - |
|
| 217 | - $this->requestTarget = $requestTarget; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Sets the given URI to the request |
|
| 222 | - * |
|
| 223 | - * @param string|UriInterface $uri |
|
| 224 | - * @param bool $preserveHost |
|
| 225 | - * |
|
| 226 | - * @return void |
|
| 227 | - * |
|
| 228 | - * @throws InvalidArgumentException |
|
| 229 | - */ |
|
| 230 | - protected function setUri($uri, $preserveHost = false) : void |
|
| 231 | - { |
|
| 232 | - if (! ($uri instanceof UriInterface)) { |
|
| 233 | - $uri = (new UriFactory)->createUri($uri); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $this->uri = $uri; |
|
| 237 | - |
|
| 238 | - if ('' === $uri->getHost() || ($preserveHost && $this->hasHeader('Host'))) { |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - $host = $uri->getHost(); |
|
| 243 | - if (null !== $uri->getPort()) { |
|
| 244 | - $host .= ':' . $uri->getPort(); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $this->addHeader('Host', $host); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Validates the given method |
|
| 252 | - * |
|
| 253 | - * @param mixed $method |
|
| 254 | - * |
|
| 255 | - * @return void |
|
| 256 | - * |
|
| 257 | - * @throws InvalidArgumentException |
|
| 258 | - * |
|
| 259 | - * @link https://tools.ietf.org/html/rfc7230#section-3.1.1 |
|
| 260 | - */ |
|
| 261 | - protected function validateMethod($method) : void |
|
| 262 | - { |
|
| 263 | - if (!is_string($method)) { |
|
| 264 | - throw new InvalidArgumentException('HTTP method must be a string'); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - if (!preg_match(HeaderInterface::RFC7230_TOKEN, $method)) { |
|
| 268 | - throw new InvalidArgumentException(sprintf('The method "%s" is not valid', $method)); |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Validates the given request-target |
|
| 274 | - * |
|
| 275 | - * @param mixed $requestTarget |
|
| 276 | - * |
|
| 277 | - * @return void |
|
| 278 | - * |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * |
|
| 281 | - * @link https://tools.ietf.org/html/rfc7230#section-5.3 |
|
| 282 | - */ |
|
| 283 | - protected function validateRequestTarget($requestTarget) : void |
|
| 284 | - { |
|
| 285 | - if (!is_string($requestTarget)) { |
|
| 286 | - throw new InvalidArgumentException('HTTP request-target must be a string'); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if (!preg_match('/^[\x21-\x7E\x80-\xFF]+$/', $requestTarget)) { |
|
| 290 | - throw new InvalidArgumentException(sprintf('The request-target "%s" is not valid', $requestTarget)); |
|
| 291 | - } |
|
| 292 | - } |
|
| 43 | + /** |
|
| 44 | + * The request method (aka verb) |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $method = self::METHOD_GET; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * The request target |
|
| 52 | + * |
|
| 53 | + * @var string|null |
|
| 54 | + */ |
|
| 55 | + protected $requestTarget = null; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The request URI |
|
| 59 | + * |
|
| 60 | + * @var UriInterface|null |
|
| 61 | + */ |
|
| 62 | + protected $uri = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Constructor of the class |
|
| 66 | + * |
|
| 67 | + * @param string|null $method |
|
| 68 | + * @param string|UriInterface|null $uri |
|
| 69 | + * @param array<string, string|array<string>>|null $headers |
|
| 70 | + * @param StreamInterface|null $body |
|
| 71 | + * @param string|null $requestTarget |
|
| 72 | + * @param string|null $protocolVersion |
|
| 73 | + * |
|
| 74 | + * @throws InvalidArgumentException |
|
| 75 | + */ |
|
| 76 | + public function __construct( |
|
| 77 | + ?string $method = null, |
|
| 78 | + $uri = null, |
|
| 79 | + ?array $headers = null, |
|
| 80 | + ?StreamInterface $body = null, |
|
| 81 | + ?string $requestTarget = null, |
|
| 82 | + ?string $protocolVersion = null |
|
| 83 | + ) { |
|
| 84 | + parent::__construct( |
|
| 85 | + $headers, |
|
| 86 | + $body, |
|
| 87 | + $protocolVersion |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + if (isset($method)) { |
|
| 91 | + $this->setMethod($method); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (isset($requestTarget)) { |
|
| 95 | + $this->setRequestTarget($requestTarget); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if (isset($uri)) { |
|
| 99 | + $this->setUri($uri); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * {@inheritdoc} |
|
| 105 | + */ |
|
| 106 | + public function getMethod() : string |
|
| 107 | + { |
|
| 108 | + return $this->method; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * {@inheritdoc} |
|
| 113 | + * |
|
| 114 | + * @throws InvalidArgumentException |
|
| 115 | + */ |
|
| 116 | + public function withMethod($method) : RequestInterface |
|
| 117 | + { |
|
| 118 | + $clone = clone $this; |
|
| 119 | + $clone->setMethod($method); |
|
| 120 | + |
|
| 121 | + return $clone; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * {@inheritdoc} |
|
| 126 | + */ |
|
| 127 | + public function getRequestTarget() : string |
|
| 128 | + { |
|
| 129 | + if (isset($this->requestTarget)) { |
|
| 130 | + return $this->requestTarget; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $uri = $this->getUri(); |
|
| 134 | + |
|
| 135 | + // https://tools.ietf.org/html/rfc7230#section-5.3.1 |
|
| 136 | + // https://tools.ietf.org/html/rfc7230#section-2.7 |
|
| 137 | + // |
|
| 138 | + // origin-form = absolute-path [ "?" query ] |
|
| 139 | + // absolute-path = 1*( "/" segment ) |
|
| 140 | + if (0 <> strncmp($uri->getPath(), '/', 1)) { |
|
| 141 | + return '/'; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $requestTarget = $uri->getPath(); |
|
| 145 | + if ('' !== $uri->getQuery()) { |
|
| 146 | + $requestTarget .= '?' . $uri->getQuery(); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $requestTarget; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * {@inheritdoc} |
|
| 154 | + * |
|
| 155 | + * @throws InvalidArgumentException |
|
| 156 | + */ |
|
| 157 | + public function withRequestTarget($requestTarget) : RequestInterface |
|
| 158 | + { |
|
| 159 | + $clone = clone $this; |
|
| 160 | + $clone->setRequestTarget($requestTarget); |
|
| 161 | + |
|
| 162 | + return $clone; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * {@inheritdoc} |
|
| 167 | + */ |
|
| 168 | + public function getUri() : UriInterface |
|
| 169 | + { |
|
| 170 | + if (null === $this->uri) { |
|
| 171 | + $this->uri = (new UriFactory)->createUri(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $this->uri; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * {@inheritdoc} |
|
| 179 | + */ |
|
| 180 | + public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface |
|
| 181 | + { |
|
| 182 | + $clone = clone $this; |
|
| 183 | + $clone->setUri($uri, $preserveHost); |
|
| 184 | + |
|
| 185 | + return $clone; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Sets the given method to the request |
|
| 190 | + * |
|
| 191 | + * @param string $method |
|
| 192 | + * |
|
| 193 | + * @return void |
|
| 194 | + * |
|
| 195 | + * @throws InvalidArgumentException |
|
| 196 | + */ |
|
| 197 | + protected function setMethod($method) : void |
|
| 198 | + { |
|
| 199 | + $this->validateMethod($method); |
|
| 200 | + |
|
| 201 | + $this->method = strtoupper($method); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Sets the given request-target to the request |
|
| 206 | + * |
|
| 207 | + * @param string $requestTarget |
|
| 208 | + * |
|
| 209 | + * @return void |
|
| 210 | + * |
|
| 211 | + * @throws InvalidArgumentException |
|
| 212 | + */ |
|
| 213 | + protected function setRequestTarget($requestTarget) : void |
|
| 214 | + { |
|
| 215 | + $this->validateRequestTarget($requestTarget); |
|
| 216 | + |
|
| 217 | + $this->requestTarget = $requestTarget; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Sets the given URI to the request |
|
| 222 | + * |
|
| 223 | + * @param string|UriInterface $uri |
|
| 224 | + * @param bool $preserveHost |
|
| 225 | + * |
|
| 226 | + * @return void |
|
| 227 | + * |
|
| 228 | + * @throws InvalidArgumentException |
|
| 229 | + */ |
|
| 230 | + protected function setUri($uri, $preserveHost = false) : void |
|
| 231 | + { |
|
| 232 | + if (! ($uri instanceof UriInterface)) { |
|
| 233 | + $uri = (new UriFactory)->createUri($uri); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $this->uri = $uri; |
|
| 237 | + |
|
| 238 | + if ('' === $uri->getHost() || ($preserveHost && $this->hasHeader('Host'))) { |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + $host = $uri->getHost(); |
|
| 243 | + if (null !== $uri->getPort()) { |
|
| 244 | + $host .= ':' . $uri->getPort(); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $this->addHeader('Host', $host); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Validates the given method |
|
| 252 | + * |
|
| 253 | + * @param mixed $method |
|
| 254 | + * |
|
| 255 | + * @return void |
|
| 256 | + * |
|
| 257 | + * @throws InvalidArgumentException |
|
| 258 | + * |
|
| 259 | + * @link https://tools.ietf.org/html/rfc7230#section-3.1.1 |
|
| 260 | + */ |
|
| 261 | + protected function validateMethod($method) : void |
|
| 262 | + { |
|
| 263 | + if (!is_string($method)) { |
|
| 264 | + throw new InvalidArgumentException('HTTP method must be a string'); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $method)) { |
|
| 268 | + throw new InvalidArgumentException(sprintf('The method "%s" is not valid', $method)); |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Validates the given request-target |
|
| 274 | + * |
|
| 275 | + * @param mixed $requestTarget |
|
| 276 | + * |
|
| 277 | + * @return void |
|
| 278 | + * |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * |
|
| 281 | + * @link https://tools.ietf.org/html/rfc7230#section-5.3 |
|
| 282 | + */ |
|
| 283 | + protected function validateRequestTarget($requestTarget) : void |
|
| 284 | + { |
|
| 285 | + if (!is_string($requestTarget)) { |
|
| 286 | + throw new InvalidArgumentException('HTTP request-target must be a string'); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if (!preg_match('/^[\x21-\x7E\x80-\xFF]+$/', $requestTarget)) { |
|
| 290 | + throw new InvalidArgumentException(sprintf('The request-target "%s" is not valid', $requestTarget)); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | 293 | } |
@@ -229,7 +229,7 @@ |
||
| 229 | 229 | */ |
| 230 | 230 | protected function setUri($uri, $preserveHost = false) : void |
| 231 | 231 | { |
| 232 | - if (! ($uri instanceof UriInterface)) { |
|
| 232 | + if (!($uri instanceof UriInterface)) { |
|
| 233 | 233 | $uri = (new UriFactory)->createUri($uri); |
| 234 | 234 | } |
| 235 | 235 | |
@@ -18,74 +18,74 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | const REASON_PHRASES = [ |
| 20 | 20 | |
| 21 | - // 1xx |
|
| 22 | - 100 => 'Continue', |
|
| 23 | - 101 => 'Switching Protocols', |
|
| 24 | - 102 => 'Processing', |
|
| 25 | - 103 => 'Early Hints', |
|
| 21 | + // 1xx |
|
| 22 | + 100 => 'Continue', |
|
| 23 | + 101 => 'Switching Protocols', |
|
| 24 | + 102 => 'Processing', |
|
| 25 | + 103 => 'Early Hints', |
|
| 26 | 26 | |
| 27 | - // 2xx |
|
| 28 | - 200 => 'OK', |
|
| 29 | - 201 => 'Created', |
|
| 30 | - 202 => 'Accepted', |
|
| 31 | - 203 => 'Non-Authoritative Information', |
|
| 32 | - 204 => 'No Content', |
|
| 33 | - 205 => 'Reset Content', |
|
| 34 | - 206 => 'Partial Content', |
|
| 35 | - 207 => 'Multi-Status', |
|
| 36 | - 208 => 'Already Reported', |
|
| 37 | - 226 => 'IM Used', |
|
| 27 | + // 2xx |
|
| 28 | + 200 => 'OK', |
|
| 29 | + 201 => 'Created', |
|
| 30 | + 202 => 'Accepted', |
|
| 31 | + 203 => 'Non-Authoritative Information', |
|
| 32 | + 204 => 'No Content', |
|
| 33 | + 205 => 'Reset Content', |
|
| 34 | + 206 => 'Partial Content', |
|
| 35 | + 207 => 'Multi-Status', |
|
| 36 | + 208 => 'Already Reported', |
|
| 37 | + 226 => 'IM Used', |
|
| 38 | 38 | |
| 39 | - // 3xx |
|
| 40 | - 300 => 'Multiple Choices', |
|
| 41 | - 301 => 'Moved Permanently', |
|
| 42 | - 302 => 'Found', |
|
| 43 | - 303 => 'See Other', |
|
| 44 | - 304 => 'Not Modified', |
|
| 45 | - 305 => 'Use Proxy', |
|
| 46 | - 307 => 'Temporary Redirect', |
|
| 47 | - 308 => 'Permanent Redirect', |
|
| 39 | + // 3xx |
|
| 40 | + 300 => 'Multiple Choices', |
|
| 41 | + 301 => 'Moved Permanently', |
|
| 42 | + 302 => 'Found', |
|
| 43 | + 303 => 'See Other', |
|
| 44 | + 304 => 'Not Modified', |
|
| 45 | + 305 => 'Use Proxy', |
|
| 46 | + 307 => 'Temporary Redirect', |
|
| 47 | + 308 => 'Permanent Redirect', |
|
| 48 | 48 | |
| 49 | - // 4xx |
|
| 50 | - 400 => 'Bad Request', |
|
| 51 | - 401 => 'Unauthorized', |
|
| 52 | - 402 => 'Payment Required', |
|
| 53 | - 403 => 'Forbidden', |
|
| 54 | - 404 => 'Not Found', |
|
| 55 | - 405 => 'Method Not Allowed', |
|
| 56 | - 406 => 'Not Acceptable', |
|
| 57 | - 407 => 'Proxy Authentication Required', |
|
| 58 | - 408 => 'Request Timeout', |
|
| 59 | - 409 => 'Conflict', |
|
| 60 | - 410 => 'Gone', |
|
| 61 | - 411 => 'Length Required', |
|
| 62 | - 412 => 'Precondition Failed', |
|
| 63 | - 413 => 'Payload Too Large', |
|
| 64 | - 414 => 'URI Too Long', |
|
| 65 | - 415 => 'Unsupported Media Type', |
|
| 66 | - 416 => 'Range Not Satisfiable', |
|
| 67 | - 417 => 'Expectation Failed', |
|
| 68 | - 421 => 'Misdirected Request', |
|
| 69 | - 422 => 'Unprocessable Entity', |
|
| 70 | - 423 => 'Locked', |
|
| 71 | - 424 => 'Failed Dependency', |
|
| 72 | - 425 => 'Too Early', |
|
| 73 | - 426 => 'Upgrade Required', |
|
| 74 | - 428 => 'Precondition Required', |
|
| 75 | - 429 => 'Too Many Requests', |
|
| 76 | - 431 => 'Request Header Fields Too Large', |
|
| 77 | - 451 => 'Unavailable For Legal Reasons', |
|
| 49 | + // 4xx |
|
| 50 | + 400 => 'Bad Request', |
|
| 51 | + 401 => 'Unauthorized', |
|
| 52 | + 402 => 'Payment Required', |
|
| 53 | + 403 => 'Forbidden', |
|
| 54 | + 404 => 'Not Found', |
|
| 55 | + 405 => 'Method Not Allowed', |
|
| 56 | + 406 => 'Not Acceptable', |
|
| 57 | + 407 => 'Proxy Authentication Required', |
|
| 58 | + 408 => 'Request Timeout', |
|
| 59 | + 409 => 'Conflict', |
|
| 60 | + 410 => 'Gone', |
|
| 61 | + 411 => 'Length Required', |
|
| 62 | + 412 => 'Precondition Failed', |
|
| 63 | + 413 => 'Payload Too Large', |
|
| 64 | + 414 => 'URI Too Long', |
|
| 65 | + 415 => 'Unsupported Media Type', |
|
| 66 | + 416 => 'Range Not Satisfiable', |
|
| 67 | + 417 => 'Expectation Failed', |
|
| 68 | + 421 => 'Misdirected Request', |
|
| 69 | + 422 => 'Unprocessable Entity', |
|
| 70 | + 423 => 'Locked', |
|
| 71 | + 424 => 'Failed Dependency', |
|
| 72 | + 425 => 'Too Early', |
|
| 73 | + 426 => 'Upgrade Required', |
|
| 74 | + 428 => 'Precondition Required', |
|
| 75 | + 429 => 'Too Many Requests', |
|
| 76 | + 431 => 'Request Header Fields Too Large', |
|
| 77 | + 451 => 'Unavailable For Legal Reasons', |
|
| 78 | 78 | |
| 79 | - // 5xx |
|
| 80 | - 500 => 'Internal Server Error', |
|
| 81 | - 501 => 'Not Implemented', |
|
| 82 | - 502 => 'Bad Gateway', |
|
| 83 | - 503 => 'Service Unavailable', |
|
| 84 | - 504 => 'Gateway Timeout', |
|
| 85 | - 505 => 'HTTP Version Not Supported', |
|
| 86 | - 506 => 'Variant Also Negotiates', |
|
| 87 | - 507 => 'Insufficient Storage', |
|
| 88 | - 508 => 'Loop Detected', |
|
| 89 | - 510 => 'Not Extended', |
|
| 90 | - 511 => 'Network Authentication Required', |
|
| 79 | + // 5xx |
|
| 80 | + 500 => 'Internal Server Error', |
|
| 81 | + 501 => 'Not Implemented', |
|
| 82 | + 502 => 'Bad Gateway', |
|
| 83 | + 503 => 'Service Unavailable', |
|
| 84 | + 504 => 'Gateway Timeout', |
|
| 85 | + 505 => 'HTTP Version Not Supported', |
|
| 86 | + 506 => 'Variant Also Negotiates', |
|
| 87 | + 507 => 'Insufficient Storage', |
|
| 88 | + 508 => 'Loop Detected', |
|
| 89 | + 510 => 'Not Extended', |
|
| 90 | + 511 => 'Network Authentication Required', |
|
| 91 | 91 | ]; |
@@ -38,61 +38,61 @@ |
||
| 38 | 38 | class ResponseFactory implements ResponseFactoryInterface |
| 39 | 39 | { |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - */ |
|
| 44 | - public function createResponse(int $statusCode = 200, string $reasonPhrase = '') : ResponseInterface |
|
| 45 | - { |
|
| 46 | - return new Response($statusCode, $reasonPhrase); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Creates a HTML response instance |
|
| 51 | - * |
|
| 52 | - * @param int $status |
|
| 53 | - * @param mixed $content |
|
| 54 | - * |
|
| 55 | - * @return ResponseInterface |
|
| 56 | - */ |
|
| 57 | - public function createHtmlResponse(int $status, $content) : ResponseInterface |
|
| 58 | - { |
|
| 59 | - $content = (string) $content; |
|
| 60 | - |
|
| 61 | - $headers = ['Content-Type' => 'text/html; charset=UTF-8']; |
|
| 62 | - |
|
| 63 | - $response = new Response($status, null, $headers); |
|
| 64 | - |
|
| 65 | - $response->getBody()->write($content); |
|
| 66 | - |
|
| 67 | - return $response; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Creates a JSON response instance |
|
| 72 | - * |
|
| 73 | - * @param int $status |
|
| 74 | - * @param mixed $payload |
|
| 75 | - * @param int $options |
|
| 76 | - * @param int $depth |
|
| 77 | - * |
|
| 78 | - * @return ResponseInterface |
|
| 79 | - * |
|
| 80 | - * @throws InvalidArgumentException |
|
| 81 | - */ |
|
| 82 | - public function createJsonResponse(int $status, $payload, int $options = 0, int $depth = 512) : ResponseInterface |
|
| 83 | - { |
|
| 84 | - json_encode(''); // reset previous error... |
|
| 85 | - $content = json_encode($payload, $options, $depth); |
|
| 86 | - if (JSON_ERROR_NONE <> json_last_error()) { |
|
| 87 | - throw new InvalidArgumentException(json_last_error_msg()); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $headers = ['Content-Type' => 'application/json; charset=UTF-8']; |
|
| 91 | - |
|
| 92 | - $response = new Response($status, null, $headers); |
|
| 93 | - |
|
| 94 | - $response->getBody()->write($content); |
|
| 95 | - |
|
| 96 | - return $response; |
|
| 97 | - } |
|
| 41 | + /** |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + */ |
|
| 44 | + public function createResponse(int $statusCode = 200, string $reasonPhrase = '') : ResponseInterface |
|
| 45 | + { |
|
| 46 | + return new Response($statusCode, $reasonPhrase); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Creates a HTML response instance |
|
| 51 | + * |
|
| 52 | + * @param int $status |
|
| 53 | + * @param mixed $content |
|
| 54 | + * |
|
| 55 | + * @return ResponseInterface |
|
| 56 | + */ |
|
| 57 | + public function createHtmlResponse(int $status, $content) : ResponseInterface |
|
| 58 | + { |
|
| 59 | + $content = (string) $content; |
|
| 60 | + |
|
| 61 | + $headers = ['Content-Type' => 'text/html; charset=UTF-8']; |
|
| 62 | + |
|
| 63 | + $response = new Response($status, null, $headers); |
|
| 64 | + |
|
| 65 | + $response->getBody()->write($content); |
|
| 66 | + |
|
| 67 | + return $response; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Creates a JSON response instance |
|
| 72 | + * |
|
| 73 | + * @param int $status |
|
| 74 | + * @param mixed $payload |
|
| 75 | + * @param int $options |
|
| 76 | + * @param int $depth |
|
| 77 | + * |
|
| 78 | + * @return ResponseInterface |
|
| 79 | + * |
|
| 80 | + * @throws InvalidArgumentException |
|
| 81 | + */ |
|
| 82 | + public function createJsonResponse(int $status, $payload, int $options = 0, int $depth = 512) : ResponseInterface |
|
| 83 | + { |
|
| 84 | + json_encode(''); // reset previous error... |
|
| 85 | + $content = json_encode($payload, $options, $depth); |
|
| 86 | + if (JSON_ERROR_NONE <> json_last_error()) { |
|
| 87 | + throw new InvalidArgumentException(json_last_error_msg()); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $headers = ['Content-Type' => 'application/json; charset=UTF-8']; |
|
| 91 | + |
|
| 92 | + $response = new Response($status, null, $headers); |
|
| 93 | + |
|
| 94 | + $response->getBody()->write($content); |
|
| 95 | + |
|
| 96 | + return $response; |
|
| 97 | + } |
|
| 98 | 98 | } |