From 472210b158d138bdc7f90ddafbe77ba791346ee6 Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 9 Nov 2023 17:04:46 +0800 Subject: [PATCH] eee --- src/Validator/BindForm.php | 70 +++++++++++++-------------- src/Validator/ValidatorMiddleware.php | 34 ++++++++----- 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/Validator/BindForm.php b/src/Validator/BindForm.php index b73b451..66ad327 100644 --- a/src/Validator/BindForm.php +++ b/src/Validator/BindForm.php @@ -13,45 +13,45 @@ class BindForm implements InjectParameterInterface { - /** - * @param string $formValidate - */ - public function __construct(readonly public string $formValidate) - { - } + /** + * @param string $formValidate + */ + public function __construct(readonly public string $formValidate) + { + } - /** - * @param string $class - * @param string $method - * @return mixed - * @throws ReflectionException - * @throws Exception - */ - public function dispatch(string $class, string $method): mixed - { - $validator = new Validator(); - $reflect = \Kiri::getDi()->getReflectionClass($this->formValidate); - $validator->setFormData($reflect->newInstanceWithoutConstructor()); - foreach ($reflect->getProperties() as $property) { - foreach ($property->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } - $rule = $attribute->newInstance(); - if ($rule instanceof ValidatorInterface) { - $validator->addRule($property->getName(), $rule); - } - } - } + /** + * @param string $class + * @param string $method + * @return mixed + * @throws ReflectionException + * @throws Exception + */ + public function dispatch(string $class, string $method): object + { + $validator = new Validator(); + $reflect = \Kiri::getDi()->getReflectionClass($this->formValidate); + $validator->setFormData($reflect->newInstanceWithoutConstructor()); + foreach ($reflect->getProperties() as $property) { + foreach ($property->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { + continue; + } + $rule = $attribute->newInstance(); + if ($rule instanceof ValidatorInterface) { + $validator->addRule($property->getName(), $rule); + } + } + } - $middleware = new ValidatorMiddleware(); - $middleware->validator = $validator; + $middleware = new ValidatorMiddleware(); + $middleware->validator = $validator; - $manager = \Kiri::getDi()->get(Middleware::class); - $manager->set($class, $method, $middleware); + $manager = \Kiri::getDi()->get(Middleware::class); + $manager->set($class, $method, $middleware); - return $validator; - } + return $validator->getFormData(); + } } diff --git a/src/Validator/ValidatorMiddleware.php b/src/Validator/ValidatorMiddleware.php index cee04ac..80b0133 100644 --- a/src/Validator/ValidatorMiddleware.php +++ b/src/Validator/ValidatorMiddleware.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Kiri\Router\Validator; use Exception; +use Kiri\Di\Inject\Container; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -16,20 +17,29 @@ class ValidatorMiddleware implements MiddlewareInterface { - public Validator $validator; + public Validator $validator; + /** + * @var ResponseInterface + */ + #[Container(ResponseInterface::class)] + public ResponseInterface $response; - /** - * @param ServerRequestInterface $request - * @param RequestHandlerInterface $handler - * @return ResponseInterface - * @throws Exception - */ - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - $this->validator->bindData($request); - return $handler->handle($request); - } + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + * @throws Exception + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $validator = $this->validator->bindData($request); + if (!$validator->run($request)) { + return $this->response->html('400 Bad Request', 400); + } else { + return $handler->handle($request); + } + } }