eee
This commit is contained in:
@@ -46,6 +46,7 @@ class BindForm implements InjectParameterInterface
|
||||
$validator->addRule($property->getName(), $rule);
|
||||
}
|
||||
}
|
||||
$validator->setTypes($property->getName(), $property->getType());
|
||||
if (!$property->hasDefaultValue()) {
|
||||
$this->insertDefaultValue($property->getType(), $object, $property->getName());
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ use Kiri\Router\Interface\ValidatorInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ReflectionException;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
|
||||
|
||||
/**
|
||||
@@ -36,6 +38,12 @@ class Validator
|
||||
protected object $formData;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $types = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param object $formData
|
||||
* @return object
|
||||
@@ -47,6 +55,17 @@ class Validator
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
* @param ReflectionNamedType|ReflectionUnionType $types
|
||||
* @return void
|
||||
*/
|
||||
public function setTypes(string $property, ReflectionNamedType|ReflectionUnionType $types): void
|
||||
{
|
||||
$this->types[$property] = $types;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*/
|
||||
@@ -81,10 +100,9 @@ class Validator
|
||||
return false;
|
||||
}
|
||||
$params = !$request->getIsPost() ? $request->getQueryParams() : $request->getParsedBody();
|
||||
$method = Kiri::getDi()->getReflectionClass($this->formData::class);
|
||||
|
||||
foreach ($params as $name => $value) {
|
||||
if (!$method->hasProperty($name)) {
|
||||
if (!isset($this->types[$name])) {
|
||||
continue;
|
||||
}
|
||||
$rules = $this->rules[$name] ?? [];
|
||||
@@ -94,9 +112,11 @@ class Validator
|
||||
return $this->addError($name);
|
||||
}
|
||||
}
|
||||
$property = $method->getProperty($name);
|
||||
if ($property->getType() instanceof \ReflectionUnionType) {
|
||||
foreach ($property->getType()->getTypes() as $type) {
|
||||
|
||||
/** @var ReflectionNamedType|ReflectionUnionType $property */
|
||||
$property = $this->types[$name];
|
||||
if ($property instanceof ReflectionUnionType) {
|
||||
foreach ($property->getTypes() as $type) {
|
||||
$typeName = $type->getName();
|
||||
if ($typeName == 'string' && is_string($value)) {
|
||||
$this->formData->{$name} = $value;
|
||||
@@ -119,10 +139,10 @@ class Validator
|
||||
throw new \Exception('Fail type value.');
|
||||
}
|
||||
} else {
|
||||
$value = match ($property->getType()?->getName()) {
|
||||
$value = match ($property->getName()) {
|
||||
'int' => (int)$value,
|
||||
'float' => (float)$value,
|
||||
'bool' => $value == 'true',
|
||||
'bool' => $value == 'true',
|
||||
default => $value
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user