This commit is contained in:
2023-12-15 16:33:36 +08:00
parent 085cb64241
commit bbe60d9ea2
2 changed files with 30 additions and 22 deletions
+16 -10
View File
@@ -13,6 +13,7 @@ use Kiri\Router\Validator\Types\IntProxy;
use Kiri\Router\Validator\Types\MixedProxy;
use Kiri\Router\Validator\Types\StringProxy;
use Kiri\Router\Validator\Types\TypesProxy;
use Kiri\Server\ServerInterface;
use ReflectionNamedType;
use ReflectionUnionType;
@@ -72,28 +73,32 @@ class BindForm implements InjectParameterInterface
private function _typeValidator(\ReflectionProperty $property): TypesProxy
{
$getType = $property->getType();
if (is_null($getType)) {
$service = \Kiri::getDi();
if ($service->has(ServerInterface::class)) {
$service->get(ServerInterface::class)->shutdown();
}
throw new Exception('Field ' . $property->getDeclaringClass()->getName() . '::' . $property->getName() . ' must have a numerical type set.');
}
$array = ['allowsNull' => $property->getType()->allowsNull()];
if (!$getType instanceof ReflectionUnionType) {
return \Kiri::createObject(array_merge($array, [
'class' => $this->_typeProxy($getType)
]));
}
$array = array_merge($array, ['class' => $this->_typeProxy($getType)]);
} else {
$types = [];
foreach ($getType->getTypes() as $type) {
$types[] = $type->getName();
}
return \Kiri::createObject(array_merge($array, [
'types' => $types,
'class' => MixedProxy::class
]));
$array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]);
}
return \Kiri::createObject($array);
}
/**
* @param ReflectionNamedType $type
* @return string
* @return string|null
*/
private function _typeProxy(ReflectionNamedType $type): string
private function _typeProxy(ReflectionNamedType $type): ?string
{
return match ($type->getName()) {
'array' => ArrayProxy::class,
@@ -101,6 +106,7 @@ class BindForm implements InjectParameterInterface
'float' => FloatProxy::class,
'int' => IntProxy::class,
'string' => StringProxy::class,
default => null
};
}
+9 -7
View File
@@ -82,7 +82,8 @@ class Validator
$this->rules[$name] = [];
}
foreach ($rule as $item) {
[$dispatch, $isFirst] = $item;
$isFirst = array_pop($item);
$dispatch = count($item) > 1 ? $item : [$item[0], 'dispatch'];
if ($isFirst) {
array_unshift($this->rules[$name], $dispatch);
} else {
@@ -104,24 +105,25 @@ class Validator
}
$params = !$request->isPost() ? $request->getQueryParams() : $request->getParsedBody();
foreach ($this->rules as $name => $rules) {
/** @var TypesProxy $typeValidator */
/** @var array<array<TypesProxy,string>> $typeValidator */
$typeValidator = array_pop($rules);
if (!isset($params[$name])) {
if ($rules[0] instanceof RequiredValidatorFilter) {
return $this->addError('The request field ' . $name . ' is mandatory and indispensable');
}
if (!$typeValidator->allowsNull) {
if (!$typeValidator[0]->allowsNull) {
return $this->addError('The request field ' . $name . ' parameter cannot be null');
}
$params[$name] = null;
}
$typeValidator = array_pop($rules);
if (!$typeValidator->dispatch($this->formData, $name, $params[$name])) {
if (!call_user_func($typeValidator, $this->formData, $name, $params[$name])) {
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
}
/** @var RValidator $rule */
/** @var array $rule */
foreach ($rules as $rule) {
if (!$rule->dispatch($this->formData->{$name})) {
if (!call_user_func($rule, $this->formData->{$name})) {
return $this->addError('Request field ' . $name . ' value format error');
}
}