You are here

public function MessageTrait::withAddedHeader in Zircon Profile 8

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

Return an instance with the specified header appended with the given value.

Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added.

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 header and/or value.

Parameters

string $header Case-insensitive header field name to add.:

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

Return value

static

Throws

\InvalidArgumentException for invalid header names or values.

File

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

Class

MessageTrait
Trait implementing the various methods defined in MessageInterface.

Namespace

Zend\Diactoros

Code

public function withAddedHeader($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);
  if (!$this
    ->hasHeader($header)) {
    return $this
      ->withHeader($header, $value);
  }
  $normalized = strtolower($header);
  $header = $this->headerNames[$normalized];
  $new = clone $this;
  $new->headers[$header] = array_merge($this->headers[$header], $value);
  return $new;
}