-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbc37eb
commit 568e018
Showing
2 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* Класс Headers | ||
* | ||
* Реализует управление заголовками запроса | ||
*/ | ||
class Headers implements HeadersInterface { | ||
|
||
/** | ||
* @var array Заголовки | ||
*/ | ||
private array $headers; | ||
|
||
public function __construct() | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
/** | ||
* Интерфейс HeadersInterface | ||
* | ||
* Декларирует методы обязательные для реализации компонента Headers | ||
*/ | ||
interface HeadersInterface { | ||
|
||
/** | ||
* Устанавливает заголовок(и) | ||
* | ||
* @param array $params Заголовок(и) [key => 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; | ||
} |