This commit is contained in:
2021-09-06 11:12:52 +08:00
parent 53e0163388
commit ca24835df8
+270 -236
View File
@@ -3,6 +3,7 @@
namespace Server\Message; namespace Server\Message;
use BadMethodCallException; use BadMethodCallException;
use Exception;
use Http\IInterface\AuthIdentity; use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
@@ -15,295 +16,328 @@ use Psr\Http\Message\UriInterface;
class Request implements RequestInterface class Request implements RequestInterface
{ {
use Message; use Message;
public string $requestTarget; public string $requestTarget;
public string $method; public string $method;
/** /**
* @var Uri|UriInterface * @var Uri|UriInterface
*/ */
private Uri|UriInterface $uri; private Uri|UriInterface $uri;
private \Swoole\Http\Request $serverRequest; private \Swoole\Http\Request $serverRequest;
private array $parseBody; private array $parseBody;
private array $files = []; private array $files = [];
/** /**
* @var AuthIdentity|null * @var AuthIdentity|null
*/ */
public ?AuthIdentity $authority = null; public ?AuthIdentity $authority = null;
/** /**
* @return int * @return int
*/ */
public function getClientId(): int public function getClientId(): int
{ {
return $this->serverRequest->fd; return $this->serverRequest->fd;
} }
/** /**
* @param AuthIdentity $authority * @param AuthIdentity $authority
*/ */
public function setAuthority(AuthIdentity $authority) public function setAuthority(AuthIdentity $authority)
{ {
$this->authority = $authority; $this->authority = $authority;
} }
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return RequestInterface * @return RequestInterface
*/ */
public static function parseRequest(\Swoole\Http\Request $request): RequestInterface public static function parseRequest(\Swoole\Http\Request $request): RequestInterface
{ {
$message = new Request(); $message = new Request();
$message->uri = Uri::parseUri($request); $message->uri = Uri::parseUri($request);
$message->method = $request->getMethod(); $message->method = $request->getMethod();
$message->requestTarget = ''; $message->requestTarget = '';
$message->serverRequest = $request; $message->serverRequest = $request;
$message->version = $request->server['server_protocol']; $message->version = $request->server['server_protocol'];
$message->stream = new Stream($request->getContent()); $message->stream = new Stream($request->getContent());
$message->servers = $request->server; $message->servers = $request->server;
$message->parseRequestHeaders($request); $message->parseRequestHeaders($request);
return $message; return $message;
} }
/** /**
* @return float * @return float
*/ */
#[Pure] public function getStartTime(): float #[Pure] public function getStartTime(): float
{ {
return $this->servers['request_time_float']; return $this->servers['request_time_float'];
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed * @return mixed
*/ */
public function input($name, $default = null): mixed public function input($name, $default = null): mixed
{ {
if (empty($this->parseBody)) { if (empty($this->parseBody)) {
$this->parseBody = $this->parseBody($this->stream); $this->parseBody = $this->parseBody($this->stream);
} }
if (!is_array($this->parseBody)) { if (!is_array($this->parseBody)) {
return $default; return $default;
} }
return $this->parseBody[$name] ?? $default; return $this->parseBody[$name] ?? $default;
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed * @return mixed
*/ */
public function post($name, $default = null): mixed public function post($name, $default = null): mixed
{ {
return $this->serverRequest->post[$name] ?? $default; return $this->serverRequest->post[$name] ?? $default;
} }
/** /**
* @param $name * @param string $field
* @param $required * @param int $max
* @return mixed * @return int
* @throws \Exception */
*/ public function size(string $field = 'size', int $max = 100): int
public function string($name, $required) {
{ $size = (int)$this->query($field);
if (is_null($data = $this->post($name))) { if ($size < 1) {
if ($required) { $size = 1;
throw new \Exception('Parameter is required and cannot be empty.'); } else if ($size > $max) {
} $size = $max;
} }
return $data; return $size;
} }
/** /**
* @param $name * @param string $field
* @param $required * @param string $sizeField
* @return mixed * @param int $max
* @throws \Exception * @return float|int
*/ */
public function int($name, $required) public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
{ {
if (is_null($data = $this->post($name))) { $page = (int)$this->query($field);
if ($required) { if ($page < 1) {
throw new \Exception('Parameter is required and cannot be empty.'); $page = 1;
} }
} return ($page - 1) * $this->size($sizeField, $max);
return (string)$data; }
}
/** /**
* @param $name * @param $name
* @param $required * @param $required
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function float($name, $required) public function string($name, $required): mixed
{ {
if (is_null($data = $this->post($name))) { if (is_null($data = $this->post($name))) {
if ($required) { if ($required) {
throw new \Exception('Parameter is required and cannot be empty.'); throw new Exception('Parameter is required and cannot be empty.');
} }
} }
return (float)$data; return $data;
} }
/** /**
* @param $name * @param $name
* @param $required * @param $required
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function array($name, $required) public function int($name, $required)
{ {
if (is_null($data = $this->post($name))) { if (is_null($data = $this->post($name))) {
if ($required) { if ($required) {
throw new \Exception('Parameter is required and cannot be empty.'); throw new Exception('Parameter is required and cannot be empty.');
} }
} }
if (!is_array($data)) return []; return (string)$data;
return $data; }
}
/** /**
* @return array|null * @param $name
*/ * @param $required
public function gets(): ?array * @return mixed
{ * @throws Exception
return $this->serverRequest->get; */
} public function float($name, $required)
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new Exception('Parameter is required and cannot be empty.');
}
}
return (float)$data;
}
/** /**
* @param $name * @param $name
* @param null $default * @param $required
* @return mixed * @return mixed
*/ * @throws Exception
public function query($name, $default = null): mixed */
{ public function array($name, $required)
return $this->serverRequest->get[$name] ?? $default; {
} if (is_null($data = $this->post($name))) {
if ($required) {
throw new Exception('Parameter is required and cannot be empty.');
}
}
if (!is_array($data)) return [];
return $data;
}
/** /**
* @param $name * @return array|null
* @return Uploaded|null */
*/ public function gets(): ?array
public function file($name): ?Uploaded {
{ return $this->serverRequest->get;
if (isset($this->serverRequest->files[$name])) { }
return new Uploaded($this->serverRequest->files[$name]);
}
return null;
}
/** /**
* @return array * @param $name
*/ * @param null $default
public function all(): array * @return mixed
{ */
if (empty($this->parseBody)) { public function query($name, $default = null): mixed
$this->parseBody = $this->parseBody($this->stream); {
} return $this->serverRequest->get[$name] ?? $default;
return array_merge($this->serverRequest->post ?? [], }
$this->serverRequest->get ?? [],
is_array($this->parseBody) ? $this->parseBody : []
);
}
/** /**
* @return string * @param $name
*/ * @return Uploaded|null
public function getRequestTarget(): string */
{ public function file($name): ?Uploaded
throw new BadMethodCallException('Not Accomplish Method.'); {
} if (isset($this->serverRequest->files[$name])) {
return new Uploaded($this->serverRequest->files[$name]);
}
return null;
}
/** /**
* @param mixed $requestTarget * @return array
* @return RequestInterface */
*/ public function all(): array
public function withRequestTarget($requestTarget): RequestInterface {
{ if (empty($this->parseBody)) {
$this->requestTarget = $requestTarget; $this->parseBody = $this->parseBody($this->stream);
return $this; }
} return array_merge($this->serverRequest->post ?? [],
$this->serverRequest->get ?? [],
is_array($this->parseBody) ? $this->parseBody : []
);
}
/** /**
* @return string * @return string
*/ */
public function getMethod(): string public function getRequestTarget(): string
{ {
return $this->method; throw new BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $method * @param mixed $requestTarget
* @return bool * @return RequestInterface
*/ */
public function isMethod(string $method): bool public function withRequestTarget($requestTarget): RequestInterface
{ {
return $this->method === $method; $this->requestTarget = $requestTarget;
} return $this;
}
/** /**
* @param string $method * @return string
* @return RequestInterface */
*/ public function getMethod(): string
public function withMethod($method): RequestInterface {
{ return $this->method;
$class = clone $this; }
$class->method = $method;
return $class;
}
/** /**
* @return \Server\Message\Uri|\Psr\Http\Message\UriInterface * @param string $method
*/ * @return bool
public function getUri(): Uri|UriInterface */
{ public function isMethod(string $method): bool
return $this->uri; {
} return $this->method === $method;
}
/** /**
* @param UriInterface $uri * @param string $method
* @param false $preserveHost * @return RequestInterface
* @return RequestInterface */
*/ public function withMethod($method): RequestInterface
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface {
{ $class = clone $this;
$class = clone $this; $class->method = $method;
$class->uri = $uri; return $class;
return $class; }
}
/**
* @return \Server\Message\Uri|\Psr\Http\Message\UriInterface
*/
public function getUri(): Uri|UriInterface
{
return $this->uri;
}
/**
* @param UriInterface $uri
* @param false $preserveHost
* @return RequestInterface
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
$class = clone $this;
$class->uri = $uri;
return $class;
}
} }