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

119 lines
1.7 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
{
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-08-02 11:07:15 +08:00
return $this->__handler__();
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-08-02 11:07:15 +08:00
return $this->__handler__($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
{
2021-08-02 11:07:15 +08:00
return $this->__handler__($name, $default);
2021-08-01 15:03:49 +08:00
}
/**
* @return string
*/
public function getContentType(): string
{
2021-08-02 11:07:15 +08:00
return $this->__handler__('content-type');
}
/**
* @return string|null
*/
public function getRequestUri(): ?string
{
return $this->__handler__('request_uri');
}
/**
* @return string|null
*/
public function getRequestMethod(): ?string
{
return $this->__handler__('request_method');
}
/**
* @return mixed
*/
public function getAgent(): mixed
{
return $this->__handler__('user-agent');
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-08-02 11:07:15 +08:00
return $this->__handler__($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-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
{
/** @var \Swoole\Http\Request $context */
$context = Context::getContext(\Swoole\Http\Request::class);
if (!empty($name)) {
return $context->header[$name] ?? $default;
}
return $context->header;
}
2020-08-31 01:27:08 +08:00
}