This commit is contained in:
2021-09-10 10:20:46 +08:00
parent ce07230cc6
commit c02da89654
+486 -475
View File
@@ -20,479 +20,490 @@ class Request implements RequestInterface
{ {
/** /**
* @return ServerRequest * @return ServerRequest
*/ */
private function __call__(): ServerRequest private function __call__(): ServerRequest
{ {
return Context::getContext(RequestInterface::class, new ServerRequest()); return Context::getContext(RequestInterface::class, new ServerRequest());
} }
/** /**
* @param $name * @param string $name
* @return mixed * @param array $arguments
*/ * @return mixed
public function __get($name): mixed */
{ public function __call(string $name, array $arguments)
// TODO: Change the autogenerated stub {
return $this->__call__()->{$name}; return $this->__call__()->{$name}(...$arguments);
} }
/** /**
* @param \Swoole\Http\Request $request * @param $name
* @return array<RequestInterface, ResponseInterface> * @return mixed
*/ */
public static function create(\Swoole\Http\Request $request): array public function __get($name): mixed
{ {
$serverRequest = ServerRequest::createServerRequest($request); // TODO: Change the autogenerated stub
return $this->__call__()->{$name};
Context::setContext(ResponseInterface::class, $response = new Response()); }
Context::setContext(RequestInterface::class, $serverRequest);
/**
return [Kiri::getDi()->get(Request::class), $response]; * @param \Swoole\Http\Request $request
} * @return array<RequestInterface, ResponseInterface>
*/
public static function create(\Swoole\Http\Request $request): array
/** {
* @return string $serverRequest = ServerRequest::createServerRequest($request);
*/
public function getProtocolVersion(): string Context::setContext(ResponseInterface::class, $response = new Response());
{
return $this->__call__()->{__FUNCTION__}(); Context::setContext(RequestInterface::class, $serverRequest);
}
return [Kiri::getDi()->get(Request::class), $response];
}
/**
* @param string $version
* @return Request /**
*/ * @return string
public function withProtocolVersion($version): RequestInterface */
{ public function getProtocolVersion(): string
return $this->__call__()->{__FUNCTION__}($version); {
} return $this->__call__()->{__FUNCTION__}();
}
/**
* @return \string[][] /**
*/ * @param string $version
public function getHeaders(): array * @return Request
{ */
return $this->__call__()->{__FUNCTION__}(); public function withProtocolVersion($version): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($version);
}
/**
* @param string $name
* @return bool /**
*/ * @return \string[][]
public function hasHeader($name): bool */
{ public function getHeaders(): array
return $this->__call__()->{__FUNCTION__}($name); {
} return $this->__call__()->{__FUNCTION__}();
}
/**
* @param string $name /**
* @return string[] * @param string $name
*/ * @return bool
public function getHeader($name): array */
{ public function hasHeader($name): bool
return $this->__call__()->{__FUNCTION__}($name); {
} return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param string $name /**
* @return string * @param string $name
*/ * @return string[]
public function getHeaderLine($name): string */
{ public function getHeader($name): array
return $this->__call__()->{__FUNCTION__}($name); {
} return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param string $name /**
* @param string|string[] $value * @param string $name
* @return Request * @return string
*/ */
public function withHeader($name, $value): RequestInterface public function getHeaderLine($name): string
{ {
return $this->__call__()->{__FUNCTION__}($name, $value); return $this->__call__()->{__FUNCTION__}($name);
} }
/** /**
* @param string $name * @param string $name
* @param string|string[] $value * @param string|string[] $value
* @return Request * @return Request
*/ */
public function withAddedHeader($name, $value): RequestInterface public function withHeader($name, $value): RequestInterface
{ {
return $this->__call__()->{__FUNCTION__}($name, $value); return $this->__call__()->{__FUNCTION__}($name, $value);
} }
/** /**
* @param string $name * @param string $name
* @return Request * @param string|string[] $value
*/ * @return Request
public function withoutHeader($name): RequestInterface */
{ public function withAddedHeader($name, $value): RequestInterface
return $this->__call__()->{__FUNCTION__}($name); {
} return $this->__call__()->{__FUNCTION__}($name, $value);
}
/**
* @return StreamInterface /**
*/ * @param string $name
public function getBody(): StreamInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}(); public function withoutHeader($name): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param StreamInterface $body
* @return Request /**
*/ * @return StreamInterface
public function withBody(StreamInterface $body): RequestInterface */
{ public function getBody(): StreamInterface
return $this->__call__()->{__FUNCTION__}($body); {
} return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string /**
*/ * @param StreamInterface $body
public function getRequestTarget(): string * @return Request
{ */
return $this->__call__()->{__FUNCTION__}(); public function withBody(StreamInterface $body): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($body);
}
/**
* @param mixed $requestTarget
* @return Request /**
*/ * @return string
public function withRequestTarget($requestTarget): RequestInterface */
{ public function getRequestTarget(): string
return $this->__call__()->{__FUNCTION__}($requestTarget); {
} return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string /**
*/ * @param mixed $requestTarget
public function getMethod(): string * @return Request
{ */
return $this->__call__()->{__FUNCTION__}(); public function withRequestTarget($requestTarget): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($requestTarget);
}
/**
* @param string $method
* @return bool /**
*/ * @return string
public function isMethod(string $method): bool */
{ public function getMethod(): string
return $this->__call__()->{__FUNCTION__}($method); {
} return $this->__call__()->{__FUNCTION__}();
}
/**
* @param string $method /**
* @return Request * @param string $method
*/ * @return bool
public function withMethod($method): RequestInterface */
{ public function isMethod(string $method): bool
return $this->__call__()->{__FUNCTION__}($method); {
} return $this->__call__()->{__FUNCTION__}($method);
}
/**
* @return UriInterface /**
*/ * @param string $method
public function getUri(): UriInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}(); public function withMethod($method): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($method);
}
/**
* @param UriInterface $uri
* @param false $preserveHost /**
* @return Request * @return UriInterface
*/ */
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface public function getUri(): UriInterface
{ {
return $this->__call__()->{__FUNCTION__}($uri, $preserveHost); return $this->__call__()->{__FUNCTION__}();
} }
/** /**
* @param string $name * @param UriInterface $uri
* @return UploadedFileInterface|null * @param false $preserveHost
*/ * @return Request
public function file(string $name): ?UploadedFileInterface */
{ public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
$files = $this->__call__()->getParsedBody(); {
if (!isset($files[$name])) { return $this->__call__()->{__FUNCTION__}($uri, $preserveHost);
return null; }
}
return new Uploaded($files['tmp_name'], $files['name'], $files['type'], $files['size'], $files['error']);
} /**
* @param string $name
* @return UploadedFileInterface|null
/** */
* @param string|null $name public function file(string $name): ?UploadedFileInterface
* @param mixed|null $default {
* @return mixed $files = $this->__call__()->getParsedBody();
*/ if (!isset($files[$name])) {
private function _getParsedBody(string|null $name = null, mixed $default = null): mixed return null;
{ }
$body = $this->__call__()->getParsedBody(); return new Uploaded($files['tmp_name'], $files['name'], $files['type'], $files['size'], $files['error']);
if (empty($name)) { }
return $body;
}
return $body[$name] ?? $default; /**
} * @param string|null $name
* @param mixed|null $default
* @return mixed
/** */
* @return array private function _getParsedBody(string|null $name = null, mixed $default = null): mixed
*/ {
public function all(): array $body = $this->__call__()->getParsedBody();
{ if (empty($name)) {
return $this->_getParsedBody(); return $body;
} }
return $body[$name] ?? $default;
}
/**
* @param string $name
* @param bool|int|string|null $default /**
* @return mixed * @return array
*/ */
public function query(string $name, bool|int|string|null $default = null): mixed public function all(): array
{ {
$files = $this->__call__()->getQueryParams(); return $this->_getParsedBody();
}
return $files[$name] ?? $default;
}
/**
* @param string $name
/** * @param bool|int|string|null $default
* @param string $name * @return mixed
* @param int|bool|array|string|null $default */
* @return mixed public function query(string $name, bool|int|string|null $default = null): mixed
*/ {
public function post(string $name, int|bool|array|string|null $default = null): mixed $files = $this->__call__()->getQueryParams();
{
return $this->_getParsedBody($name, $default); return $files[$name] ?? $default;
} }
/** /**
* @param string $name * @param string $name
* @param bool $required * @param int|bool|array|string|null $default
* @return int|null * @return mixed
* @throws \Exception */
*/ public function post(string $name, int|bool|array|string|null $default = null): mixed
public function int(string $name, bool $required = false): ?int {
{ return $this->_getParsedBody($name, $default);
$int = $this->_getParsedBody($name); }
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
} /**
return (int)$int; * @param string $name
} * @param bool $required
* @return int|null
* @throws \Exception
/** */
* @param string $name public function int(string $name, bool $required = false): ?int
* @param bool $required {
* @return float|null $int = $this->_getParsedBody($name);
* @throws \Exception if (is_null($int) && $required) {
*/ throw new \Exception('Required param "' . $name . '"');
public function float(string $name, bool $required = false): ?float }
{ return (int)$int;
$int = $this->_getParsedBody($name); }
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
} /**
return (float)$int; * @param string $name
} * @param bool $required
* @return float|null
* @throws \Exception
/** */
* @param string $name public function float(string $name, bool $required = false): ?float
* @param bool $required {
* @return string|null $int = $this->_getParsedBody($name);
* @throws \Exception if (is_null($int) && $required) {
*/ throw new \Exception('Required param "' . $name . '"');
public function date(string $name, bool $required = false): ?string }
{ return (float)$int;
$int = $this->_getParsedBody($name); }
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
} /**
return (string)$int; * @param string $name
} * @param bool $required
* @return string|null
* @throws \Exception
/** */
* @param string $name public function date(string $name, bool $required = false): ?string
* @param bool $required {
* @return int|null $int = $this->_getParsedBody($name);
* @throws \Exception if (is_null($int) && $required) {
*/ throw new \Exception('Required param "' . $name . '"');
public function timestamp(string $name, bool $required = false): ?int }
{ return (string)$int;
$int = $this->_getParsedBody($name); }
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
} /**
return (int)$int; * @param string $name
} * @param bool $required
* @return int|null
* @throws \Exception
/** */
* @param string $name public function timestamp(string $name, bool $required = false): ?int
* @param bool $required {
* @return string|null $int = $this->_getParsedBody($name);
* @throws \Exception if (is_null($int) && $required) {
*/ throw new \Exception('Required param "' . $name . '"');
public function string(string $name, bool $required = false): ?string }
{ return (int)$int;
$int = $this->_getParsedBody($name); }
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
} /**
return (string)$int; * @param string $name
} * @param bool $required
* @return string|null
* @throws \Exception
/** */
* @param string $name public function string(string $name, bool $required = false): ?string
* @param array $default {
* @return array|null $int = $this->_getParsedBody($name);
*/ if (is_null($int) && $required) {
public function array(string $name, array $default = []): ?array throw new \Exception('Required param "' . $name . '"');
{ }
$int = $this->_getParsedBody($name); return (string)$int;
if (is_null($int)) { }
return $default;
}
return $int; /**
} * @param string $name
* @param array $default
* @return array|null
/** */
* @return array|null public function array(string $name, array $default = []): ?array
*/ {
public function gets(): ?array $int = $this->_getParsedBody($name);
{ if (is_null($int)) {
return $this->__call__()->getQueryParams(); return $default;
} }
return $int;
}
/**
* @param string $field
* @param string $sizeField /**
* @param int $max * @return array|null
* @return float|int */
*/ public function gets(): ?array
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int {
{ return $this->__call__()->getQueryParams();
$page = $this->query($field); }
$size = $this->size($sizeField, $max);
$offset = ($page - 1) * $size;
if ($offset < 0) { /**
$offset = 0; * @param string $field
} * @param string $sizeField
return $offset; * @param int $max
} * @return float|int
*/
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
/** {
* @param string $field $page = $this->query($field);
* @param int $max $size = $this->size($sizeField, $max);
* @return int $offset = ($page - 1) * $size;
*/ if ($offset < 0) {
public function size(string $field = 'size', int $max = 100): int $offset = 0;
{ }
$size = $this->query($field); return $offset;
if ($size > $max) { }
$size = $max;
}
return $size; /**
} * @param string $field
* @param int $max
* @return int
/** */
* @param $name public function size(string $field = 'size', int $max = 100): int
* @param null $default {
* @return mixed $size = $this->query($field);
*/ if ($size > $max) {
public function input($name, $default = null): mixed $size = $max;
{ }
return $this->_getParsedBody($name, $default); return $size;
} }
/** /**
* @return float * @param $name
*/ * @param null $default
#[Pure] public function getStartTime(): float * @return mixed
{ */
return $this->__call__()->{__FUNCTION__}(); public function input($name, $default = null): mixed
} {
return $this->_getParsedBody($name, $default);
}
/**
* @param AuthIdentity $authority
*/ /**
public function setAuthority(AuthIdentity $authority): void * @return float
{ */
$this->__call__()->{__FUNCTION__}($authority); #[Pure] public function getStartTime(): float
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return int
*/ /**
public function getClientId(): int * @param AuthIdentity $authority
{ */
return $this->__call__()->{__FUNCTION__}(); public function setAuthority(AuthIdentity $authority): void
} {
$this->__call__()->{__FUNCTION__}($authority);
}
/**
* @return string|null
*/ /**
#[Pure] public function getAccessControlAllowOrigin(): ?string * @return int
{ */
return $this->__call__()->{__FUNCTION__}(); public function getClientId(): int
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string|null
*/ /**
#[Pure] public function getAccessControlAllowHeaders(): ?string * @return string|null
{ */
return $this->__call__()->{__FUNCTION__}(); #[Pure] public function getAccessControlAllowOrigin(): ?string
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string|null
*/ /**
#[Pure] public function getAccessControlRequestMethod(): ?string * @return string|null
{ */
return $this->__call__()->{__FUNCTION__}(); #[Pure] public function getAccessControlAllowHeaders(): ?string
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @return string|null
*/
#[Pure] public function getAccessControlRequestMethod(): ?string
{
return $this->__call__()->{__FUNCTION__}();
}
} }