Files
kiri-router/src/Validator/BindForm.php
T

55 lines
1.2 KiB
PHP
Raw Normal View History

2023-04-16 03:38:47 +08:00
<?php
namespace Kiri\Router\Validator;
2023-04-16 13:27:54 +08:00
use Exception;
2023-04-16 03:38:47 +08:00
use Kiri\Di\Interface\InjectParameterInterface;
2023-04-16 12:44:43 +08:00
use Kiri\Router\Base\Middleware;
2023-04-16 03:38:47 +08:00
use Kiri\Router\Interface\ValidatorInterface;
2023-04-16 12:23:16 +08:00
use ReflectionException;
2023-04-16 03:38:47 +08:00
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class BindForm implements InjectParameterInterface
{
/**
* @param string $formValidate
*/
public function __construct(readonly public string $formValidate)
{
}
/**
2023-04-16 12:50:57 +08:00
* @param string $class
2023-04-16 12:23:16 +08:00
* @param string $method
2023-04-16 03:38:47 +08:00
* @return mixed
2023-04-16 12:23:16 +08:00
* @throws ReflectionException
2023-04-16 13:27:54 +08:00
* @throws Exception
2023-04-16 03:38:47 +08:00
*/
2023-04-16 12:50:57 +08:00
public function dispatch(string $class, string $method): mixed
2023-04-16 03:38:47 +08:00
{
$validator = new Validator();
$reflect = \Kiri::getDi()->getReflectionClass($this->formValidate);
2023-04-16 03:51:18 +08:00
$validator->setFormData($reflect->newInstanceWithoutConstructor());
2023-04-16 03:38:47 +08:00
foreach ($reflect->getProperties() as $property) {
foreach ($property->getAttributes() as $attribute) {
$rule = $attribute->newInstance();
if ($rule instanceof ValidatorInterface) {
2023-04-16 03:51:18 +08:00
$validator->addRule($property->getName(), $rule);
2023-04-16 03:38:47 +08:00
}
}
}
2023-04-16 12:44:43 +08:00
2023-04-16 14:49:35 +08:00
$middleware = new ValidatorMiddleware();
$middleware->validator = $validator;
$manager = \Kiri::getDi()->get(Middleware::class);
$manager->set($class, $method, $middleware);
2023-04-16 03:38:47 +08:00
return $validator;
}
}