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-12-04 23:38:33 +08:00
|
|
|
use Kiri\Router\Validator\Inject\Binding;
|
2023-04-16 12:23:16 +08:00
|
|
|
use ReflectionException;
|
2023-12-04 21:17:04 +08:00
|
|
|
use ReflectionNamedType;
|
|
|
|
|
use ReflectionUnionType;
|
2023-04-16 03:38:47 +08:00
|
|
|
|
|
|
|
|
#[\Attribute(\Attribute::TARGET_PARAMETER)]
|
|
|
|
|
class BindForm implements InjectParameterInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 17:04:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $formValidate
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(readonly public string $formValidate)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return mixed
|
2023-12-12 15:35:35 +08:00
|
|
|
* @throws
|
2023-11-09 17:04:46 +08:00
|
|
|
*/
|
|
|
|
|
public function dispatch(string $class, string $method): object
|
|
|
|
|
{
|
|
|
|
|
$validator = new Validator();
|
2023-11-09 20:33:44 +08:00
|
|
|
$container = \Kiri::getDi();
|
|
|
|
|
$reflect = $container->getReflectionClass($this->formValidate);
|
2023-12-04 21:17:04 +08:00
|
|
|
$object = $validator->setFormData($reflect->newInstanceWithoutConstructor());
|
2023-11-09 17:04:46 +08:00
|
|
|
foreach ($reflect->getProperties() as $property) {
|
|
|
|
|
foreach ($property->getAttributes() as $attribute) {
|
|
|
|
|
if (!class_exists($attribute->getName())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-11-09 20:45:16 +08:00
|
|
|
$rule = \inject($attribute->newInstance());
|
2023-11-09 17:04:46 +08:00
|
|
|
if ($rule instanceof ValidatorInterface) {
|
|
|
|
|
$validator->addRule($property->getName(), $rule);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-04 22:15:18 +08:00
|
|
|
$validator->setTypes($property->getName(), $property->getType());
|
2023-12-04 21:17:04 +08:00
|
|
|
if (!$property->hasDefaultValue()) {
|
|
|
|
|
$this->insertDefaultValue($property->getType(), $object, $property->getName());
|
|
|
|
|
}
|
2023-11-09 17:04:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-09 17:38:49 +08:00
|
|
|
$middleware = \instance(ValidatorMiddleware::class);
|
2023-11-09 17:04:46 +08:00
|
|
|
$middleware->validator = $validator;
|
2023-11-09 20:33:44 +08:00
|
|
|
$container->get(Middleware::class)->set($class, $method, $middleware);
|
2023-11-09 17:04:46 +08:00
|
|
|
|
|
|
|
|
return $validator->getFormData();
|
|
|
|
|
}
|
2023-04-16 03:38:47 +08:00
|
|
|
|
2023-12-04 21:17:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ReflectionNamedType|ReflectionUnionType $reflectionProperty
|
|
|
|
|
* @param object $object
|
|
|
|
|
* @param string $property
|
|
|
|
|
* @return void
|
2023-12-12 15:35:35 +08:00
|
|
|
* @throws
|
2023-12-04 21:17:04 +08:00
|
|
|
*/
|
|
|
|
|
private function insertDefaultValue(ReflectionNamedType|ReflectionUnionType $reflectionProperty, object $object, string $property): void
|
|
|
|
|
{
|
|
|
|
|
if ($reflectionProperty->allowsNull()) {
|
|
|
|
|
$object->{$property} = null;
|
|
|
|
|
} else if ($reflectionProperty instanceof ReflectionUnionType) {
|
|
|
|
|
$object->{$property} = $this->defaultValue($reflectionProperty->getTypes()[0]);
|
|
|
|
|
} else {
|
|
|
|
|
$object->{$property} = $this->defaultValue($reflectionProperty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ReflectionNamedType $type
|
|
|
|
|
* @return array|false|int|string
|
2023-12-12 15:35:35 +08:00
|
|
|
* @throws
|
2023-12-04 21:17:04 +08:00
|
|
|
*/
|
|
|
|
|
private function defaultValue(ReflectionNamedType $type): array|false|int|string
|
|
|
|
|
{
|
|
|
|
|
return match ($type->getName()) {
|
|
|
|
|
'array' => [],
|
|
|
|
|
'int' => 0,
|
|
|
|
|
'bool' => false,
|
|
|
|
|
'string' => '',
|
2023-12-04 21:19:32 +08:00
|
|
|
default => throw new Exception('暂不支持的类型')
|
2023-12-04 21:17:04 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-16 03:38:47 +08:00
|
|
|
}
|