| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 6 | +use Dudlewebs\WPMCS\s3\Psr\Http\Message\MessageInterface; | |
| 5 | 7 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 8 | /** |
| 7 | 9 | * Trait implementing functionality common to requests and responses. |
| 8 | 10 | */ |
| @@ -7,21 +9,21 @@ | ||
| 7 | 9 | * Trait implementing functionality common to requests and responses. |
| 8 | 10 | */ |
| 9 | 11 | trait MessageTrait |
| 10 | 12 | { |
| 11 | - /** @var array Map of all registered headers, as original name => array of values */ | |
| 13 | + /** @var string[][] Map of all registered headers, as original name => array of values */ | |
| 12 | 14 | private $headers = []; |
| 13 | - /** @var array Map of lowercase header name => original name at registration */ | |
| 15 | + /** @var string[] Map of lowercase header name => original name at registration */ | |
| 14 | 16 | private $headerNames = []; |
| 15 | 17 | /** @var string */ |
| 16 | 18 | private $protocol = '1.1'; |
| 17 | 19 | /** @var StreamInterface|null */ |
| 18 | 20 | private $stream; |
| 19 | - public function getProtocolVersion() | |
| 21 | + public function getProtocolVersion() : string | |
| 20 | 22 | { |
| 21 | 23 | return $this->protocol; |
| 22 | 24 | } |
| 23 | - public function withProtocolVersion($version) | |
| 25 | + public function withProtocolVersion($version) : MessageInterface | |
| 24 | 26 | { |
| 25 | 27 | if ($this->protocol === $version) { |
| 26 | 28 | return $this; |
| 27 | 29 | } |
| @@ -28,17 +30,17 @@ | ||
| 28 | 30 | $new = clone $this; |
| 29 | 31 | $new->protocol = $version; |
| 30 | 32 | return $new; |
| 31 | 33 | } |
| 32 | - public function getHeaders() | |
| 34 | + public function getHeaders() : array | |
| 33 | 35 | { |
| 34 | 36 | return $this->headers; |
| 35 | 37 | } |
| 36 | - public function hasHeader($header) | |
| 38 | + public function hasHeader($header) : bool | |
| 37 | 39 | { |
| 38 | 40 | return isset($this->headerNames[\strtolower($header)]); |
| 39 | 41 | } |
| 40 | - public function getHeader($header) | |
| 42 | + public function getHeader($header) : array | |
| 41 | 43 | { |
| 42 | 44 | $header = \strtolower($header); |
| 43 | 45 | if (!isset($this->headerNames[$header])) { |
| 44 | 46 | return []; |
| @@ -45,13 +47,13 @@ | ||
| 45 | 47 | } |
| 46 | 48 | $header = $this->headerNames[$header]; |
| 47 | 49 | return $this->headers[$header]; |
| 48 | 50 | } |
| 49 | - public function getHeaderLine($header) | |
| 51 | + public function getHeaderLine($header) : string | |
| 50 | 52 | { |
| 51 | 53 | return \implode(', ', $this->getHeader($header)); |
| 52 | 54 | } |
| 53 | - public function withHeader($header, $value) | |
| 55 | + public function withHeader($header, $value) : MessageInterface | |
| 54 | 56 | { |
| 55 | 57 | $this->assertHeader($header); |
| 56 | 58 | $value = $this->normalizeHeaderValue($value); |
| 57 | 59 | $normalized = \strtolower($header); |
| @@ -62,9 +64,9 @@ | ||
| 62 | 64 | $new->headerNames[$normalized] = $header; |
| 63 | 65 | $new->headers[$header] = $value; |
| 64 | 66 | return $new; |
| 65 | 67 | } |
| 66 | - public function withAddedHeader($header, $value) | |
| 68 | + public function withAddedHeader($header, $value) : MessageInterface | |
| 67 | 69 | { |
| 68 | 70 | $this->assertHeader($header); |
| 69 | 71 | $value = $this->normalizeHeaderValue($value); |
| 70 | 72 | $normalized = \strtolower($header); |
| @@ -77,9 +79,9 @@ | ||
| 77 | 79 | $new->headers[$header] = $value; |
| 78 | 80 | } |
| 79 | 81 | return $new; |
| 80 | 82 | } |
| 81 | - public function withoutHeader($header) | |
| 83 | + public function withoutHeader($header) : MessageInterface | |
| 82 | 84 | { |
| 83 | 85 | $normalized = \strtolower($header); |
| 84 | 86 | if (!isset($this->headerNames[$normalized])) { |
| 85 | 87 | return $this; |
| @@ -88,9 +90,9 @@ | ||
| 88 | 90 | $new = clone $this; |
| 89 | 91 | unset($new->headers[$header], $new->headerNames[$normalized]); |
| 90 | 92 | return $new; |
| 91 | 93 | } |
| 92 | - public function getBody() | |
| 94 | + public function getBody() : StreamInterface | |
| 93 | 95 | { |
| 94 | 96 | if (!$this->stream) { |
| 95 | 97 | $this->stream = Utils::streamFor(''); |
| 96 | 98 | } |
| @@ -95,9 +97,9 @@ | ||
| 95 | 97 | $this->stream = Utils::streamFor(''); |
| 96 | 98 | } |
| 97 | 99 | return $this->stream; |
| 98 | 100 | } |
| 99 | - public function withBody(StreamInterface $body) | |
| 101 | + public function withBody(StreamInterface $body) : MessageInterface | |
| 100 | 102 | { |
| 101 | 103 | if ($body === $this->stream) { |
| 102 | 104 | return $this; |
| 103 | 105 | } |
| @@ -104,17 +106,17 @@ | ||
| 104 | 106 | $new = clone $this; |
| 105 | 107 | $new->stream = $body; |
| 106 | 108 | return $new; |
| 107 | 109 | } |
| 108 | - private function setHeaders(array $headers) | |
| 110 | + /** | |
| 111 | + * @param (string|string[])[] $headers | |
| 112 | + */ | |
| 113 | + private function setHeaders(array $headers) : void | |
| 109 | 114 | { |
| 110 | 115 | $this->headerNames = $this->headers = []; |
| 111 | 116 | foreach ($headers as $header => $value) { |
| 112 | - if (\is_int($header)) { | |
| 113 | - // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec | |
| 114 | - // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. | |
| 115 | - $header = (string) $header; | |
| 116 | - } | |
| 117 | + // Numeric array keys are converted to int by PHP. | |
| 118 | + $header = (string) $header; | |
| 117 | 119 | $this->assertHeader($header); |
| 118 | 120 | $value = $this->normalizeHeaderValue($value); |
| 119 | 121 | $normalized = \strtolower($header); |
| 120 | 122 | if (isset($this->headerNames[$normalized])) { |
| @@ -130,16 +132,13 @@ | ||
| 130 | 132 | * @param mixed $value |
| 131 | 133 | * |
| 132 | 134 | * @return string[] |
| 133 | 135 | */ |
| 134 | - private function normalizeHeaderValue($value) | |
| 136 | + private function normalizeHeaderValue($value) : array | |
| 135 | 137 | { |
| 136 | 138 | if (!\is_array($value)) { |
| 137 | 139 | return $this->trimAndValidateHeaderValues([$value]); |
| 138 | 140 | } |
| 139 | - if (\count($value) === 0) { | |
| 140 | - throw new \InvalidArgumentException('Header value can not be an empty array.'); | |
| 141 | - } | |
| 142 | 141 | return $this->trimAndValidateHeaderValues($value); |
| 143 | 142 | } |
| 144 | 143 | /** |
| 145 | 144 | * Trims whitespace from the header values. |
| @@ -152,11 +151,11 @@ | ||
| 152 | 151 | * @param mixed[] $values Header values |
| 153 | 152 | * |
| 154 | 153 | * @return string[] Trimmed header values |
| 155 | 154 | * |
| 156 | - * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 | |
| 155 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 | |
| 157 | 156 | */ |
| 158 | - private function trimAndValidateHeaderValues(array $values) | |
| 157 | + private function trimAndValidateHeaderValues(array $values) : array | |
| 159 | 158 | { |
| 160 | 159 | return \array_map(function ($value) { |
| 161 | 160 | if (!\is_scalar($value) && null !== $value) { |
| 162 | 161 | throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
| @@ -166,33 +165,24 @@ | ||
| 166 | 165 | return $trimmed; |
| 167 | 166 | }, \array_values($values)); |
| 168 | 167 | } |
| 169 | 168 | /** |
| 170 | - * @see https://tools.ietf.org/html/rfc7230#section-3.2 | |
| 169 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 | |
| 171 | 170 | * |
| 172 | 171 | * @param mixed $header |
| 173 | - * | |
| 174 | - * @return void | |
| 175 | 172 | */ |
| 176 | - private function assertHeader($header) | |
| 173 | + private function assertHeader($header) : void | |
| 177 | 174 | { |
| 178 | 175 | if (!\is_string($header)) { |
| 179 | 176 | throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header))); |
| 180 | 177 | } |
| 181 | - if ($header === '') { | |
| 182 | - throw new \InvalidArgumentException('Header name can not be empty.'); | |
| 178 | + if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { | |
| 179 | + throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name.', $header)); | |
| 183 | 180 | } |
| 184 | - if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) { | |
| 185 | - throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name', $header)); | |
| 186 | - } | |
| 187 | 181 | } |
| 188 | 182 | /** |
| 189 | - * @param string $value | |
| 183 | + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 | |
| 190 | 184 | * |
| 191 | - * @return void | |
| 192 | - * | |
| 193 | - * @see https://tools.ietf.org/html/rfc7230#section-3.2 | |
| 194 | - * | |
| 195 | 185 | * field-value = *( field-content / obs-fold ) |
| 196 | 186 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 197 | 187 | * field-vchar = VCHAR / obs-text |
| 198 | 188 | * VCHAR = %x21-7E |
| @@ -198,9 +188,9 @@ | ||
| 198 | 188 | * VCHAR = %x21-7E |
| 199 | 189 | * obs-text = %x80-FF |
| 200 | 190 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 201 | 191 | */ |
| 202 | - private function assertValue($value) | |
| 192 | + private function assertValue(string $value) : void | |
| 203 | 193 | { |
| 204 | 194 | // The regular expression intentionally does not support the obs-fold production, because as |
| 205 | 195 | // per RFC 7230#3.2.4: |
| 206 | 196 | // |
| @@ -211,9 +201,9 @@ | ||
| 211 | 201 | // |
| 212 | 202 | // Clients must not send a request with line folding and a server sending folded headers is |
| 213 | 203 | // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
| 214 | 204 | // folding is not likely to break any legitimate use case. |
| 215 | - if (!\preg_match('/^[\\x20\\x09\\x21-\\x7E\\x80-\\xFF]*$/', $value)) { | |
| 216 | - throw new \InvalidArgumentException(\sprintf('"%s" is not valid header value', $value)); | |
| 205 | + if (!\preg_match('/^[\\x20\\x09\\x21-\\x7E\\x80-\\xFF]*$/D', $value)) { | |
| 206 | + throw new \InvalidArgumentException(\sprintf('"%s" is not valid header value.', $value)); | |
| 217 | 207 | } |
| 218 | 208 | } |
| 219 | 209 | } |