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

51 lines
1.2 KiB
PHP
Raw Normal View History

2023-04-16 03:38:47 +08:00
<?php
namespace Kiri\Router\Validator;
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:23:16 +08:00
* @param object $class
* @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 12:44:43 +08:00
* @throws \Exception
2023-04-16 03:38:47 +08:00
*/
2023-04-16 12:23:16 +08:00
public function dispatch(object $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
$manager = \Kiri::getDi()->get(Middleware::class);
$manager->set($class::class, $method, ValidatorMiddleware::class, [$validator]);
2023-04-16 03:38:47 +08:00
return $validator;
}
}