This commit is contained in:
2023-12-13 18:59:56 +08:00
parent 75071b85af
commit 8aa4752065
+22 -1
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator;
use Exception;
use Kiri\Router\Constrict\ConstrictRequest;
use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
@@ -134,13 +135,14 @@ class Validator
}
}
if ($this->formData->{$name} != $value) {
throw new \Exception('Fail type value.');
throw new Exception('Fail type value.');
}
} else {
$this->formData->{$name} = match ($property->getName()) {
'int' => (int)$value,
'float' => (float)$value,
'bool' => $value == 'true',
'array' => $this->arrayCheck($property, $name, $value),
default => $value
};
}
@@ -149,6 +151,25 @@ class Validator
}
/**
* @param ReflectionNamedType $property
* @param string $name
* @param string|array $value
* @return array
* @throws Exception
*/
protected function arrayCheck(ReflectionNamedType $property, string $name, string|array $value): array
{
if (empty($value) || !is_array($value)) {
if ($property->allowsNull()) {
$this->formData->{$name} = null;
}
throw new Exception('TypeError Cannot assign string to property ' . $name . ' of type array');
}
return $value;
}
/**
* @param $field
* @return bool