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-08-05 19:14:08 +08:00
|
|
|
private array $_headers = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $headers
|
|
|
|
|
*/
|
|
|
|
|
public function setHeaders(array $headers): void
|
|
|
|
|
{
|
|
|
|
|
$this->_headers = $headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 14:07:18 +08:00
|
|
|
/**
|
2021-08-02 11:07:15 +08:00
|
|
|
* @return array
|
2021-04-07 14:07:18 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function toArray(): array
|
2021-04-07 14:07:18 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__();
|
2021-04-07 14:07:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-02 11:07:15 +08:00
|
|
|
* @param $name
|
|
|
|
|
* @return mixed|null
|
2021-04-07 14:07:18 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function getHeader($name): ?string
|
2021-04-07 14:07:18 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__($name);
|
2021-04-07 14:07:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
2021-08-02 11:07:15 +08:00
|
|
|
* @param null $default
|
|
|
|
|
* @return mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function get($name, $default = null): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__($name, $default);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-02 11:07:15 +08:00
|
|
|
* @return string
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function getContentType(): string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__('content-type');
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
2021-08-01 15:16:50 +08:00
|
|
|
|
|
|
|
|
/**
|
2021-08-02 11:07:15 +08:00
|
|
|
* @return string|null
|
2021-08-01 15:16:50 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function getRequestUri(): ?string
|
2021-08-01 15:16:50 +08:00
|
|
|
{
|
2021-08-04 18:09:37 +08:00
|
|
|
$uri = $this->__handler__('request_uri', '/');
|
|
|
|
|
if (empty($uri)) {
|
|
|
|
|
return '/';
|
|
|
|
|
}
|
|
|
|
|
return $uri;
|
2021-08-01 15:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-02 14:28:21 +08:00
|
|
|
/**
|
2021-08-02 11:07:15 +08:00
|
|
|
* @return string|null
|
2020-09-02 14:28:21 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function getRequestMethod(): ?string
|
2020-09-02 14:28:21 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__('request_method');
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-25 15:57:52 +08:00
|
|
|
* @return mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-08-02 11:07:15 +08:00
|
|
|
public function getAgent(): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__('user-agent');
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:03:49 +08:00
|
|
|
* @param $name
|
|
|
|
|
* @return bool
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function exists($name): bool
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__($name) === null;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-27 18:21:30 +08:00
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:03:49 +08:00
|
|
|
* @return array
|
2021-07-27 18:21:30 +08:00
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function getHeaders(): array
|
2021-07-27 18:21:30 +08:00
|
|
|
{
|
2021-08-02 11:07:15 +08:00
|
|
|
return $this->__handler__();
|
2021-07-27 18:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-02 11:07:15 +08:00
|
|
|
/**
|
|
|
|
|
* @param null $name
|
|
|
|
|
* @param null $default
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
private function __handler__($name = null, $default = null): mixed
|
|
|
|
|
{
|
2021-08-05 19:14:08 +08:00
|
|
|
$headers = Context::getContext(Request::class);
|
2021-08-02 11:07:15 +08:00
|
|
|
if (!empty($name)) {
|
2021-08-05 19:14:08 +08:00
|
|
|
return $headers->_headers[$name] ?? $default;
|
2021-08-02 11:07:15 +08:00
|
|
|
}
|
2021-08-05 19:14:08 +08:00
|
|
|
return $headers->_headers;
|
2021-08-02 11:07:15 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|