Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update header methods #51

Merged
merged 1 commit into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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