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-12-15 16:14:39 +08:00
|
|
|
use Kiri\Router\Validator\RequestFilter\RequestFilterInterface;
|
|
|
|
|
use Kiri\Router\Validator\Types\ArrayProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\BoolProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\FloatProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\IntProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\MixedProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\StringProxy;
|
|
|
|
|
use Kiri\Router\Validator\Types\TypesProxy;
|
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-12-15 16:14:39 +08:00
|
|
|
if ($rule instanceof RequestFilterInterface) {
|
|
|
|
|
$validator->addRule($property->getName(), $rule->dispatch($object, $property->getName()));
|
2023-11-09 17:04:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-15 16:14:39 +08:00
|
|
|
|
|
|
|
|
$typeProxy = $this->_typeValidator($property);
|
|
|
|
|
$validator->addRule($property->getName(), [$typeProxy, false]);
|
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
|
|
|
|
|
|
|
|
/**
|
2023-12-15 16:14:39 +08:00
|
|
|
* @param \ReflectionProperty $property
|
|
|
|
|
* @return object
|
|
|
|
|
* @throws Exception
|
2023-12-04 21:17:04 +08:00
|
|
|
*/
|
2023-12-15 16:14:39 +08:00
|
|
|
private function _typeValidator(\ReflectionProperty $property): TypesProxy
|
2023-12-04 21:17:04 +08:00
|
|
|
{
|
2023-12-15 16:14:39 +08:00
|
|
|
$getType = $property->getType();
|
|
|
|
|
$array = ['allowsNull' => $property->getType()->allowsNull()];
|
|
|
|
|
if (!$getType instanceof ReflectionUnionType) {
|
|
|
|
|
return \Kiri::createObject(array_merge($array, [
|
|
|
|
|
'class' => $this->_typeProxy($getType)
|
|
|
|
|
]));
|
2023-12-04 21:17:04 +08:00
|
|
|
}
|
2023-12-15 16:14:39 +08:00
|
|
|
$types = [];
|
|
|
|
|
foreach ($getType->getTypes() as $type) {
|
|
|
|
|
$types[] = $type->getName();
|
|
|
|
|
}
|
|
|
|
|
return \Kiri::createObject(array_merge($array, [
|
|
|
|
|
'types' => $types,
|
|
|
|
|
'class' => MixedProxy::class
|
|
|
|
|
]));
|
2023-12-04 21:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ReflectionNamedType $type
|
2023-12-15 16:14:39 +08:00
|
|
|
* @return string
|
2023-12-04 21:17:04 +08:00
|
|
|
*/
|
2023-12-15 16:14:39 +08:00
|
|
|
private function _typeProxy(ReflectionNamedType $type): string
|
2023-12-04 21:17:04 +08:00
|
|
|
{
|
|
|
|
|
return match ($type->getName()) {
|
2023-12-15 16:14:39 +08:00
|
|
|
'array' => ArrayProxy::class,
|
|
|
|
|
'bool' => BoolProxy::class,
|
|
|
|
|
'float' => FloatProxy::class,
|
|
|
|
|
'int' => IntProxy::class,
|
|
|
|
|
'string' => StringProxy::class,
|
2023-12-04 21:17:04 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 03:38:47 +08:00
|
|
|
}
|