annotate vendor/zendframework/zend-diactoros/src/MessageTrait.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@12 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@12 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 6 */
Chris@0 7
Chris@0 8 namespace Zend\Diactoros;
Chris@0 9
Chris@0 10 use InvalidArgumentException;
Chris@0 11 use Psr\Http\Message\StreamInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Trait implementing the various methods defined in MessageInterface.
Chris@0 15 *
Chris@0 16 * @see https://github.com/php-fig/http-message/tree/master/src/MessageInterface.php
Chris@0 17 */
Chris@0 18 trait MessageTrait
Chris@0 19 {
Chris@0 20 /**
Chris@0 21 * List of all registered headers, as key => array of values.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 protected $headers = [];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Map of normalized header name to original name used to register header.
Chris@0 29 *
Chris@0 30 * @var array
Chris@0 31 */
Chris@0 32 protected $headerNames = [];
Chris@0 33
Chris@0 34 /**
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 private $protocol = '1.1';
Chris@0 38
Chris@0 39 /**
Chris@0 40 * @var StreamInterface
Chris@0 41 */
Chris@0 42 private $stream;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Retrieves the HTTP protocol version as a string.
Chris@0 46 *
Chris@0 47 * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
Chris@0 48 *
Chris@0 49 * @return string HTTP protocol version.
Chris@0 50 */
Chris@0 51 public function getProtocolVersion()
Chris@0 52 {
Chris@0 53 return $this->protocol;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Return an instance with the specified HTTP protocol version.
Chris@0 58 *
Chris@0 59 * The version string MUST contain only the HTTP version number (e.g.,
Chris@0 60 * "1.1", "1.0").
Chris@0 61 *
Chris@0 62 * This method MUST be implemented in such a way as to retain the
Chris@0 63 * immutability of the message, and MUST return an instance that has the
Chris@0 64 * new protocol version.
Chris@0 65 *
Chris@0 66 * @param string $version HTTP protocol version
Chris@0 67 * @return static
Chris@0 68 */
Chris@0 69 public function withProtocolVersion($version)
Chris@0 70 {
Chris@0 71 $this->validateProtocolVersion($version);
Chris@0 72 $new = clone $this;
Chris@0 73 $new->protocol = $version;
Chris@0 74 return $new;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Retrieves all message headers.
Chris@0 79 *
Chris@0 80 * The keys represent the header name as it will be sent over the wire, and
Chris@0 81 * each value is an array of strings associated with the header.
Chris@0 82 *
Chris@0 83 * // Represent the headers as a string
Chris@0 84 * foreach ($message->getHeaders() as $name => $values) {
Chris@0 85 * echo $name . ": " . implode(", ", $values);
Chris@0 86 * }
Chris@0 87 *
Chris@0 88 * // Emit headers iteratively:
Chris@0 89 * foreach ($message->getHeaders() as $name => $values) {
Chris@0 90 * foreach ($values as $value) {
Chris@0 91 * header(sprintf('%s: %s', $name, $value), false);
Chris@0 92 * }
Chris@0 93 * }
Chris@0 94 *
Chris@0 95 * @return array Returns an associative array of the message's headers. Each
Chris@0 96 * key MUST be a header name, and each value MUST be an array of strings.
Chris@0 97 */
Chris@0 98 public function getHeaders()
Chris@0 99 {
Chris@0 100 return $this->headers;
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Checks if a header exists by the given case-insensitive name.
Chris@0 105 *
Chris@0 106 * @param string $header Case-insensitive header name.
Chris@0 107 * @return bool Returns true if any header names match the given header
Chris@0 108 * name using a case-insensitive string comparison. Returns false if
Chris@0 109 * no matching header name is found in the message.
Chris@0 110 */
Chris@0 111 public function hasHeader($header)
Chris@0 112 {
Chris@0 113 return isset($this->headerNames[strtolower($header)]);
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Retrieves a message header value by the given case-insensitive name.
Chris@0 118 *
Chris@0 119 * This method returns an array of all the header values of the given
Chris@0 120 * case-insensitive header name.
Chris@0 121 *
Chris@0 122 * If the header does not appear in the message, this method MUST return an
Chris@0 123 * empty array.
Chris@0 124 *
Chris@0 125 * @param string $header Case-insensitive header field name.
Chris@0 126 * @return string[] An array of string values as provided for the given
Chris@0 127 * header. If the header does not appear in the message, this method MUST
Chris@0 128 * return an empty array.
Chris@0 129 */
Chris@0 130 public function getHeader($header)
Chris@0 131 {
Chris@0 132 if (! $this->hasHeader($header)) {
Chris@0 133 return [];
Chris@0 134 }
Chris@0 135
Chris@0 136 $header = $this->headerNames[strtolower($header)];
Chris@0 137
Chris@0 138 return $this->headers[$header];
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Retrieves a comma-separated string of the values for a single header.
Chris@0 143 *
Chris@0 144 * This method returns all of the header values of the given
Chris@0 145 * case-insensitive header name as a string concatenated together using
Chris@0 146 * a comma.
Chris@0 147 *
Chris@0 148 * NOTE: Not all header values may be appropriately represented using
Chris@0 149 * comma concatenation. For such headers, use getHeader() instead
Chris@0 150 * and supply your own delimiter when concatenating.
Chris@0 151 *
Chris@0 152 * If the header does not appear in the message, this method MUST return
Chris@0 153 * an empty string.
Chris@0 154 *
Chris@0 155 * @param string $name Case-insensitive header field name.
Chris@0 156 * @return string A string of values as provided for the given header
Chris@0 157 * concatenated together using a comma. If the header does not appear in
Chris@0 158 * the message, this method MUST return an empty string.
Chris@0 159 */
Chris@0 160 public function getHeaderLine($name)
Chris@0 161 {
Chris@0 162 $value = $this->getHeader($name);
Chris@0 163 if (empty($value)) {
Chris@0 164 return '';
Chris@0 165 }
Chris@0 166
Chris@0 167 return implode(',', $value);
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Return an instance with the provided header, replacing any existing
Chris@0 172 * values of any headers with the same case-insensitive name.
Chris@0 173 *
Chris@0 174 * While header names are case-insensitive, the casing of the header will
Chris@0 175 * be preserved by this function, and returned from getHeaders().
Chris@0 176 *
Chris@0 177 * This method MUST be implemented in such a way as to retain the
Chris@0 178 * immutability of the message, and MUST return an instance that has the
Chris@0 179 * new and/or updated header and value.
Chris@0 180 *
Chris@0 181 * @param string $header Case-insensitive header field name.
Chris@0 182 * @param string|string[] $value Header value(s).
Chris@0 183 * @return static
Chris@0 184 * @throws \InvalidArgumentException for invalid header names or values.
Chris@0 185 */
Chris@0 186 public function withHeader($header, $value)
Chris@0 187 {
Chris@0 188 $this->assertHeader($header);
Chris@0 189
Chris@0 190 $normalized = strtolower($header);
Chris@0 191
Chris@0 192 $new = clone $this;
Chris@0 193 if ($new->hasHeader($header)) {
Chris@0 194 unset($new->headers[$new->headerNames[$normalized]]);
Chris@0 195 }
Chris@0 196
Chris@0 197 $value = $this->filterHeaderValue($value);
Chris@0 198
Chris@0 199 $new->headerNames[$normalized] = $header;
Chris@0 200 $new->headers[$header] = $value;
Chris@0 201
Chris@0 202 return $new;
Chris@0 203 }
Chris@0 204
Chris@0 205 /**
Chris@0 206 * Return an instance with the specified header appended with the
Chris@0 207 * given value.
Chris@0 208 *
Chris@0 209 * Existing values for the specified header will be maintained. The new
Chris@0 210 * value(s) will be appended to the existing list. If the header did not
Chris@0 211 * exist previously, it will be added.
Chris@0 212 *
Chris@0 213 * This method MUST be implemented in such a way as to retain the
Chris@0 214 * immutability of the message, and MUST return an instance that has the
Chris@0 215 * new header and/or value.
Chris@0 216 *
Chris@0 217 * @param string $header Case-insensitive header field name to add.
Chris@0 218 * @param string|string[] $value Header value(s).
Chris@0 219 * @return static
Chris@0 220 * @throws \InvalidArgumentException for invalid header names or values.
Chris@0 221 */
Chris@0 222 public function withAddedHeader($header, $value)
Chris@0 223 {
Chris@0 224 $this->assertHeader($header);
Chris@0 225
Chris@0 226 if (! $this->hasHeader($header)) {
Chris@0 227 return $this->withHeader($header, $value);
Chris@0 228 }
Chris@0 229
Chris@0 230 $header = $this->headerNames[strtolower($header)];
Chris@0 231
Chris@0 232 $new = clone $this;
Chris@0 233 $value = $this->filterHeaderValue($value);
Chris@0 234 $new->headers[$header] = array_merge($this->headers[$header], $value);
Chris@0 235 return $new;
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Return an instance without the specified header.
Chris@0 240 *
Chris@0 241 * Header resolution MUST be done without case-sensitivity.
Chris@0 242 *
Chris@0 243 * This method MUST be implemented in such a way as to retain the
Chris@0 244 * immutability of the message, and MUST return an instance that removes
Chris@0 245 * the named header.
Chris@0 246 *
Chris@0 247 * @param string $header Case-insensitive header field name to remove.
Chris@0 248 * @return static
Chris@0 249 */
Chris@0 250 public function withoutHeader($header)
Chris@0 251 {
Chris@0 252 if (! $this->hasHeader($header)) {
Chris@0 253 return clone $this;
Chris@0 254 }
Chris@0 255
Chris@0 256 $normalized = strtolower($header);
Chris@0 257 $original = $this->headerNames[$normalized];
Chris@0 258
Chris@0 259 $new = clone $this;
Chris@0 260 unset($new->headers[$original], $new->headerNames[$normalized]);
Chris@0 261 return $new;
Chris@0 262 }
Chris@0 263
Chris@0 264 /**
Chris@0 265 * Gets the body of the message.
Chris@0 266 *
Chris@0 267 * @return StreamInterface Returns the body as a stream.
Chris@0 268 */
Chris@0 269 public function getBody()
Chris@0 270 {
Chris@0 271 return $this->stream;
Chris@0 272 }
Chris@0 273
Chris@0 274 /**
Chris@0 275 * Return an instance with the specified message body.
Chris@0 276 *
Chris@0 277 * The body MUST be a StreamInterface object.
Chris@0 278 *
Chris@0 279 * This method MUST be implemented in such a way as to retain the
Chris@0 280 * immutability of the message, and MUST return a new instance that has the
Chris@0 281 * new body stream.
Chris@0 282 *
Chris@0 283 * @param StreamInterface $body Body.
Chris@0 284 * @return static
Chris@0 285 * @throws \InvalidArgumentException When the body is not valid.
Chris@0 286 */
Chris@0 287 public function withBody(StreamInterface $body)
Chris@0 288 {
Chris@0 289 $new = clone $this;
Chris@0 290 $new->stream = $body;
Chris@0 291 return $new;
Chris@0 292 }
Chris@0 293
Chris@0 294 private function getStream($stream, $modeIfNotInstance)
Chris@0 295 {
Chris@0 296 if ($stream instanceof StreamInterface) {
Chris@0 297 return $stream;
Chris@0 298 }
Chris@0 299
Chris@0 300 if (! is_string($stream) && ! is_resource($stream)) {
Chris@0 301 throw new InvalidArgumentException(
Chris@0 302 'Stream must be a string stream resource identifier, '
Chris@0 303 . 'an actual stream resource, '
Chris@0 304 . 'or a Psr\Http\Message\StreamInterface implementation'
Chris@0 305 );
Chris@0 306 }
Chris@0 307
Chris@0 308 return new Stream($stream, $modeIfNotInstance);
Chris@0 309 }
Chris@0 310
Chris@0 311 /**
Chris@0 312 * Filter a set of headers to ensure they are in the correct internal format.
Chris@0 313 *
Chris@0 314 * Used by message constructors to allow setting all initial headers at once.
Chris@0 315 *
Chris@0 316 * @param array $originalHeaders Headers to filter.
Chris@0 317 */
Chris@0 318 private function setHeaders(array $originalHeaders)
Chris@0 319 {
Chris@0 320 $headerNames = $headers = [];
Chris@0 321
Chris@0 322 foreach ($originalHeaders as $header => $value) {
Chris@0 323 $value = $this->filterHeaderValue($value);
Chris@0 324
Chris@0 325 $this->assertHeader($header);
Chris@0 326
Chris@0 327 $headerNames[strtolower($header)] = $header;
Chris@0 328 $headers[$header] = $value;
Chris@0 329 }
Chris@0 330
Chris@0 331 $this->headerNames = $headerNames;
Chris@0 332 $this->headers = $headers;
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * Validate the HTTP protocol version
Chris@0 337 *
Chris@0 338 * @param string $version
Chris@0 339 * @throws InvalidArgumentException on invalid HTTP protocol version
Chris@0 340 */
Chris@0 341 private function validateProtocolVersion($version)
Chris@0 342 {
Chris@0 343 if (empty($version)) {
Chris@12 344 throw new InvalidArgumentException(
Chris@0 345 'HTTP protocol version can not be empty'
Chris@12 346 );
Chris@0 347 }
Chris@0 348 if (! is_string($version)) {
Chris@0 349 throw new InvalidArgumentException(sprintf(
Chris@0 350 'Unsupported HTTP protocol version; must be a string, received %s',
Chris@0 351 (is_object($version) ? get_class($version) : gettype($version))
Chris@0 352 ));
Chris@0 353 }
Chris@0 354
Chris@0 355 // HTTP/1 uses a "<major>.<minor>" numbering scheme to indicate
Chris@0 356 // versions of the protocol, while HTTP/2 does not.
Chris@0 357 if (! preg_match('#^(1\.[01]|2)$#', $version)) {
Chris@0 358 throw new InvalidArgumentException(sprintf(
Chris@0 359 'Unsupported HTTP protocol version "%s" provided',
Chris@0 360 $version
Chris@0 361 ));
Chris@0 362 }
Chris@0 363 }
Chris@0 364
Chris@0 365 /**
Chris@0 366 * @param mixed $values
Chris@0 367 * @return string[]
Chris@0 368 */
Chris@0 369 private function filterHeaderValue($values)
Chris@0 370 {
Chris@0 371 if (! is_array($values)) {
Chris@0 372 $values = [$values];
Chris@0 373 }
Chris@0 374
Chris@0 375 return array_map(function ($value) {
Chris@0 376 HeaderSecurity::assertValid($value);
Chris@0 377
Chris@0 378 return (string) $value;
Chris@0 379 }, $values);
Chris@0 380 }
Chris@0 381
Chris@0 382 /**
Chris@0 383 * Ensure header name and values are valid.
Chris@0 384 *
Chris@0 385 * @param string $name
Chris@0 386 *
Chris@0 387 * @throws InvalidArgumentException
Chris@0 388 */
Chris@0 389 private function assertHeader($name)
Chris@0 390 {
Chris@0 391 HeaderSecurity::assertValidName($name);
Chris@0 392 }
Chris@0 393 }