eee
This commit is contained in:
+35
-35
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user