diff --git a/src/Validator/BindForm.php b/src/Validator/BindForm.php index 98cca76..5ee05a9 100644 --- a/src/Validator/BindForm.php +++ b/src/Validator/BindForm.php @@ -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; @@ -52,7 +53,7 @@ class BindForm implements InjectParameterInterface } } - $typeProxy = $this->_typeValidator($property); + $typeProxy = $this->_typeValidator($property); $validator->addRule($property->getName(), [$typeProxy, false]); } @@ -72,28 +73,32 @@ class BindForm implements InjectParameterInterface private function _typeValidator(\ReflectionProperty $property): TypesProxy { $getType = $property->getType(); - $array = ['allowsNull' => $property->getType()->allowsNull()]; + 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(); + } + $array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]); } - $types = []; - foreach ($getType->getTypes() as $type) { - $types[] = $type->getName(); - } - return \Kiri::createObject(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 }; } diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index 57aa09b..9bffa6e 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -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> $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'); } }