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

139 lines
2.0 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-12-25 15:57:52 +08:00
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
{
2021-04-07 14:07:18 +08:00
/**
* @param string $uri
*/
public function setRequestUri(string $uri)
{
2021-07-27 18:21:30 +08:00
$this->replace('request_uri', $uri);
2021-04-07 14:07:18 +08:00
}
/**
* @param string $method
*/
public function setRequestMethod(string $method)
{
2021-07-27 18:21:30 +08:00
$this->replace('request_method', $method);
2021-04-07 14:07:18 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param $value
*/
public function replace($name, $value)
{
2021-07-27 18:21:30 +08:00
$this->addHeaders([$name => $value]);
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @param $value
*/
public function addHeader($name, $value)
{
2021-07-27 18:21:30 +08:00
$this->addHeaders([$name => $value]);
2020-08-31 01:27:08 +08:00
}
/**
* @param array $headers
* @return $this
*/
2020-12-25 15:57:52 +08:00
public function addHeaders(array $headers): static
2020-08-31 01:27:08 +08:00
{
if (empty($headers)) {
return $this;
}
2021-07-27 18:21:30 +08:00
$request = Context::getContext('request');
if (!empty($request->headers)) {
$headers = array_merge($request->headers, $headers);
2020-08-31 01:27:08 +08:00
}
2021-07-27 18:21:30 +08:00
$request->headers = $headers;
2020-08-31 01:27:08 +08:00
return $this;
}
2020-09-02 14:28:21 +08:00
/**
* @return array
*/
2020-12-25 15:57:52 +08:00
public function toArray(): array
2020-09-02 14:28:21 +08:00
{
2021-07-27 18:21:30 +08:00
return $this->___call();
2020-09-02 14:28:21 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @return mixed|null
*/
2020-12-25 15:57:52 +08:00
public function getHeader($name): ?string
2020-08-31 01:27:08 +08:00
{
2021-07-27 18:21:30 +08:00
$headers = $this->___call();
if (!isset($headers[$name])) {
2020-09-17 19:10:38 +08:00
return null;
}
2021-07-27 18:21:30 +08:00
return $headers[$name];
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
2020-09-08 01:01:58 +08:00
* @param null $default
2020-12-25 15:57:52 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2020-12-25 15:57:52 +08:00
public function get($name, $default = null): mixed
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
*/
2020-12-25 15:57:52 +08:00
public function exists($name): bool
2020-08-31 01:27:08 +08:00
{
2021-07-27 18:21:30 +08:00
$headers = $this->___call();
return isset($headers[$name]) && $headers[$name] != null;
2020-08-31 01:27:08 +08:00
}
/**
* @return array
*/
2020-12-25 15:57:52 +08:00
public function getHeaders(): array
2020-08-31 01:27:08 +08:00
{
2021-07-27 18:21:30 +08:00
return $this->___call();
2020-08-31 01:27:08 +08:00
}
2021-07-27 18:21:30 +08:00
/**
* @return mixed
*/
private function ___call(): array
{
return Context::getContext('request')->header ?? [];
}
2020-08-31 01:27:08 +08:00
}