eee
This commit is contained in:
@@ -7,6 +7,8 @@ use Kiri\Di\Interface\InjectParameterInterface;
|
||||
use Kiri\Router\Base\Middleware;
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
use ReflectionException;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PARAMETER)]
|
||||
class BindForm implements InjectParameterInterface
|
||||
@@ -33,7 +35,7 @@ class BindForm implements InjectParameterInterface
|
||||
$validator = new Validator();
|
||||
$container = \Kiri::getDi();
|
||||
$reflect = $container->getReflectionClass($this->formValidate);
|
||||
$validator->setFormData($reflect->newInstanceWithoutConstructor());
|
||||
$object = $validator->setFormData($reflect->newInstanceWithoutConstructor());
|
||||
foreach ($reflect->getProperties() as $property) {
|
||||
foreach ($property->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
@@ -44,6 +46,9 @@ class BindForm implements InjectParameterInterface
|
||||
$validator->addRule($property->getName(), $rule);
|
||||
}
|
||||
}
|
||||
if (!$property->hasDefaultValue()) {
|
||||
$this->insertDefaultValue($property->getType(), $object, $property->getName());
|
||||
}
|
||||
}
|
||||
|
||||
$middleware = \instance(ValidatorMiddleware::class);
|
||||
@@ -53,4 +58,41 @@ class BindForm implements InjectParameterInterface
|
||||
return $validator->getFormData();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionNamedType|ReflectionUnionType $reflectionProperty
|
||||
* @param object $object
|
||||
* @param string $property
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
*/
|
||||
private function defaultValue(ReflectionNamedType $type): array|false|int|string
|
||||
{
|
||||
return match ($type->getName()) {
|
||||
'array' => [],
|
||||
'int' => 0,
|
||||
'bool' => false,
|
||||
'string' => '',
|
||||
default => throw new Exception('暂不支持')
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -38,10 +38,12 @@ class Validator
|
||||
|
||||
/**
|
||||
* @param object $formData
|
||||
* @return object
|
||||
*/
|
||||
public function setFormData(object $formData): void
|
||||
public function setFormData(object $formData): object
|
||||
{
|
||||
$this->formData = $formData;
|
||||
return $formData;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user