Skip to content

Commit

Permalink
Handle headers in AbstractMessage and remove some duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 27, 2012
1 parent 1ce0f4b commit 8d6d5da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 16 additions & 2 deletions lib/Buzz/Message/AbstractMessage.php
Expand Up @@ -95,7 +95,7 @@ public function toDomDocument()

public function setHeaders(array $headers)
{
$this->headers = $headers;
$this->headers = $this->flattenHeaders($headers);
}

public function addHeader($header)
Expand All @@ -105,7 +105,7 @@ public function addHeader($header)

public function addHeaders(array $headers)
{
$this->headers = array_merge($this->headers, $headers);
$this->headers = array_merge($this->headers, $this->flattenHeaders($headers));
}

public function getHeaders()
Expand Down Expand Up @@ -133,4 +133,18 @@ public function __toString()

return $string;
}

protected function flattenHeaders(array $headers)
{
$flattened = array();
foreach ($headers as $key => $header) {
if (is_int($key)) {
$flattened[] = $header;
} else {
$flattened[] = $key.': '.$header;
}
}

return $flattened;
}
}
8 changes: 2 additions & 6 deletions lib/Buzz/Message/Request.php
Expand Up @@ -29,12 +29,8 @@ public function setHeaders(array $headers)
{
parent::setHeaders(array());

foreach ($headers as $key => $header) {
if (is_int($key)) {
$this->addHeader($header);
} else {
$this->addHeader($key.': '.$header);
}
foreach ($this->flattenHeaders($headers) as $header) {
$this->addHeader($header);
}
}

Expand Down

0 comments on commit 8d6d5da

Please sign in to comment.