eee
This commit is contained in:
+35
-35
@@ -13,45 +13,45 @@ class BindForm implements InjectParameterInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $formValidate
|
* @param string $formValidate
|
||||||
*/
|
*/
|
||||||
public function __construct(readonly public string $formValidate)
|
public function __construct(readonly public string $formValidate)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function dispatch(string $class, string $method): mixed
|
public function dispatch(string $class, string $method): object
|
||||||
{
|
{
|
||||||
$validator = new Validator();
|
$validator = new Validator();
|
||||||
$reflect = \Kiri::getDi()->getReflectionClass($this->formValidate);
|
$reflect = \Kiri::getDi()->getReflectionClass($this->formValidate);
|
||||||
$validator->setFormData($reflect->newInstanceWithoutConstructor());
|
$validator->setFormData($reflect->newInstanceWithoutConstructor());
|
||||||
foreach ($reflect->getProperties() as $property) {
|
foreach ($reflect->getProperties() as $property) {
|
||||||
foreach ($property->getAttributes() as $attribute) {
|
foreach ($property->getAttributes() as $attribute) {
|
||||||
if (!class_exists($attribute->getName())) {
|
if (!class_exists($attribute->getName())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$rule = $attribute->newInstance();
|
$rule = $attribute->newInstance();
|
||||||
if ($rule instanceof ValidatorInterface) {
|
if ($rule instanceof ValidatorInterface) {
|
||||||
$validator->addRule($property->getName(), $rule);
|
$validator->addRule($property->getName(), $rule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$middleware = new ValidatorMiddleware();
|
$middleware = new ValidatorMiddleware();
|
||||||
$middleware->validator = $validator;
|
$middleware->validator = $validator;
|
||||||
|
|
||||||
$manager = \Kiri::getDi()->get(Middleware::class);
|
$manager = \Kiri::getDi()->get(Middleware::class);
|
||||||
$manager->set($class, $method, $middleware);
|
$manager->set($class, $method, $middleware);
|
||||||
|
|
||||||
return $validator;
|
return $validator->getFormData();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
namespace Kiri\Router\Validator;
|
namespace Kiri\Router\Validator;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri\Di\Inject\Container;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\MiddlewareInterface;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user