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