Skip to content

Commit

Permalink
Merge pull request #51 from fcastilloes/header-methods
Browse files Browse the repository at this point in the history
Update header methods
  • Loading branch information
fcastilloes committed Jul 13, 2017
2 parents 05f9232 + d0ff21f commit 5ceb806
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Upcoming
## Added
- New header methods

## [1.1.7] - 2017-07-12
## Fixed
- Fix default property in getProperty()
Expand Down
56 changes: 49 additions & 7 deletions src/Api/Protocol/Http/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class HttpRequest
*/
private $headers = [];

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

/**
* @var string
*/
Expand Down Expand Up @@ -89,7 +94,11 @@ public function __construct(
$this->url = $url;
$this->query = $query;
$this->postData = $postData;
$this->headers = $headers;
$this->headers = array_combine(
array_map('strtoupper', array_keys($headers)),
array_values($headers)
);
$this->headerKeys = array_keys($headers);
$this->body = $body;
$this->files = $files;
}
Expand Down Expand Up @@ -258,27 +267,60 @@ public function getProtocolVersion()
* @param string $name
* @return bool
*/
public function hasHeader($name)
public function hasHeader(string $name): bool
{
return isset($this->headers[$name]);
return isset($this->headers[strtoupper($name)]);
}

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

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

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

/**
* @return array
*/
public function getHeaders(): array
{
return $this->hasHeader($name)? $this->headers[$name] : $default;
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 Down
2 changes: 1 addition & 1 deletion src/Api/TransportMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function getLevel()
*/
public function getProperty(string $name, string $default = ''): string
{
return $this->properties[$name] ?: $default;
return $this->properties[$name] ?? $default;
}

/**
Expand Down

0 comments on commit 5ceb806

Please sign in to comment.