Skip to content

Commit

Permalink
Базовая реализация компонента
Browse files Browse the repository at this point in the history
  • Loading branch information
mepihindeveloper committed Mar 4, 2021
1 parent fbc37eb commit 568e018
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
126 changes: 126 additions & 0 deletions Headers.php
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;
}
}
71 changes: 71 additions & 0 deletions interfaces/HeadersInterface.php
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;
}

0 comments on commit 568e018

Please sign in to comment.