Files
kiri-core/http-server/Constrict/Request.php
T

436 lines
7.7 KiB
PHP
Raw Normal View History

2021-08-05 19:14:08 +08:00
<?php
namespace Server\Constrict;
2021-08-17 16:52:50 +08:00
use Http\Context\Context;
2021-09-09 16:38:54 +08:00
use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
2021-08-26 18:27:20 +08:00
use Kiri\Kiri;
2021-09-09 16:38:54 +08:00
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
2021-09-06 17:43:40 +08:00
use Server\Message\Request as RequestMessage;
2021-09-06 17:59:12 +08:00
use Server\Message\Response;
2021-09-09 16:38:54 +08:00
use Server\Message\Uploaded;
use Server\RequestInterface;
2021-09-06 17:59:12 +08:00
use Server\ResponseInterface;
2021-08-05 19:14:08 +08:00
class Request implements RequestInterface
{
2021-09-06 17:43:40 +08:00
/**
2021-09-06 17:47:19 +08:00
* @return RequestMessage
2021-09-06 17:43:40 +08:00
*/
2021-09-06 17:47:19 +08:00
private function __call__(): RequestMessage
2021-09-06 17:43:40 +08:00
{
2021-09-09 17:25:52 +08:00
return Context::getContext(RequestInterface::class, new RequestMessage());
2021-09-06 17:43:40 +08:00
}
2021-08-26 18:27:20 +08:00
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
// TODO: Change the autogenerated stub
2021-09-06 17:43:40 +08:00
return $this->__call__()->{$name};
2021-08-26 18:27:20 +08:00
}
/**
* @param \Swoole\Http\Request $request
2021-09-07 16:14:44 +08:00
* @return array<RequestInterface, ResponseInterface>
2021-08-26 18:27:20 +08:00
*/
2021-09-06 19:03:38 +08:00
public static function create(\Swoole\Http\Request $request): array
2021-08-26 18:27:20 +08:00
{
2021-09-06 19:03:38 +08:00
Context::setContext(ResponseInterface::class, $response = new Response());
2021-08-26 18:27:20 +08:00
2021-09-09 17:25:52 +08:00
Context::setContext(RequestInterface::class, RequestMessage::parseRequest($request));
2021-08-26 18:27:20 +08:00
2021-09-06 19:03:38 +08:00
return [Kiri::getDi()->get(Request::class), $response];
2021-08-26 18:27:20 +08:00
}
2021-09-09 16:38:54 +08:00
/**
* @return string
*/
public function getProtocolVersion(): string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param string $version
* @return Request
*/
public function withProtocolVersion($version): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($version);
2021-09-09 16:38:54 +08:00
}
/**
* @return \string[][]
*/
public function getHeaders(): array
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @return bool
*/
public function hasHeader($name): bool
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @return string[]
*/
public function getHeader($name): array
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @return string
*/
public function getHeaderLine($name): string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param string|string[] $value
* @return Request
*/
public function withHeader($name, $value): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $value);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param string|string[] $value
* @return Request
*/
public function withAddedHeader($name, $value): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $value);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @return Request
*/
public function withoutHeader($name): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name);
2021-09-09 16:38:54 +08:00
}
/**
* @return StreamInterface
*/
public function getBody(): StreamInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param StreamInterface $body
* @return Request
*/
public function withBody(StreamInterface $body): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($body);
2021-09-09 16:38:54 +08:00
}
/**
* @return string
*/
public function getRequestTarget(): string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param mixed $requestTarget
* @return Request
*/
public function withRequestTarget($requestTarget): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($requestTarget);
2021-09-09 16:38:54 +08:00
}
/**
* @return string
*/
public function getMethod(): string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param string $method
* @return bool
*/
public function isMethod(string $method): bool
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($method);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $method
* @return Request
*/
public function withMethod($method): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($method);
2021-09-09 16:38:54 +08:00
}
/**
* @return UriInterface
*/
public function getUri(): UriInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param UriInterface $uri
* @param false $preserveHost
* @return Request
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($uri, $preserveHost);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @return Uploaded|null
*/
public function file(string $name): ?Uploaded
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name);
2021-09-09 16:38:54 +08:00
}
/**
* @return array
*/
public function all(): array
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool|int|string|null $default
* @return mixed
*/
public function query(string $name, bool|int|string|null $default = null): mixed
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $default);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param int|bool|array|string|null $default
* @return mixed
*/
public function post(string $name, int|bool|array|string|null $default = null): mixed
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $default);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function int(string $name, bool $required = false): ?int
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $required);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool $required
* @return float|null
*/
public function float(string $name, bool $required = false): ?float
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $required);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function date(string $name, bool $required = false): ?string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $required);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function timestamp(string $name, bool $required = false): ?int
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $required);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function string(string $name, bool $required = false): ?string
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $required);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $name
* @param array $default
* @return array|null
*/
public function array(string $name, array $default = []): ?array
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $default);
2021-09-09 16:38:54 +08:00
}
/**
* @return array|null
*/
public function gets(): ?array
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param string $field
* @param string $sizeField
* @param int $max
* @return float|int
*/
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($field, $sizeField, $max);
2021-09-09 16:38:54 +08:00
}
/**
* @param string $field
* @param int $max
* @return int
*/
public function size(string $field = 'size', int $max = 100): int
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($field, $max);
2021-09-09 16:38:54 +08:00
}
/**
* @param $name
* @param null $default
* @return mixed
*/
public function input($name, $default = null): mixed
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}($name, $default);
2021-09-09 16:38:54 +08:00
}
/**
* @return float
*/
#[Pure] public function getStartTime(): float
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
/**
* @param AuthIdentity $authority
*/
public function setAuthority(AuthIdentity $authority): void
{
2021-09-09 16:58:15 +08:00
$this->__call__()->{__FUNCTION__}($authority);
2021-09-09 16:38:54 +08:00
}
/**
* @return int
*/
public function getClientId(): int
{
2021-09-09 16:58:15 +08:00
return $this->__call__()->{__FUNCTION__}();
2021-09-09 16:38:54 +08:00
}
2021-09-09 17:01:52 +08:00
/**
* @return string|null
*/
public function getAccessControlAllowOrigin(): ?string
{
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string|null
*/
public function getAccessControlAllowHeaders(): ?string
{
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string|null
*/
public function getAccessControlRequestMethod(): ?string
{
return $this->__call__()->{__FUNCTION__}();
}
2021-08-05 19:14:08 +08:00
}