Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Ensure that adding a header overwrites when not a multi-value header
Browse files Browse the repository at this point in the history
If the header is not a multi-value header, but a header of that type was
previously injected, we should overwrite it.
  • Loading branch information
weierophinney committed Apr 26, 2018
1 parent 0261976 commit 4434d7d
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,27 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null)
*/
public function addHeader(Header\HeaderInterface $header)
{
$this->headersKeys[] = static::createKey($header->getFieldName());
$this->headers[] = $header;
$key = static::createKey($header->getFieldName());
$index = array_search($key, $this->headersKeys);

// No header by that key presently; append key and header to list.
if ($index === false) {
$this->headersKeys[] = $key;
$this->headers[] = $header;
return $this;
}

// Header exists, and is a multi-value header; append key and header to
// list (as multi-value headers are aggregated on retrieval)
$class = ($this->getPluginClassLoader()->load(str_replace('-', '', $key))) ?: Header\GenericHeader::class;
if (in_array(Header\MultipleHeaderInterface::class, class_implements($class, true))) {
$this->headersKeys[] = $key;
$this->headers[] = $header;
return $this;
}

// Otherwise, we replace the current instance.
$this->headers[$index] = $header;

return $this;
}
Expand Down Expand Up @@ -286,6 +305,7 @@ public function get($name)
if (is_array($this->headers[$index])) {
return $this->lazyLoadHeader($index);
}

return $this->headers[$index];
}

Expand Down

0 comments on commit 4434d7d

Please sign in to comment.