diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a9ab25..59bbb31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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() diff --git a/src/Api/Protocol/Http/HttpRequest.php b/src/Api/Protocol/Http/HttpRequest.php index bfb0187..ce6ec1e 100644 --- a/src/Api/Protocol/Http/HttpRequest.php +++ b/src/Api/Protocol/Http/HttpRequest.php @@ -54,6 +54,11 @@ class HttpRequest */ private $headers = []; + /** + * @var array + */ + private $headerKeys = []; + /** * @var string */ @@ -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; } @@ -258,9 +267,9 @@ 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)]); } /** @@ -268,17 +277,50 @@ public function hasHeader($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) + ); } /** diff --git a/src/Api/TransportMeta.php b/src/Api/TransportMeta.php index fe2cdfc..dad8ba3 100644 --- a/src/Api/TransportMeta.php +++ b/src/Api/TransportMeta.php @@ -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; } /**