@@ -39,394 +39,394 @@ |
||
| 39 | 39 | abstract class Message implements MessageInterface |
| 40 | 40 | { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Supported HTTP versions |
|
| 44 | - * |
|
| 45 | - * @var list<string> |
|
| 46 | - */ |
|
| 47 | - public const SUPPORTED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2']; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Default HTTP version |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - public const DEFAULT_HTTP_VERSION = '1.1'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The message HTTP version |
|
| 58 | - * |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private string $protocolVersion = self::DEFAULT_HTTP_VERSION; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The message headers |
|
| 65 | - * |
|
| 66 | - * @var array<string, list<string>> |
|
| 67 | - */ |
|
| 68 | - private array $headers = []; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Original header names (see $headers) |
|
| 72 | - * |
|
| 73 | - * @var array<string, string> |
|
| 74 | - */ |
|
| 75 | - private array $headerNames = []; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The message body |
|
| 79 | - * |
|
| 80 | - * @var StreamInterface|null |
|
| 81 | - */ |
|
| 82 | - private ?StreamInterface $body = null; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Gets the message HTTP version |
|
| 86 | - * |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - public function getProtocolVersion(): string |
|
| 90 | - { |
|
| 91 | - return $this->protocolVersion; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Creates a new instance of the message with the given HTTP version |
|
| 96 | - * |
|
| 97 | - * @param string $version |
|
| 98 | - * |
|
| 99 | - * @return static |
|
| 100 | - * |
|
| 101 | - * @throws InvalidArgumentException |
|
| 102 | - * If the HTTP version isn't valid. |
|
| 103 | - */ |
|
| 104 | - public function withProtocolVersion($version): MessageInterface |
|
| 105 | - { |
|
| 106 | - $clone = clone $this; |
|
| 107 | - $clone->setProtocolVersion($version); |
|
| 108 | - |
|
| 109 | - return $clone; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Gets the message headers |
|
| 114 | - * |
|
| 115 | - * @return array<string, list<string>> |
|
| 116 | - */ |
|
| 117 | - public function getHeaders(): array |
|
| 118 | - { |
|
| 119 | - return $this->headers; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Checks if a header exists in the message by the given name |
|
| 124 | - * |
|
| 125 | - * @param string $name |
|
| 126 | - * |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function hasHeader($name): bool |
|
| 130 | - { |
|
| 131 | - $key = strtolower($name); |
|
| 132 | - |
|
| 133 | - return isset($this->headerNames[$key]); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Gets a header value(s) from the message by the given name |
|
| 138 | - * |
|
| 139 | - * @param string $name |
|
| 140 | - * |
|
| 141 | - * @return list<string> |
|
| 142 | - */ |
|
| 143 | - public function getHeader($name): array |
|
| 144 | - { |
|
| 145 | - $key = strtolower($name); |
|
| 146 | - |
|
| 147 | - if (!isset($this->headerNames[$key])) { |
|
| 148 | - return []; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $this->headers[$this->headerNames[$key]]; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Gets a header value as a string from the message by the given name |
|
| 156 | - * |
|
| 157 | - * @param string $name |
|
| 158 | - * |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - public function getHeaderLine($name): string |
|
| 162 | - { |
|
| 163 | - $key = strtolower($name); |
|
| 164 | - |
|
| 165 | - if (!isset($this->headerNames[$key])) { |
|
| 166 | - return ''; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return implode(',', $this->headers[$this->headerNames[$key]]); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Creates a new instance of the message with the given header overwriting the old header |
|
| 174 | - * |
|
| 175 | - * @param string $name |
|
| 176 | - * @param string|string[] $value |
|
| 177 | - * |
|
| 178 | - * @return static |
|
| 179 | - * |
|
| 180 | - * @throws InvalidArgumentException |
|
| 181 | - * If the header isn't valid. |
|
| 182 | - */ |
|
| 183 | - public function withHeader($name, $value): MessageInterface |
|
| 184 | - { |
|
| 185 | - $clone = clone $this; |
|
| 186 | - $clone->setHeader($name, $value, true); |
|
| 187 | - |
|
| 188 | - return $clone; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Creates a new instance of the message with the given header NOT overwriting the old header |
|
| 193 | - * |
|
| 194 | - * @param string $name |
|
| 195 | - * @param string|string[] $value |
|
| 196 | - * |
|
| 197 | - * @return static |
|
| 198 | - * |
|
| 199 | - * @throws InvalidArgumentException |
|
| 200 | - * If the header isn't valid. |
|
| 201 | - */ |
|
| 202 | - public function withAddedHeader($name, $value): MessageInterface |
|
| 203 | - { |
|
| 204 | - $clone = clone $this; |
|
| 205 | - $clone->setHeader($name, $value, false); |
|
| 206 | - |
|
| 207 | - return $clone; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Creates a new instance of the message without a header by the given name |
|
| 212 | - * |
|
| 213 | - * @param string $name |
|
| 214 | - * |
|
| 215 | - * @return static |
|
| 216 | - */ |
|
| 217 | - public function withoutHeader($name): MessageInterface |
|
| 218 | - { |
|
| 219 | - $clone = clone $this; |
|
| 220 | - $clone->deleteHeader($name); |
|
| 221 | - |
|
| 222 | - return $clone; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Gets the message body |
|
| 227 | - * |
|
| 228 | - * @return StreamInterface |
|
| 229 | - */ |
|
| 230 | - public function getBody(): StreamInterface |
|
| 231 | - { |
|
| 232 | - return $this->body ??= new PhpTempStream(); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Creates a new instance of the message with the given body |
|
| 237 | - * |
|
| 238 | - * @param StreamInterface $body |
|
| 239 | - * |
|
| 240 | - * @return static |
|
| 241 | - */ |
|
| 242 | - public function withBody(StreamInterface $body): MessageInterface |
|
| 243 | - { |
|
| 244 | - $clone = clone $this; |
|
| 245 | - $clone->setBody($body); |
|
| 246 | - |
|
| 247 | - return $clone; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Sets the given HTTP version to the message |
|
| 252 | - * |
|
| 253 | - * @param string $protocolVersion |
|
| 254 | - * |
|
| 255 | - * @return void |
|
| 256 | - * |
|
| 257 | - * @throws InvalidArgumentException |
|
| 258 | - * If the HTTP version isn't valid. |
|
| 259 | - */ |
|
| 260 | - final protected function setProtocolVersion($protocolVersion): void |
|
| 261 | - { |
|
| 262 | - $this->validateProtocolVersion($protocolVersion); |
|
| 263 | - |
|
| 264 | - $this->protocolVersion = $protocolVersion; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Sets a new header to the message with the given name and value(s) |
|
| 269 | - * |
|
| 270 | - * @param string $name |
|
| 271 | - * @param string|string[] $value |
|
| 272 | - * @param bool $replace |
|
| 273 | - * |
|
| 274 | - * @return void |
|
| 275 | - * |
|
| 276 | - * @throws InvalidArgumentException |
|
| 277 | - * If the header isn't valid. |
|
| 278 | - */ |
|
| 279 | - final protected function setHeader($name, $value, bool $replace = true): void |
|
| 280 | - { |
|
| 281 | - if (!is_array($value)) { |
|
| 282 | - $value = [$value]; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $this->validateHeaderName($name); |
|
| 286 | - $this->validateHeaderValue($name, $value); |
|
| 287 | - |
|
| 288 | - if ($replace) { |
|
| 289 | - $this->deleteHeader($name); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $key = strtolower($name); |
|
| 293 | - |
|
| 294 | - $this->headerNames[$key] ??= $name; |
|
| 295 | - $this->headers[$this->headerNames[$key]] ??= []; |
|
| 296 | - |
|
| 297 | - foreach ($value as $item) { |
|
| 298 | - $this->headers[$this->headerNames[$key]][] = $item; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Sets the given headers to the message |
|
| 304 | - * |
|
| 305 | - * @param array<string, string|string[]> $headers |
|
| 306 | - * |
|
| 307 | - * @return void |
|
| 308 | - * |
|
| 309 | - * @throws InvalidArgumentException |
|
| 310 | - * If one of the headers isn't valid. |
|
| 311 | - */ |
|
| 312 | - final protected function setHeaders(array $headers): void |
|
| 313 | - { |
|
| 314 | - foreach ($headers as $name => $value) { |
|
| 315 | - $this->setHeader($name, $value, false); |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Deletes a header from the message by the given name |
|
| 321 | - * |
|
| 322 | - * @param string $name |
|
| 323 | - * |
|
| 324 | - * @return void |
|
| 325 | - */ |
|
| 326 | - final protected function deleteHeader($name): void |
|
| 327 | - { |
|
| 328 | - $key = strtolower($name); |
|
| 329 | - |
|
| 330 | - if (isset($this->headerNames[$key])) { |
|
| 331 | - unset($this->headers[$this->headerNames[$key]]); |
|
| 332 | - unset($this->headerNames[$key]); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Sets the given body to the message |
|
| 338 | - * |
|
| 339 | - * @param StreamInterface $body |
|
| 340 | - * |
|
| 341 | - * @return void |
|
| 342 | - */ |
|
| 343 | - final protected function setBody(StreamInterface $body): void |
|
| 344 | - { |
|
| 345 | - $this->body = $body; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Validates the given HTTP version |
|
| 350 | - * |
|
| 351 | - * @param mixed $protocolVersion |
|
| 352 | - * |
|
| 353 | - * @return void |
|
| 354 | - * |
|
| 355 | - * @throws InvalidArgumentException |
|
| 356 | - * If the HTTP version isn't valid. |
|
| 357 | - */ |
|
| 358 | - private function validateProtocolVersion($protocolVersion): void |
|
| 359 | - { |
|
| 360 | - if (!in_array($protocolVersion, self::SUPPORTED_HTTP_VERSIONS, true)) { |
|
| 361 | - throw new InvalidArgumentException('Invalid or unsupported HTTP version'); |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Validates the given header name |
|
| 367 | - * |
|
| 368 | - * @param mixed $name |
|
| 369 | - * |
|
| 370 | - * @return void |
|
| 371 | - * |
|
| 372 | - * @throws InvalidArgumentException |
|
| 373 | - * If the header name isn't valid. |
|
| 374 | - */ |
|
| 375 | - private function validateHeaderName($name): void |
|
| 376 | - { |
|
| 377 | - if ($name === '') { |
|
| 378 | - throw new InvalidArgumentException('HTTP header name cannot be an empty'); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - if (!is_string($name)) { |
|
| 382 | - throw new InvalidArgumentException('HTTP header name must be a string'); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) { |
|
| 386 | - throw new InvalidArgumentException('HTTP header name is invalid'); |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Validates the given header value |
|
| 392 | - * |
|
| 393 | - * @param string $name |
|
| 394 | - * @param array $value |
|
| 395 | - * |
|
| 396 | - * @return void |
|
| 397 | - * |
|
| 398 | - * @throws InvalidArgumentException |
|
| 399 | - * If the header value isn't valid. |
|
| 400 | - */ |
|
| 401 | - private function validateHeaderValue(string $name, array $value): void |
|
| 402 | - { |
|
| 403 | - if ([] === $value) { |
|
| 404 | - throw new InvalidArgumentException(sprintf( |
|
| 405 | - 'The "%s" HTTP header value cannot be an empty array', |
|
| 406 | - $name, |
|
| 407 | - )); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - foreach ($value as $key => $item) { |
|
| 411 | - if ('' === $item) { |
|
| 412 | - continue; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - if (!is_string($item)) { |
|
| 416 | - throw new InvalidArgumentException(sprintf( |
|
| 417 | - 'The "%s[%s]" HTTP header value must be a string', |
|
| 418 | - $name, |
|
| 419 | - $key |
|
| 420 | - )); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) { |
|
| 424 | - throw new InvalidArgumentException(sprintf( |
|
| 425 | - 'The "%s[%s]" HTTP header value is invalid', |
|
| 426 | - $name, |
|
| 427 | - $key |
|
| 428 | - )); |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - } |
|
| 42 | + /** |
|
| 43 | + * Supported HTTP versions |
|
| 44 | + * |
|
| 45 | + * @var list<string> |
|
| 46 | + */ |
|
| 47 | + public const SUPPORTED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2']; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Default HTTP version |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + public const DEFAULT_HTTP_VERSION = '1.1'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The message HTTP version |
|
| 58 | + * |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private string $protocolVersion = self::DEFAULT_HTTP_VERSION; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The message headers |
|
| 65 | + * |
|
| 66 | + * @var array<string, list<string>> |
|
| 67 | + */ |
|
| 68 | + private array $headers = []; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Original header names (see $headers) |
|
| 72 | + * |
|
| 73 | + * @var array<string, string> |
|
| 74 | + */ |
|
| 75 | + private array $headerNames = []; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The message body |
|
| 79 | + * |
|
| 80 | + * @var StreamInterface|null |
|
| 81 | + */ |
|
| 82 | + private ?StreamInterface $body = null; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Gets the message HTTP version |
|
| 86 | + * |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + public function getProtocolVersion(): string |
|
| 90 | + { |
|
| 91 | + return $this->protocolVersion; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Creates a new instance of the message with the given HTTP version |
|
| 96 | + * |
|
| 97 | + * @param string $version |
|
| 98 | + * |
|
| 99 | + * @return static |
|
| 100 | + * |
|
| 101 | + * @throws InvalidArgumentException |
|
| 102 | + * If the HTTP version isn't valid. |
|
| 103 | + */ |
|
| 104 | + public function withProtocolVersion($version): MessageInterface |
|
| 105 | + { |
|
| 106 | + $clone = clone $this; |
|
| 107 | + $clone->setProtocolVersion($version); |
|
| 108 | + |
|
| 109 | + return $clone; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Gets the message headers |
|
| 114 | + * |
|
| 115 | + * @return array<string, list<string>> |
|
| 116 | + */ |
|
| 117 | + public function getHeaders(): array |
|
| 118 | + { |
|
| 119 | + return $this->headers; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Checks if a header exists in the message by the given name |
|
| 124 | + * |
|
| 125 | + * @param string $name |
|
| 126 | + * |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function hasHeader($name): bool |
|
| 130 | + { |
|
| 131 | + $key = strtolower($name); |
|
| 132 | + |
|
| 133 | + return isset($this->headerNames[$key]); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Gets a header value(s) from the message by the given name |
|
| 138 | + * |
|
| 139 | + * @param string $name |
|
| 140 | + * |
|
| 141 | + * @return list<string> |
|
| 142 | + */ |
|
| 143 | + public function getHeader($name): array |
|
| 144 | + { |
|
| 145 | + $key = strtolower($name); |
|
| 146 | + |
|
| 147 | + if (!isset($this->headerNames[$key])) { |
|
| 148 | + return []; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $this->headers[$this->headerNames[$key]]; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Gets a header value as a string from the message by the given name |
|
| 156 | + * |
|
| 157 | + * @param string $name |
|
| 158 | + * |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + public function getHeaderLine($name): string |
|
| 162 | + { |
|
| 163 | + $key = strtolower($name); |
|
| 164 | + |
|
| 165 | + if (!isset($this->headerNames[$key])) { |
|
| 166 | + return ''; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return implode(',', $this->headers[$this->headerNames[$key]]); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Creates a new instance of the message with the given header overwriting the old header |
|
| 174 | + * |
|
| 175 | + * @param string $name |
|
| 176 | + * @param string|string[] $value |
|
| 177 | + * |
|
| 178 | + * @return static |
|
| 179 | + * |
|
| 180 | + * @throws InvalidArgumentException |
|
| 181 | + * If the header isn't valid. |
|
| 182 | + */ |
|
| 183 | + public function withHeader($name, $value): MessageInterface |
|
| 184 | + { |
|
| 185 | + $clone = clone $this; |
|
| 186 | + $clone->setHeader($name, $value, true); |
|
| 187 | + |
|
| 188 | + return $clone; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Creates a new instance of the message with the given header NOT overwriting the old header |
|
| 193 | + * |
|
| 194 | + * @param string $name |
|
| 195 | + * @param string|string[] $value |
|
| 196 | + * |
|
| 197 | + * @return static |
|
| 198 | + * |
|
| 199 | + * @throws InvalidArgumentException |
|
| 200 | + * If the header isn't valid. |
|
| 201 | + */ |
|
| 202 | + public function withAddedHeader($name, $value): MessageInterface |
|
| 203 | + { |
|
| 204 | + $clone = clone $this; |
|
| 205 | + $clone->setHeader($name, $value, false); |
|
| 206 | + |
|
| 207 | + return $clone; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Creates a new instance of the message without a header by the given name |
|
| 212 | + * |
|
| 213 | + * @param string $name |
|
| 214 | + * |
|
| 215 | + * @return static |
|
| 216 | + */ |
|
| 217 | + public function withoutHeader($name): MessageInterface |
|
| 218 | + { |
|
| 219 | + $clone = clone $this; |
|
| 220 | + $clone->deleteHeader($name); |
|
| 221 | + |
|
| 222 | + return $clone; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Gets the message body |
|
| 227 | + * |
|
| 228 | + * @return StreamInterface |
|
| 229 | + */ |
|
| 230 | + public function getBody(): StreamInterface |
|
| 231 | + { |
|
| 232 | + return $this->body ??= new PhpTempStream(); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Creates a new instance of the message with the given body |
|
| 237 | + * |
|
| 238 | + * @param StreamInterface $body |
|
| 239 | + * |
|
| 240 | + * @return static |
|
| 241 | + */ |
|
| 242 | + public function withBody(StreamInterface $body): MessageInterface |
|
| 243 | + { |
|
| 244 | + $clone = clone $this; |
|
| 245 | + $clone->setBody($body); |
|
| 246 | + |
|
| 247 | + return $clone; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Sets the given HTTP version to the message |
|
| 252 | + * |
|
| 253 | + * @param string $protocolVersion |
|
| 254 | + * |
|
| 255 | + * @return void |
|
| 256 | + * |
|
| 257 | + * @throws InvalidArgumentException |
|
| 258 | + * If the HTTP version isn't valid. |
|
| 259 | + */ |
|
| 260 | + final protected function setProtocolVersion($protocolVersion): void |
|
| 261 | + { |
|
| 262 | + $this->validateProtocolVersion($protocolVersion); |
|
| 263 | + |
|
| 264 | + $this->protocolVersion = $protocolVersion; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Sets a new header to the message with the given name and value(s) |
|
| 269 | + * |
|
| 270 | + * @param string $name |
|
| 271 | + * @param string|string[] $value |
|
| 272 | + * @param bool $replace |
|
| 273 | + * |
|
| 274 | + * @return void |
|
| 275 | + * |
|
| 276 | + * @throws InvalidArgumentException |
|
| 277 | + * If the header isn't valid. |
|
| 278 | + */ |
|
| 279 | + final protected function setHeader($name, $value, bool $replace = true): void |
|
| 280 | + { |
|
| 281 | + if (!is_array($value)) { |
|
| 282 | + $value = [$value]; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $this->validateHeaderName($name); |
|
| 286 | + $this->validateHeaderValue($name, $value); |
|
| 287 | + |
|
| 288 | + if ($replace) { |
|
| 289 | + $this->deleteHeader($name); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $key = strtolower($name); |
|
| 293 | + |
|
| 294 | + $this->headerNames[$key] ??= $name; |
|
| 295 | + $this->headers[$this->headerNames[$key]] ??= []; |
|
| 296 | + |
|
| 297 | + foreach ($value as $item) { |
|
| 298 | + $this->headers[$this->headerNames[$key]][] = $item; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Sets the given headers to the message |
|
| 304 | + * |
|
| 305 | + * @param array<string, string|string[]> $headers |
|
| 306 | + * |
|
| 307 | + * @return void |
|
| 308 | + * |
|
| 309 | + * @throws InvalidArgumentException |
|
| 310 | + * If one of the headers isn't valid. |
|
| 311 | + */ |
|
| 312 | + final protected function setHeaders(array $headers): void |
|
| 313 | + { |
|
| 314 | + foreach ($headers as $name => $value) { |
|
| 315 | + $this->setHeader($name, $value, false); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Deletes a header from the message by the given name |
|
| 321 | + * |
|
| 322 | + * @param string $name |
|
| 323 | + * |
|
| 324 | + * @return void |
|
| 325 | + */ |
|
| 326 | + final protected function deleteHeader($name): void |
|
| 327 | + { |
|
| 328 | + $key = strtolower($name); |
|
| 329 | + |
|
| 330 | + if (isset($this->headerNames[$key])) { |
|
| 331 | + unset($this->headers[$this->headerNames[$key]]); |
|
| 332 | + unset($this->headerNames[$key]); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Sets the given body to the message |
|
| 338 | + * |
|
| 339 | + * @param StreamInterface $body |
|
| 340 | + * |
|
| 341 | + * @return void |
|
| 342 | + */ |
|
| 343 | + final protected function setBody(StreamInterface $body): void |
|
| 344 | + { |
|
| 345 | + $this->body = $body; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Validates the given HTTP version |
|
| 350 | + * |
|
| 351 | + * @param mixed $protocolVersion |
|
| 352 | + * |
|
| 353 | + * @return void |
|
| 354 | + * |
|
| 355 | + * @throws InvalidArgumentException |
|
| 356 | + * If the HTTP version isn't valid. |
|
| 357 | + */ |
|
| 358 | + private function validateProtocolVersion($protocolVersion): void |
|
| 359 | + { |
|
| 360 | + if (!in_array($protocolVersion, self::SUPPORTED_HTTP_VERSIONS, true)) { |
|
| 361 | + throw new InvalidArgumentException('Invalid or unsupported HTTP version'); |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Validates the given header name |
|
| 367 | + * |
|
| 368 | + * @param mixed $name |
|
| 369 | + * |
|
| 370 | + * @return void |
|
| 371 | + * |
|
| 372 | + * @throws InvalidArgumentException |
|
| 373 | + * If the header name isn't valid. |
|
| 374 | + */ |
|
| 375 | + private function validateHeaderName($name): void |
|
| 376 | + { |
|
| 377 | + if ($name === '') { |
|
| 378 | + throw new InvalidArgumentException('HTTP header name cannot be an empty'); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + if (!is_string($name)) { |
|
| 382 | + throw new InvalidArgumentException('HTTP header name must be a string'); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) { |
|
| 386 | + throw new InvalidArgumentException('HTTP header name is invalid'); |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Validates the given header value |
|
| 392 | + * |
|
| 393 | + * @param string $name |
|
| 394 | + * @param array $value |
|
| 395 | + * |
|
| 396 | + * @return void |
|
| 397 | + * |
|
| 398 | + * @throws InvalidArgumentException |
|
| 399 | + * If the header value isn't valid. |
|
| 400 | + */ |
|
| 401 | + private function validateHeaderValue(string $name, array $value): void |
|
| 402 | + { |
|
| 403 | + if ([] === $value) { |
|
| 404 | + throw new InvalidArgumentException(sprintf( |
|
| 405 | + 'The "%s" HTTP header value cannot be an empty array', |
|
| 406 | + $name, |
|
| 407 | + )); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + foreach ($value as $key => $item) { |
|
| 411 | + if ('' === $item) { |
|
| 412 | + continue; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + if (!is_string($item)) { |
|
| 416 | + throw new InvalidArgumentException(sprintf( |
|
| 417 | + 'The "%s[%s]" HTTP header value must be a string', |
|
| 418 | + $name, |
|
| 419 | + $key |
|
| 420 | + )); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) { |
|
| 424 | + throw new InvalidArgumentException(sprintf( |
|
| 425 | + 'The "%s[%s]" HTTP header value is invalid', |
|
| 426 | + $name, |
|
| 427 | + $key |
|
| 428 | + )); |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | 432 | } |
@@ -31,53 +31,53 @@ |
||
| 31 | 31 | final class Path implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x40-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x40-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "path" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "path" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * {@inheritdoc} |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getValue(): string |
|
| 80 | - { |
|
| 81 | - return $this->value; |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * {@inheritdoc} |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getValue(): string |
|
| 80 | + { |
|
| 81 | + return $this->value; |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -31,71 +31,71 @@ |
||
| 31 | 31 | final class User implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "user" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "user" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a user component |
|
| 76 | - * |
|
| 77 | - * @param mixed $user |
|
| 78 | - * |
|
| 79 | - * @return User |
|
| 80 | - * |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - public static function create($user): User |
|
| 84 | - { |
|
| 85 | - if ($user instanceof User) { |
|
| 86 | - return $user; |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Creates a user component |
|
| 76 | + * |
|
| 77 | + * @param mixed $user |
|
| 78 | + * |
|
| 79 | + * @return User |
|
| 80 | + * |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + public static function create($user): User |
|
| 84 | + { |
|
| 85 | + if ($user instanceof User) { |
|
| 86 | + return $user; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - return new User($user); |
|
| 90 | - } |
|
| 89 | + return new User($user); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getValue(): string |
|
| 98 | - { |
|
| 99 | - return $this->value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getValue(): string |
|
| 98 | + { |
|
| 99 | + return $this->value; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -31,71 +31,71 @@ |
||
| 31 | 31 | final class Password implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "password" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "password" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a password component |
|
| 76 | - * |
|
| 77 | - * @param mixed $password |
|
| 78 | - * |
|
| 79 | - * @return Password |
|
| 80 | - * |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - public static function create($password): Password |
|
| 84 | - { |
|
| 85 | - if ($password instanceof Password) { |
|
| 86 | - return $password; |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Creates a password component |
|
| 76 | + * |
|
| 77 | + * @param mixed $password |
|
| 78 | + * |
|
| 79 | + * @return Password |
|
| 80 | + * |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + public static function create($password): Password |
|
| 84 | + { |
|
| 85 | + if ($password instanceof Password) { |
|
| 86 | + return $password; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - return new Password($password); |
|
| 90 | - } |
|
| 89 | + return new Password($password); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getValue(): string |
|
| 98 | - { |
|
| 99 | - return $this->value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getValue(): string |
|
| 98 | + { |
|
| 99 | + return $this->value; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -32,56 +32,56 @@ |
||
| 32 | 32 | final class Host implements ComponentInterface |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Regular expression used for the component normalization |
|
| 37 | - * |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - // phpcs:ignore Generic.Files.LineLength |
|
| 41 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 35 | + /** |
|
| 36 | + * Regular expression used for the component normalization |
|
| 37 | + * |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + // phpcs:ignore Generic.Files.LineLength |
|
| 41 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * The component value |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - private string $value = ''; |
|
| 43 | + /** |
|
| 44 | + * The component value |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + private string $value = ''; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Constructor of the class |
|
| 52 | - * |
|
| 53 | - * @param mixed $value |
|
| 54 | - * |
|
| 55 | - * @throws InvalidArgumentException |
|
| 56 | - * If the component isn't valid. |
|
| 57 | - */ |
|
| 58 | - public function __construct($value) |
|
| 59 | - { |
|
| 60 | - if ($value === '') { |
|
| 61 | - return; |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * Constructor of the class |
|
| 52 | + * |
|
| 53 | + * @param mixed $value |
|
| 54 | + * |
|
| 55 | + * @throws InvalidArgumentException |
|
| 56 | + * If the component isn't valid. |
|
| 57 | + */ |
|
| 58 | + public function __construct($value) |
|
| 59 | + { |
|
| 60 | + if ($value === '') { |
|
| 61 | + return; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - if (!is_string($value)) { |
|
| 65 | - throw new InvalidArgumentException('URI component "host" must be a string'); |
|
| 66 | - } |
|
| 64 | + if (!is_string($value)) { |
|
| 65 | + throw new InvalidArgumentException('URI component "host" must be a string'); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 69 | - /** @var array{0: string, 1?: string} $match */ |
|
| 68 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 69 | + /** @var array{0: string, 1?: string} $match */ |
|
| 70 | 70 | |
| 71 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 72 | - }, $value); |
|
| 71 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 72 | + }, $value); |
|
| 73 | 73 | |
| 74 | - // the component is case-insensitive... |
|
| 75 | - $this->value = strtolower($this->value); |
|
| 76 | - } |
|
| 74 | + // the component is case-insensitive... |
|
| 75 | + $this->value = strtolower($this->value); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * {@inheritdoc} |
|
| 80 | - * |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function getValue(): string |
|
| 84 | - { |
|
| 85 | - return $this->value; |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * {@inheritdoc} |
|
| 80 | + * |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function getValue(): string |
|
| 84 | + { |
|
| 85 | + return $this->value; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -31,53 +31,53 @@ |
||
| 31 | 31 | final class Query implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x3f-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x3f-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "query" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "query" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * {@inheritdoc} |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getValue(): string |
|
| 80 | - { |
|
| 81 | - return $this->value; |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * {@inheritdoc} |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getValue(): string |
|
| 80 | + { |
|
| 81 | + return $this->value; |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -31,53 +31,53 @@ |
||
| 31 | 31 | final class Fragment implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x3f-\x5a\x5f\x61-\x7a\x7e])*|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x3f-\x5a\x5f\x61-\x7a\x7e])*|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "fragment" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "fragment" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, static function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * {@inheritdoc} |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getValue(): string |
|
| 80 | - { |
|
| 81 | - return $this->value; |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * {@inheritdoc} |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getValue(): string |
|
| 80 | + { |
|
| 81 | + return $this->value; |
|
| 82 | + } |
|
| 83 | 83 | } |