From 568e018c0e24381d9eba79dfbe3f35b21aaae377 Mon Sep 17 00:00:00 2001 From: mepihindeveloper Date: Thu, 4 Mar 2021 23:39:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Headers.php | 126 ++++++++++++++++++++++++++++++++ interfaces/HeadersInterface.php | 71 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 Headers.php create mode 100644 interfaces/HeadersInterface.php diff --git a/Headers.php b/Headers.php new file mode 100644 index 0000000..19172df --- /dev/null +++ b/Headers.php @@ -0,0 +1,126 @@ +headers = $this->getAllHeaders(); + } + + /** + * Получает все заголовки методами apache и nginx + * + * @return array + */ + private function getAllHeaders(): array + { + if (!function_exists('getallheaders')) + { + if (!is_array($_SERVER)) + { + return []; + } + + $headers = []; + + foreach ($_SERVER as $name => $value) + { + if (strpos($name, 'HTTP_') === 0) + { + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; + } + } + + return $headers; + } + + return getallheaders() !== false ? getallheaders() : []; + } + + /** + * @inheritDoc + */ + public function set(array $params): void { + $this->getAll(); + + foreach ($params as $header => $value) + { + $this->headers[$header] = $value; + } + + $this->add($params); + } + + /** + * @inheritDoc + */ + public function add(array $params): void { + foreach ($params as $header => $value) + { + $headerExists = array_key_exists($header, $this->headers); + $this->headers[$header] = $value; + + header("{$header}: {$value}", $headerExists); + } + } + + /** + * @inheritDoc + */ + public function remove(string $key): void { + $this->getAll(); + + unset($this->headers[$key]); + header_remove($key); + } + + /** + * @inheritDoc + */ + public function removeAll(): void { + $this->headers = []; + header_remove(); + } + + /** + * @inheritDoc + */ + public function has(string $key): bool { + $this->getAll(); + + return isset($this->headers[$key]); + } + + /** + * @inheritDoc + */ + public function get(string $key): string { + if (!$this->has($key)) + { + throw new InvalidArgumentException("Заголоков {$key} отсутсвует."); + } + + return $this->headers[$key]; + } + + /** + * @inheritDoc + */ + public function getAll(): array { + $this->headers = !empty($this->headers) ? $this->headers : $this->getAllHeaders(); + + return $this->headers; + } +} \ No newline at end of file diff --git a/interfaces/HeadersInterface.php b/interfaces/HeadersInterface.php new file mode 100644 index 0000000..52e259b --- /dev/null +++ b/interfaces/HeadersInterface.php @@ -0,0 +1,71 @@ + value] + * + * @return void + */ + public function set(array $params): void; + + /** + * Добавляет заголовок. Если заголовок уже существует, то он будет перезаписан. + * + * @param array $params Заголовки [key => value] + * + * @return void + */ + public function add(array $params): void; + + /** + * Удаляет заголовок + * + * @param string $key Заголовок + * + * @return void + */ + public function remove(string $key): void; + + /** + * Удаляет все заголовки + * + * @return void + */ + public function removeAll(): void; + + /** + * Проверяет наличие заголовка. Проверка идет на наличие ключа и значения + * + * @param string $key Заголовок + * + * @return bool + */ + public function has(string $key): bool; + + /** + * Получает значение заголовка + * + * @param string $key Заголовок + * + * @return string + * + * @throws InvalidArgumentException + */ + public function get(string $key): string; + + /** + * Получает все заголовки + * + * @return array + */ + public function getAll(): array; +} \ No newline at end of file