From 0db2958351a212198eaf198b27ea9ff0aa0fc52b Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 9 Nov 2023 22:08:24 +0800 Subject: [PATCH] eee --- src/Interface/ValidatorInterface.php | 4 +-- src/Validator/Inject/Email.php | 18 ++--------- src/Validator/Inject/Ignore.php | 12 ++++---- src/Validator/Inject/In.php | 28 +++++------------ src/Validator/Inject/Length.php | 28 +++++------------ src/Validator/Inject/Max.php | 18 ++--------- src/Validator/Inject/MaxLength.php | 29 +++++------------- src/Validator/Inject/Min.php | 29 +++++------------- src/Validator/Inject/MinLength.php | 29 +++++------------- src/Validator/Inject/Must.php | 41 ++++++++----------------- src/Validator/Inject/NotEmpty.php | 25 ++++------------ src/Validator/Inject/NotIn.php | 30 +++++-------------- src/Validator/Inject/NotNull.php | 28 +++++------------ src/Validator/Inject/Phone.php | 17 ++--------- src/Validator/Inject/Required.php | 17 ++--------- src/Validator/Inject/Round.php | 17 +++++------ src/Validator/Validator.php | 43 +++++++++++++++++---------- src/Validator/ValidatorMiddleware.php | 5 ++-- 18 files changed, 121 insertions(+), 297 deletions(-) diff --git a/src/Interface/ValidatorInterface.php b/src/Interface/ValidatorInterface.php index d73414f..f9e3c60 100644 --- a/src/Interface/ValidatorInterface.php +++ b/src/Interface/ValidatorInterface.php @@ -8,10 +8,10 @@ interface ValidatorInterface /** + * @param mixed $data * @param object $class - * @param string $name * @return bool */ - public function dispatch(object $class, string $name): bool; + public function dispatch(mixed $data, object $class): bool; } diff --git a/src/Validator/Inject/Email.php b/src/Validator/Inject/Email.php index 06923c9..33dd47d 100644 --- a/src/Validator/Inject/Email.php +++ b/src/Validator/Inject/Email.php @@ -3,22 +3,13 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Email implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @return string */ @@ -29,17 +20,12 @@ class Email implements ValidatorInterface /** + * @param mixed $data * @param object $class - * @param string $name * @return bool */ - public function dispatch(object $class, string $name): bool + public function dispatch(mixed $data, object $class): bool { - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } if ($data === null) { return false; } diff --git a/src/Validator/Inject/Ignore.php b/src/Validator/Inject/Ignore.php index 13284c5..3f879ea 100644 --- a/src/Validator/Inject/Ignore.php +++ b/src/Validator/Inject/Ignore.php @@ -10,12 +10,12 @@ class Ignore implements ValidatorInterface { - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool { return true; } diff --git a/src/Validator/Inject/In.php b/src/Validator/Inject/In.php index 39d21df..6c4c583 100644 --- a/src/Validator/Inject/In.php +++ b/src/Validator/Inject/In.php @@ -3,22 +3,13 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class In implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @param array $value */ @@ -27,18 +18,13 @@ class In implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { if ($data === null) { return false; } diff --git a/src/Validator/Inject/Length.php b/src/Validator/Inject/Length.php index 55db98d..ba54882 100644 --- a/src/Validator/Inject/Length.php +++ b/src/Validator/Inject/Length.php @@ -3,20 +3,12 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Length implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - /** * @param int $value @@ -26,19 +18,13 @@ class Length implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { if ($data === null) { return false; } diff --git a/src/Validator/Inject/Max.php b/src/Validator/Inject/Max.php index 9f1f740..59bd0cd 100644 --- a/src/Validator/Inject/Max.php +++ b/src/Validator/Inject/Max.php @@ -3,20 +3,12 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Max implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - /** * @param int $value @@ -27,18 +19,12 @@ class Max implements ValidatorInterface /** + * @param mixed $data * @param object $class - * @param string $name * @return bool */ - public function dispatch(object $class, string $name): bool + public function dispatch(mixed $data, object $class): bool { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } if ($data === null) { return false; } diff --git a/src/Validator/Inject/MaxLength.php b/src/Validator/Inject/MaxLength.php index 2e76f04..9f649c1 100644 --- a/src/Validator/Inject/MaxLength.php +++ b/src/Validator/Inject/MaxLength.php @@ -3,22 +3,13 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class MaxLength implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @param int $value @@ -28,19 +19,13 @@ class MaxLength implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { if ($data === null) { return false; } diff --git a/src/Validator/Inject/Min.php b/src/Validator/Inject/Min.php index 67b6fa1..afe738f 100644 --- a/src/Validator/Inject/Min.php +++ b/src/Validator/Inject/Min.php @@ -3,22 +3,13 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Min implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @param int $value */ @@ -27,19 +18,13 @@ class Min implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { if ($data === null) { return false; } diff --git a/src/Validator/Inject/MinLength.php b/src/Validator/Inject/MinLength.php index 1164846..954c7eb 100644 --- a/src/Validator/Inject/MinLength.php +++ b/src/Validator/Inject/MinLength.php @@ -3,22 +3,13 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class MinLength implements ValidatorInterface { - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @param int $value */ @@ -27,19 +18,13 @@ class MinLength implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { if ($data === null) { return false; } diff --git a/src/Validator/Inject/Must.php b/src/Validator/Inject/Must.php index e087581..84c4845 100644 --- a/src/Validator/Inject/Must.php +++ b/src/Validator/Inject/Must.php @@ -4,43 +4,28 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Must implements ValidatorInterface { /** - * @var RequestInterface + * @param mixed $value */ - #[Container(RequestInterface::class)] - public RequestInterface $request; + public function __construct(readonly public mixed $value) + { + } - /** - * @param mixed $value - */ - public function __construct(readonly public mixed $value) - { - } - - - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } - return $data === $this->value; - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { + return $data === $this->value; + } } diff --git a/src/Validator/Inject/NotEmpty.php b/src/Validator/Inject/NotEmpty.php index 9902aa9..f92bc18 100644 --- a/src/Validator/Inject/NotEmpty.php +++ b/src/Validator/Inject/NotEmpty.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class NotEmpty implements ValidatorInterface @@ -13,25 +11,12 @@ class NotEmpty implements ValidatorInterface /** - * @var RequestInterface + * @param mixed $data + * @param object $class + * @return bool */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + public function dispatch(mixed $data, object $class): bool + { return !empty($data); } } diff --git a/src/Validator/Inject/NotIn.php b/src/Validator/Inject/NotIn.php index eca8950..b35ee4f 100644 --- a/src/Validator/Inject/NotIn.php +++ b/src/Validator/Inject/NotIn.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] @@ -13,14 +11,6 @@ class NotIn implements ValidatorInterface { - - /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** * @param array $value */ @@ -29,19 +19,13 @@ class NotIn implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { return !in_array($data, $this->value); } } diff --git a/src/Validator/Inject/NotNull.php b/src/Validator/Inject/NotNull.php index cdf5aac..069e509 100644 --- a/src/Validator/Inject/NotNull.php +++ b/src/Validator/Inject/NotNull.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class NotNull implements ValidatorInterface @@ -13,24 +11,12 @@ class NotNull implements ValidatorInterface /** - * @var RequestInterface + * @param mixed $data + * @param object $class + * @return bool */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } - return !($data === null); - } + public function dispatch(mixed $data, object $class): bool + { + return !($data === null); + } } diff --git a/src/Validator/Inject/Phone.php b/src/Validator/Inject/Phone.php index f2bac58..e70e85d 100644 --- a/src/Validator/Inject/Phone.php +++ b/src/Validator/Inject/Phone.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Phone implements ValidatorInterface @@ -14,23 +12,12 @@ class Phone implements ValidatorInterface /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** + * @param mixed $data * @param object $class - * @param string $name * @return bool */ - public function dispatch(object $class, string $name): bool + public function dispatch(mixed $data, object $class): bool { - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } return preg_match(self::REG, $data); } } diff --git a/src/Validator/Inject/Required.php b/src/Validator/Inject/Required.php index 18de417..f3d162f 100644 --- a/src/Validator/Inject/Required.php +++ b/src/Validator/Inject/Required.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; -use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; -use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Required implements ValidatorInterface @@ -13,23 +11,12 @@ class Required implements ValidatorInterface /** - * @var RequestInterface - */ - #[Container(RequestInterface::class)] - public RequestInterface $request; - - /** + * @param mixed $data * @param object $class - * @param string $name * @return bool */ - public function dispatch(object $class, string $name): bool + public function dispatch(mixed $data, object $class): bool { - if ($this->request->getIsPost()) { - $data = $this->request->post($name, null); - } else { - $data = $this->request->query($name, null); - } return !($data === null); } diff --git a/src/Validator/Inject/Round.php b/src/Validator/Inject/Round.php index c4cebb1..efe9094 100644 --- a/src/Validator/Inject/Round.php +++ b/src/Validator/Inject/Round.php @@ -18,14 +18,13 @@ class Round implements ValidatorInterface } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - return round($class->$name, $this->value) === $class->$name; + /** + * @param mixed $data + * @param object $class + * @return bool + */ + public function dispatch(mixed $data, object $class): bool + { + return round($data, $this->value) === $data; } } diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index 2646db3..e4ee078 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -5,6 +5,7 @@ namespace Kiri\Router\Validator; use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Request; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ServerRequestInterface; @@ -70,36 +71,48 @@ class Validator $data = $request->getQueryParams(); } foreach ($data as $key => $value) { - if (property_exists($this->formData, $key)) { - $type = new \ReflectionProperty($this->formData, $key); - if (!($type->getType() instanceof \ReflectionUnionType)) { - $value = match ($type->getType()?->getName()) { - 'int' => (int)$value, - 'float' => (float)$value, - default => $value - }; - } - if ($value === 'Null') { - $value = null; - } - $this->formData->{$key} = $value; + if (!property_exists($this->formData, $key)) { + $this->addError($key); + return $this; } + $type = new \ReflectionProperty($this->formData, $key); + if (!($type->getType() instanceof \ReflectionUnionType)) { + $value = match ($type->getType()?->getName()) { + 'int' => (int)$value, + 'float' => (float)$value, + default => $value + }; + } + if ($value === 'Null') { + $value = null; + } + $this->formData->{$key} = $value; } return $this; } /** + * @param RequestInterface $request * @return bool */ - public function run(): bool + public function run(RequestInterface $request): bool { + if (!empty($this->message)) { + return false; + } foreach ($this->rules as $name => $rule) { + $value = $request->query($name, null); + if ($request->getIsPost()) { + $value = $request->post($name, null); + } foreach ($rule as $item) { - if (!$item->dispatch($this->formData, $name)) { + /** @var ValidatorInterface $item */ + if (!$item->dispatch($value, $this->formData)) { return $this->addError($name); } } + $this->formData->{$name} = $value; } return true; } diff --git a/src/Validator/ValidatorMiddleware.php b/src/Validator/ValidatorMiddleware.php index 20a1fa8..24b1310 100644 --- a/src/Validator/ValidatorMiddleware.php +++ b/src/Validator/ValidatorMiddleware.php @@ -35,9 +35,8 @@ class ValidatorMiddleware implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $validator = $this->validator->bindData($request); - if (!$validator->run()) { - return $this->response->html($validator->error(), 415); + if (!$this->validator->run($request)) { + return $this->response->html($this->validator->error(), 415); } else { return $handler->handle($request); }