| 1 |
<?php |
| 2 |
|
| 3 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 |
|
| 5 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 |
/** |
| 7 |
* Trait implementing functionality common to requests and responses. |
| 8 |
*/ |
| 9 |
trait MessageTrait |
| 10 |
{ |
| 11 |
/** @var array Map of all registered headers, as original name => array of values */ |
| 12 |
private $headers = []; |
| 13 |
/** @var array Map of lowercase header name => original name at registration */ |
| 14 |
private $headerNames = []; |
| 15 |
/** @var string */ |
| 16 |
private $protocol = '1.1'; |
| 17 |
/** @var StreamInterface|null */ |
| 18 |
private $stream; |
| 19 |
public function getProtocolVersion() |
| 20 |
{ |
| 21 |
return $this->protocol; |
| 22 |
} |
| 23 |
public function withProtocolVersion($version) |
| 24 |
{ |
| 25 |
if ($this->protocol === $version) { |
| 26 |
return $this; |
| 27 |
} |
| 28 |
$new = clone $this; |
| 29 |
$new->protocol = $version; |
| 30 |
return $new; |
| 31 |
} |
| 32 |
public function getHeaders() |
| 33 |
{ |
| 34 |
return $this->headers; |
| 35 |
} |
| 36 |
public function hasHeader($header) |
| 37 |
{ |
| 38 |
return isset($this->headerNames[\strtolower($header)]); |
| 39 |
} |
| 40 |
public function getHeader($header) |
| 41 |
{ |
| 42 |
$header = \strtolower($header); |
| 43 |
if (!isset($this->headerNames[$header])) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
$header = $this->headerNames[$header]; |
| 47 |
return $this->headers[$header]; |
| 48 |
} |
| 49 |
public function getHeaderLine($header) |
| 50 |
{ |
| 51 |
return \implode(', ', $this->getHeader($header)); |
| 52 |
} |
| 53 |
public function withHeader($header, $value) |
| 54 |
{ |
| 55 |
$this->assertHeader($header); |
| 56 |
$value = $this->normalizeHeaderValue($value); |
| 57 |
$normalized = \strtolower($header); |
| 58 |
$new = clone $this; |
| 59 |
if (isset($new->headerNames[$normalized])) { |
| 60 |
unset($new->headers[$new->headerNames[$normalized]]); |
| 61 |
} |
| 62 |
$new->headerNames[$normalized] = $header; |
| 63 |
$new->headers[$header] = $value; |
| 64 |
return $new; |
| 65 |
} |
| 66 |
public function withAddedHeader($header, $value) |
| 67 |
{ |
| 68 |
$this->assertHeader($header); |
| 69 |
$value = $this->normalizeHeaderValue($value); |
| 70 |
$normalized = \strtolower($header); |
| 71 |
$new = clone $this; |
| 72 |
if (isset($new->headerNames[$normalized])) { |
| 73 |
$header = $this->headerNames[$normalized]; |
| 74 |
$new->headers[$header] = \array_merge($this->headers[$header], $value); |
| 75 |
} else { |
| 76 |
$new->headerNames[$normalized] = $header; |
| 77 |
$new->headers[$header] = $value; |
| 78 |
} |
| 79 |
return $new; |
| 80 |
} |
| 81 |
public function withoutHeader($header) |
| 82 |
{ |
| 83 |
$normalized = \strtolower($header); |
| 84 |
if (!isset($this->headerNames[$normalized])) { |
| 85 |
return $this; |
| 86 |
} |
| 87 |
$header = $this->headerNames[$normalized]; |
| 88 |
$new = clone $this; |
| 89 |
unset($new->headers[$header], $new->headerNames[$normalized]); |
| 90 |
return $new; |
| 91 |
} |
| 92 |
public function getBody() |
| 93 |
{ |
| 94 |
if (!$this->stream) { |
| 95 |
$this->stream = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::streamFor(''); |
| 96 |
} |
| 97 |
return $this->stream; |
| 98 |
} |
| 99 |
public function withBody(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $body) |
| 100 |
{ |
| 101 |
if ($body === $this->stream) { |
| 102 |
return $this; |
| 103 |
} |
| 104 |
$new = clone $this; |
| 105 |
$new->stream = $body; |
| 106 |
return $new; |
| 107 |
} |
| 108 |
private function setHeaders(array $headers) |
| 109 |
{ |
| 110 |
$this->headerNames = $this->headers = []; |
| 111 |
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 |
$this->assertHeader($header); |
| 118 |
$value = $this->normalizeHeaderValue($value); |
| 119 |
$normalized = \strtolower($header); |
| 120 |
if (isset($this->headerNames[$normalized])) { |
| 121 |
$header = $this->headerNames[$normalized]; |
| 122 |
$this->headers[$header] = \array_merge($this->headers[$header], $value); |
| 123 |
} else { |
| 124 |
$this->headerNames[$normalized] = $header; |
| 125 |
$this->headers[$header] = $value; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
private function normalizeHeaderValue($value) |
| 130 |
{ |
| 131 |
if (!\is_array($value)) { |
| 132 |
return $this->trimHeaderValues([$value]); |
| 133 |
} |
| 134 |
if (\count($value) === 0) { |
| 135 |
throw new \InvalidArgumentException('Header value can not be an empty array.'); |
| 136 |
} |
| 137 |
return $this->trimHeaderValues($value); |
| 138 |
} |
| 139 |
/** |
| 140 |
* Trims whitespace from the header values. |
| 141 |
* |
| 142 |
* Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
| 143 |
* |
| 144 |
* header-field = field-name ":" OWS field-value OWS |
| 145 |
* OWS = *( SP / HTAB ) |
| 146 |
* |
| 147 |
* @param string[] $values Header values |
| 148 |
* |
| 149 |
* @return string[] Trimmed header values |
| 150 |
* |
| 151 |
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4 |
| 152 |
*/ |
| 153 |
private function trimHeaderValues(array $values) |
| 154 |
{ |
| 155 |
return \array_map(function ($value) { |
| 156 |
if (!\is_scalar($value) && null !== $value) { |
| 157 |
throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
| 158 |
} |
| 159 |
return \trim((string) $value, " \t"); |
| 160 |
}, \array_values($values)); |
| 161 |
} |
| 162 |
private function assertHeader($header) |
| 163 |
{ |
| 164 |
if (!\is_string($header)) { |
| 165 |
throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header))); |
| 166 |
} |
| 167 |
if ($header === '') { |
| 168 |
throw new \InvalidArgumentException('Header name can not be empty.'); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|