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