| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
use Psr\Http\Message\MessageInterface; |
| 8 |
use Psr\Http\Message\StreamInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
trait MessageTrait |
| 14 |
{ |
| 15 |
|
| 16 |
private $headers = []; |
| 17 |
|
| 18 |
|
| 19 |
private $headerNames = []; |
| 20 |
|
| 21 |
|
| 22 |
private $protocol = '1.1'; |
| 23 |
|
| 24 |
|
| 25 |
private $stream; |
| 26 |
|
| 27 |
public function getProtocolVersion(): string |
| 28 |
{ |
| 29 |
return $this->protocol; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function withProtocolVersion($version): MessageInterface |
| 36 |
{ |
| 37 |
if (!\is_string($version)) { |
| 38 |
\trigger_deprecation( |
| 39 |
'guzzlehttp/psr7', |
| 40 |
'2.11', |
| 41 |
'Passing %s to MessageInterface::withProtocolVersion() is deprecated; guzzlehttp/psr7 3.0 requires string.', |
| 42 |
\get_debug_type($version) |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
if ($this->protocol === $version) { |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
$new = clone $this; |
| 51 |
$new->protocol = $version; |
| 52 |
|
| 53 |
return $new; |
| 54 |
} |
| 55 |
|
| 56 |
public function getHeaders(): array |
| 57 |
{ |
| 58 |
return $this->headers; |
| 59 |
} |
| 60 |
|
| 61 |
public function hasHeader($header): bool |
| 62 |
{ |
| 63 |
return isset($this->headerNames[strtolower($header)]); |
| 64 |
} |
| 65 |
|
| 66 |
public function getHeader($header): array |
| 67 |
{ |
| 68 |
$header = strtolower($header); |
| 69 |
|
| 70 |
if (!isset($this->headerNames[$header])) { |
| 71 |
return []; |
| 72 |
} |
| 73 |
|
| 74 |
$header = $this->headerNames[$header]; |
| 75 |
|
| 76 |
return $this->headers[$header]; |
| 77 |
} |
| 78 |
|
| 79 |
public function getHeaderLine($header): string |
| 80 |
{ |
| 81 |
return implode(', ', $this->getHeader($header)); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function withHeader($header, $value): MessageInterface |
| 88 |
{ |
| 89 |
$this->assertHeader($header); |
| 90 |
$values = \is_array($value) ? $value : [$value]; |
| 91 |
foreach ($values as $item) { |
| 92 |
if (!\is_string($item) && (\is_scalar($item) || $item === null)) { |
| 93 |
\trigger_deprecation( |
| 94 |
'guzzlehttp/psr7', |
| 95 |
'2.11', |
| 96 |
'Passing %s to MessageInterface::withHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', |
| 97 |
\get_debug_type($item) |
| 98 |
); |
| 99 |
|
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
$value = $this->normalizeHeaderValue($value); |
| 104 |
$normalized = strtolower($header); |
| 105 |
|
| 106 |
$new = clone $this; |
| 107 |
if (isset($new->headerNames[$normalized])) { |
| 108 |
unset($new->headers[$new->headerNames[$normalized]]); |
| 109 |
} |
| 110 |
$new->headerNames[$normalized] = $header; |
| 111 |
$new->headers[$header] = $value; |
| 112 |
|
| 113 |
return $new; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function withAddedHeader($header, $value): MessageInterface |
| 120 |
{ |
| 121 |
$this->assertHeader($header); |
| 122 |
$values = \is_array($value) ? $value : [$value]; |
| 123 |
foreach ($values as $item) { |
| 124 |
if (!\is_string($item) && (\is_scalar($item) || $item === null)) { |
| 125 |
\trigger_deprecation( |
| 126 |
'guzzlehttp/psr7', |
| 127 |
'2.11', |
| 128 |
'Passing %s to MessageInterface::withAddedHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', |
| 129 |
\get_debug_type($item) |
| 130 |
); |
| 131 |
|
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
$value = $this->normalizeHeaderValue($value); |
| 136 |
$normalized = strtolower($header); |
| 137 |
|
| 138 |
$new = clone $this; |
| 139 |
if (isset($new->headerNames[$normalized])) { |
| 140 |
$header = $this->headerNames[$normalized]; |
| 141 |
$new->headers[$header] = array_merge($this->headers[$header], $value); |
| 142 |
} else { |
| 143 |
$new->headerNames[$normalized] = $header; |
| 144 |
$new->headers[$header] = $value; |
| 145 |
} |
| 146 |
|
| 147 |
return $new; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function withoutHeader($header): MessageInterface |
| 154 |
{ |
| 155 |
$normalized = strtolower($header); |
| 156 |
|
| 157 |
if (!isset($this->headerNames[$normalized])) { |
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
$header = $this->headerNames[$normalized]; |
| 162 |
|
| 163 |
$new = clone $this; |
| 164 |
unset($new->headers[$header], $new->headerNames[$normalized]); |
| 165 |
|
| 166 |
return $new; |
| 167 |
} |
| 168 |
|
| 169 |
public function getBody(): StreamInterface |
| 170 |
{ |
| 171 |
if (!$this->stream) { |
| 172 |
$this->stream = Utils::streamFor(''); |
| 173 |
} |
| 174 |
|
| 175 |
return $this->stream; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function withBody(StreamInterface $body): MessageInterface |
| 182 |
{ |
| 183 |
if ($body === $this->stream) { |
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
$new = clone $this; |
| 188 |
$new->stream = $body; |
| 189 |
|
| 190 |
return $new; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private function setHeaders(array $headers): void |
| 197 |
{ |
| 198 |
$this->headerNames = $this->headers = []; |
| 199 |
foreach ($headers as $header => $value) { |
| 200 |
|
| 201 |
$header = (string) $header; |
| 202 |
|
| 203 |
$this->assertHeader($header); |
| 204 |
$values = \is_array($value) ? $value : [$value]; |
| 205 |
foreach ($values as $item) { |
| 206 |
if (!\is_string($item) && (\is_scalar($item) || $item === null)) { |
| 207 |
\trigger_deprecation( |
| 208 |
'guzzlehttp/psr7', |
| 209 |
'2.11', |
| 210 |
'Passing %s to %s::__construct() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', |
| 211 |
\get_debug_type($item), |
| 212 |
static::class |
| 213 |
); |
| 214 |
|
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
$value = $this->normalizeHeaderValue($value); |
| 219 |
$normalized = strtolower($header); |
| 220 |
if (isset($this->headerNames[$normalized])) { |
| 221 |
$header = $this->headerNames[$normalized]; |
| 222 |
$this->headers[$header] = array_merge($this->headers[$header], $value); |
| 223 |
} else { |
| 224 |
$this->headerNames[$normalized] = $header; |
| 225 |
$this->headers[$header] = $value; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private function normalizeHeaderValue($value): array |
| 236 |
{ |
| 237 |
if (is_array($value) && $value === []) { |
| 238 |
\trigger_deprecation( |
| 239 |
'guzzlehttp/psr7', |
| 240 |
'2.11', |
| 241 |
'Passing an empty array as a header value is deprecated; guzzlehttp/psr7 3.0 rejects empty header value arrays.' |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
if (!is_array($value)) { |
| 246 |
return $this->trimAndValidateHeaderValues([$value]); |
| 247 |
} |
| 248 |
|
| 249 |
return $this->trimAndValidateHeaderValues($value); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private function trimAndValidateHeaderValues(array $values): array |
| 267 |
{ |
| 268 |
return array_map(function ($value) { |
| 269 |
if (!is_scalar($value) && null !== $value) { |
| 270 |
throw new \InvalidArgumentException(sprintf( |
| 271 |
'Header value must be scalar or null but %s provided.', |
| 272 |
is_object($value) ? get_class($value) : gettype($value) |
| 273 |
)); |
| 274 |
} |
| 275 |
|
| 276 |
$trimmed = trim((string) $value, " \t"); |
| 277 |
$this->assertValue($trimmed); |
| 278 |
|
| 279 |
return $trimmed; |
| 280 |
}, array_values($values)); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private function assertHeader($header): void |
| 289 |
{ |
| 290 |
if (!is_string($header)) { |
| 291 |
throw new \InvalidArgumentException(sprintf( |
| 292 |
'Header name must be a string but %s provided.', |
| 293 |
is_object($header) ? get_class($header) : gettype($header) |
| 294 |
)); |
| 295 |
} |
| 296 |
|
| 297 |
if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
| 298 |
throw new \InvalidArgumentException( |
| 299 |
sprintf('"%s" is not valid header name.', $header) |
| 300 |
); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
private function assertValue(string $value): void |
| 315 |
{ |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) { |
| 328 |
throw new \InvalidArgumentException( |
| 329 |
sprintf('"%s" is not valid header value.', $value) |
| 330 |
); |
| 331 |
} |
| 332 |
} |
| 333 |
} |