Files
kiri-core/HttpServer/Http/HttpHeaders.php
T

152 lines
2.1 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-18
* Time: 14:54
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Http;
/**
* Class HttpHeaders
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Http
2020-08-31 01:27:08 +08:00
*/
class HttpHeaders
{
/**
* @var string[]
*/
2020-10-29 18:17:25 +08:00
private array $headers = [];
2020-08-31 01:27:08 +08:00
/**
* @var string[]
*/
2020-10-29 18:17:25 +08:00
private array $response = [];
2020-08-31 01:27:08 +08:00
/**
* HttpHeaders constructor.
* @param $headers
*/
public function __construct($headers)
{
$this->headers = $headers;
}
/**
* @param $name
* @param $value
*/
public function setHeader($name, $value)
{
$this->response[$name] = $value;
}
/**
* @param array $headers
*/
public function setHeaders(array $headers)
{
foreach ($headers as $key => $val) {
$this->response[$key] = $val;
}
}
/**
* @param $name
* @param $value
*/
public function replace($name, $value)
{
$this->headers[$name] = $value;
}
/**
* @param $name
* @param $value
*/
public function addHeader($name, $value)
{
$this->headers[$name] = $value;
}
/**
* @param array $headers
* @return $this
*/
public function addHeaders(array $headers)
{
if (empty($headers)) {
return $this;
}
if (!empty($this->headers)) {
$headers = array_merge($this->headers, $headers);
}
$this->headers = $headers;
return $this;
}
/**
* @return array
*/
public function getResponseHeaders()
{
return $this->response;
}
2020-09-02 14:28:21 +08:00
/**
* @return array
*/
public function toArray()
{
return $this->headers;
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @return mixed|null
*/
public function getHeader($name)
{
2020-09-17 19:10:38 +08:00
if (!isset($this->headers[$name])) {
return null;
}
return $this->headers[$name];
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
2020-09-08 01:01:58 +08:00
* @param null $default
2020-08-31 01:27:08 +08:00
* @return mixed|string|null
*/
2020-09-08 01:01:58 +08:00
public function get($name, $default = null)
2020-08-31 01:27:08 +08:00
{
2020-09-08 01:02:17 +08:00
if (($value = $this->getHeader($name)) === null) {
2020-09-08 01:01:58 +08:00
return $default;
}
return $value;
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @return bool
*/
public function exists($name)
{
return isset($this->headers[$name]) && $this->headers[$name] != null;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
}