Skip to content

Commit

Permalink
Merge pull request #62 from fcastilloes/master
Browse files Browse the repository at this point in the history
Treat headers as an array of values on response
  • Loading branch information
fcastilloes committed Sep 4, 2017
2 parents 3675996 + 704d863 commit fc42556
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## Fixed
- Treat headers as an array of values on response

## [1.2.0] - 2017-09-01
## Added
Expand Down
57 changes: 50 additions & 7 deletions src/Api/Protocol/Http/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class HttpResponse
*/
private $headers = [];

/**
* @var array
*/
private $headerKeys = [];

/**
* @param string $version
* @param HttpStatus $status
Expand All @@ -57,7 +62,11 @@ public function __construct(
$this->version = $version;
$this->status = $status;
$this->body = $body;
$this->headers = $headers;
$this->headers = array_combine(
array_map('strtoupper', array_keys($headers)),
array_values($headers)
);
$this->headerKeys = array_keys($headers);
}

/**
Expand Down Expand Up @@ -137,26 +146,60 @@ public function setStatus($code, $text)
* @param string $header
* @return bool
*/
public function hasHeader($header)
public function hasHeader($header): bool
{
return isset($this->headers[$header]);
}

/**
* @param string $header
* @param string $default
* @return string
*/
public function getHeader($header)
public function getHeader($header, string $default = ''): string
{
return $this->headers[$header][0] ?? $default;
}

/**
* @param string $header
* @param array $default
* @return array
*/
public function getHeaderArray($header, array $default = []): array
{
return $this->headers[$header] ?? $default;
}

/**
* @param array $arr
* @return mixed
*/
private function getFirst(array $arr)
{
return $arr[0];
}

/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers[$header];
return array_combine(
$this->headerKeys,
array_values(array_map([$this, 'getFirst'], $this->headers))
);
}

/**
* @return array
*/
public function getHeaders()
public function getHeadersArray(): array
{
return $this->headers;
return array_combine(
$this->headerKeys,
array_values($this->headers)
);
}

/**
Expand All @@ -166,7 +209,7 @@ public function getHeaders()
*/
public function setHeader($header, $value)
{
$this->headers[$header] = $value;
$this->headers[$header][] = $value;

return $this;
}
Expand Down

0 comments on commit fc42556

Please sign in to comment.