This commit is contained in:
2023-12-04 22:05:34 +08:00
parent fa4b2bb8d9
commit 6bcbc9e2de
+27 -2
View File
@@ -73,6 +73,7 @@ class Validator
* @param RequestInterface|ServerRequestInterface|ConstrictRequest $request * @param RequestInterface|ServerRequestInterface|ConstrictRequest $request
* @return bool * @return bool
* @throws ReflectionException * @throws ReflectionException
* @throws \Exception
*/ */
public function run(RequestInterface|ServerRequestInterface|ConstrictRequest $request): bool public function run(RequestInterface|ServerRequestInterface|ConstrictRequest $request): bool
{ {
@@ -94,10 +95,34 @@ class Validator
} }
} }
$property = $method->getProperty($name); $property = $method->getProperty($name);
if (!($property->getType() instanceof \ReflectionUnionType)) { if ($property->getType() instanceof \ReflectionUnionType) {
foreach ($property->getType()->getTypes() as $type) {
$typeName = $type->getName();
if ($typeName == 'string' && is_string($value)) {
$this->formData->{$name} = $value;
break;
} else if ($typeName == 'int' && $value == ($int = intval($value))) {
$this->formData->{$name} = $int;
break;
} else if ($typeName == 'bool' && in_array($value, ['true', 'false'])) {
$this->formData->{$name} = $value == 'true';
break;
} else if ($typeName == 'float' && $value == ($flo = floatval($value))) {
$this->formData->{$name} = $flo;
break;
} else if ($typeName == 'array' && is_array($value)) {
$this->formData->{$name} = $value;
break;
}
}
if ($this->formData->{$name} != $value) {
throw new \Exception('Fail type value.');
}
} else {
$value = match ($property->getType()?->getName()) { $value = match ($property->getType()?->getName()) {
'int' => (int)$value, 'int' => (int)$value,
'float' => (float)$value, 'float' => (float)$value,
'bool' => $value == 'true',
default => $value default => $value
}; };
} }