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