You are here

public function MessageTrait::withHeader in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-diactoros/src/MessageTrait.php \Zend\Diactoros\MessageTrait::withHeader()
  2. 8 vendor/guzzlehttp/psr7/src/MessageTrait.php \GuzzleHttp\Psr7\MessageTrait::withHeader()
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/MessageTrait.php \Zend\Diactoros\MessageTrait::withHeader()

Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.

While header names are case-insensitive, the casing of the header will be preserved by this function, and returned from getHeaders().

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new and/or updated header and value.

Parameters

string $header Case-insensitive header field name.:

string|string[] $value Header value(s).:

Return value

static

Throws

\InvalidArgumentException for invalid header names or values.

1 call to MessageTrait::withHeader()
MessageTrait::withAddedHeader in vendor/zendframework/zend-diactoros/src/MessageTrait.php
Return an instance with the specified header appended with the given value.

File

vendor/zendframework/zend-diactoros/src/MessageTrait.php, line 189

Class

MessageTrait
Trait implementing the various methods defined in MessageInterface.

Namespace

Zend\Diactoros

Code

public function withHeader($header, $value) {
  if (is_string($value)) {
    $value = [
      $value,
    ];
  }
  if (!is_array($value) || !$this
    ->arrayContainsOnlyStrings($value)) {
    throw new InvalidArgumentException('Invalid header value; must be a string or array of strings');
  }
  HeaderSecurity::assertValidName($header);
  self::assertValidHeaderValue($value);
  $normalized = strtolower($header);
  $new = clone $this;
  $new->headerNames[$normalized] = $header;
  $new->headers[$header] = $value;
  return $new;
}