eee
This commit is contained in:
+21
-15
@@ -13,6 +13,7 @@ use Kiri\Router\Validator\Types\IntProxy;
|
|||||||
use Kiri\Router\Validator\Types\MixedProxy;
|
use Kiri\Router\Validator\Types\MixedProxy;
|
||||||
use Kiri\Router\Validator\Types\StringProxy;
|
use Kiri\Router\Validator\Types\StringProxy;
|
||||||
use Kiri\Router\Validator\Types\TypesProxy;
|
use Kiri\Router\Validator\Types\TypesProxy;
|
||||||
|
use Kiri\Server\ServerInterface;
|
||||||
use ReflectionNamedType;
|
use ReflectionNamedType;
|
||||||
use ReflectionUnionType;
|
use ReflectionUnionType;
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ class BindForm implements InjectParameterInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$typeProxy = $this->_typeValidator($property);
|
$typeProxy = $this->_typeValidator($property);
|
||||||
$validator->addRule($property->getName(), [$typeProxy, false]);
|
$validator->addRule($property->getName(), [$typeProxy, false]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,28 +73,32 @@ class BindForm implements InjectParameterInterface
|
|||||||
private function _typeValidator(\ReflectionProperty $property): TypesProxy
|
private function _typeValidator(\ReflectionProperty $property): TypesProxy
|
||||||
{
|
{
|
||||||
$getType = $property->getType();
|
$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) {
|
if (!$getType instanceof ReflectionUnionType) {
|
||||||
return \Kiri::createObject(array_merge($array, [
|
$array = array_merge($array, ['class' => $this->_typeProxy($getType)]);
|
||||||
'class' => $this->_typeProxy($getType)
|
} else {
|
||||||
]));
|
$types = [];
|
||||||
|
foreach ($getType->getTypes() as $type) {
|
||||||
|
$types[] = $type->getName();
|
||||||
|
}
|
||||||
|
$array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]);
|
||||||
}
|
}
|
||||||
$types = [];
|
return \Kiri::createObject($array);
|
||||||
foreach ($getType->getTypes() as $type) {
|
|
||||||
$types[] = $type->getName();
|
|
||||||
}
|
|
||||||
return \Kiri::createObject(array_merge($array, [
|
|
||||||
'types' => $types,
|
|
||||||
'class' => MixedProxy::class
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionNamedType $type
|
* @param ReflectionNamedType $type
|
||||||
* @return string
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
private function _typeProxy(ReflectionNamedType $type): string
|
private function _typeProxy(ReflectionNamedType $type): ?string
|
||||||
{
|
{
|
||||||
return match ($type->getName()) {
|
return match ($type->getName()) {
|
||||||
'array' => ArrayProxy::class,
|
'array' => ArrayProxy::class,
|
||||||
@@ -101,6 +106,7 @@ class BindForm implements InjectParameterInterface
|
|||||||
'float' => FloatProxy::class,
|
'float' => FloatProxy::class,
|
||||||
'int' => IntProxy::class,
|
'int' => IntProxy::class,
|
||||||
'string' => StringProxy::class,
|
'string' => StringProxy::class,
|
||||||
|
default => null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,8 @@ class Validator
|
|||||||
$this->rules[$name] = [];
|
$this->rules[$name] = [];
|
||||||
}
|
}
|
||||||
foreach ($rule as $item) {
|
foreach ($rule as $item) {
|
||||||
[$dispatch, $isFirst] = $item;
|
$isFirst = array_pop($item);
|
||||||
|
$dispatch = count($item) > 1 ? $item : [$item[0], 'dispatch'];
|
||||||
if ($isFirst) {
|
if ($isFirst) {
|
||||||
array_unshift($this->rules[$name], $dispatch);
|
array_unshift($this->rules[$name], $dispatch);
|
||||||
} else {
|
} else {
|
||||||
@@ -104,24 +105,25 @@ class Validator
|
|||||||
}
|
}
|
||||||
$params = !$request->isPost() ? $request->getQueryParams() : $request->getParsedBody();
|
$params = !$request->isPost() ? $request->getQueryParams() : $request->getParsedBody();
|
||||||
foreach ($this->rules as $name => $rules) {
|
foreach ($this->rules as $name => $rules) {
|
||||||
/** @var TypesProxy $typeValidator */
|
/** @var array<array<TypesProxy,string>> $typeValidator */
|
||||||
|
$typeValidator = array_pop($rules);
|
||||||
if (!isset($params[$name])) {
|
if (!isset($params[$name])) {
|
||||||
if ($rules[0] instanceof RequiredValidatorFilter) {
|
if ($rules[0] instanceof RequiredValidatorFilter) {
|
||||||
return $this->addError('The request field ' . $name . ' is mandatory and indispensable');
|
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');
|
return $this->addError('The request field ' . $name . ' parameter cannot be null');
|
||||||
}
|
}
|
||||||
|
$params[$name] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$typeValidator = array_pop($rules);
|
if (!call_user_func($typeValidator, $this->formData, $name, $params[$name])) {
|
||||||
if (!$typeValidator->dispatch($this->formData, $name, $params[$name])) {
|
|
||||||
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
|
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var RValidator $rule */
|
/** @var array $rule */
|
||||||
foreach ($rules as $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');
|
return $this->addError('Request field ' . $name . ' value format error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user