eee
This commit is contained in:
+38
-30
@@ -5,9 +5,14 @@ namespace Kiri\Router\Validator;
|
||||
use Exception;
|
||||
use Kiri\Di\Interface\InjectParameterInterface;
|
||||
use Kiri\Router\Base\Middleware;
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
use Kiri\Router\Validator\Inject\Binding;
|
||||
use ReflectionException;
|
||||
use Kiri\Router\Validator\RequestFilter\RequestFilterInterface;
|
||||
use Kiri\Router\Validator\Types\ArrayProxy;
|
||||
use Kiri\Router\Validator\Types\BoolProxy;
|
||||
use Kiri\Router\Validator\Types\FloatProxy;
|
||||
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 ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
|
||||
@@ -42,14 +47,13 @@ class BindForm implements InjectParameterInterface
|
||||
continue;
|
||||
}
|
||||
$rule = \inject($attribute->newInstance());
|
||||
if ($rule instanceof ValidatorInterface) {
|
||||
$validator->addRule($property->getName(), $rule);
|
||||
if ($rule instanceof RequestFilterInterface) {
|
||||
$validator->addRule($property->getName(), $rule->dispatch($object, $property->getName()));
|
||||
}
|
||||
}
|
||||
$validator->setTypes($property->getName(), $property->getType());
|
||||
if (!$property->hasDefaultValue()) {
|
||||
$this->insertDefaultValue($property->getType(), $object, $property->getName());
|
||||
}
|
||||
|
||||
$typeProxy = $this->_typeValidator($property);
|
||||
$validator->addRule($property->getName(), [$typeProxy, false]);
|
||||
}
|
||||
|
||||
$middleware = \instance(ValidatorMiddleware::class);
|
||||
@@ -61,39 +65,43 @@ class BindForm implements InjectParameterInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionNamedType|ReflectionUnionType $reflectionProperty
|
||||
* @param object $object
|
||||
* @param string $property
|
||||
* @return void
|
||||
* @throws
|
||||
* @param \ReflectionProperty $property
|
||||
* @return object
|
||||
* @throws Exception
|
||||
*/
|
||||
private function insertDefaultValue(ReflectionNamedType|ReflectionUnionType $reflectionProperty, object $object, string $property): void
|
||||
private function _typeValidator(\ReflectionProperty $property): TypesProxy
|
||||
{
|
||||
if ($reflectionProperty->allowsNull()) {
|
||||
$object->{$property} = null;
|
||||
} else if ($reflectionProperty instanceof ReflectionUnionType) {
|
||||
$object->{$property} = $this->defaultValue($reflectionProperty->getTypes()[0]);
|
||||
} else {
|
||||
$object->{$property} = $this->defaultValue($reflectionProperty);
|
||||
$getType = $property->getType();
|
||||
$array = ['allowsNull' => $property->getType()->allowsNull()];
|
||||
if (!$getType instanceof ReflectionUnionType) {
|
||||
return \Kiri::createObject(array_merge($array, [
|
||||
'class' => $this->_typeProxy($getType)
|
||||
]));
|
||||
}
|
||||
$types = [];
|
||||
foreach ($getType->getTypes() as $type) {
|
||||
$types[] = $type->getName();
|
||||
}
|
||||
return \Kiri::createObject(array_merge($array, [
|
||||
'types' => $types,
|
||||
'class' => MixedProxy::class
|
||||
]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionNamedType $type
|
||||
* @return array|false|int|string
|
||||
* @throws
|
||||
* @return string
|
||||
*/
|
||||
private function defaultValue(ReflectionNamedType $type): array|false|int|string
|
||||
private function _typeProxy(ReflectionNamedType $type): string
|
||||
{
|
||||
return match ($type->getName()) {
|
||||
'array' => [],
|
||||
'int' => 0,
|
||||
'bool' => false,
|
||||
'string' => '',
|
||||
default => throw new Exception('暂不支持的类型')
|
||||
'array' => ArrayProxy::class,
|
||||
'bool' => BoolProxy::class,
|
||||
'float' => FloatProxy::class,
|
||||
'int' => IntProxy::class,
|
||||
'string' => StringProxy::class,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
use Kiri;
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
use Kiri\Router\Validator\RequestFilter\BetweenValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\InValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\LengthValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\MaxLengthValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\MaxValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\MinLengthValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\MinValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\NotBetweenValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\NotInValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\RequiredValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\RoundValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\MustValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\EmailValidatorFilter;
|
||||
use Kiri\Router\Validator\RequestFilter\RequestFilterInterface;
|
||||
use Kiri\Router\Validator\RequestFilter\PhoneValidatorFilter;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Binding implements RequestFilterInterface
|
||||
{
|
||||
|
||||
|
||||
const array TYPES = [
|
||||
'required' => ['class' => RequiredValidatorFilter::class],
|
||||
'length' => ['class' => LengthValidatorFilter::class],
|
||||
'minLength' => ['class' => MinLengthValidatorFilter::class],
|
||||
'maxLength' => ['class' => MaxLengthValidatorFilter::class],
|
||||
'in' => ['class' => InValidatorFilter::class],
|
||||
'notIn' => ['class' => NotInValidatorFilter::class],
|
||||
'between' => ['class' => BetweenValidatorFilter::class],
|
||||
'notBetween' => ['class' => NotBetweenValidatorFilter::class],
|
||||
'max' => ['class' => MaxValidatorFilter::class],
|
||||
'min' => ['class' => MinValidatorFilter::class],
|
||||
'round' => ['class' => RoundValidatorFilter::class],
|
||||
'must' => ['class' => MustValidatorFilter::class],
|
||||
'email' => ['class' => EmailValidatorFilter::class],
|
||||
'phone' => ['class' => PhoneValidatorFilter::class],
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param array $rules
|
||||
* @param mixed $defaultValue
|
||||
*/
|
||||
public function __construct(public string $field, public array $rules, public mixed $defaultValue = null)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
* @param string $property
|
||||
* @return array
|
||||
*/
|
||||
public function dispatch(object $class, string $property): array
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
$array = [];
|
||||
foreach ($this->rules as $key => $rule) {
|
||||
if (is_string($key)) {
|
||||
$array[] = $this->getValidator($key, $rule);
|
||||
} else if (method_exists($this, $rule)) {
|
||||
$array[] = [$class, $rule, false];
|
||||
} else {
|
||||
$array[] = $this->getValidator($key, $rule);
|
||||
}
|
||||
}
|
||||
if (!is_null($this->defaultValue)) {
|
||||
$class->{$property} = $this->defaultValue;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $rule
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
protected function getValidator($key, $rule): array
|
||||
{
|
||||
$class = array_merge(self::TYPES[$key], ['value' => $rule, 'field' => $key]);
|
||||
$isFirst = false;
|
||||
if ($class['class'] === RequiredValidatorFilter::class) {
|
||||
$isFirst = true;
|
||||
}
|
||||
return [Kiri::createObject($class), $isFirst];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
|
||||
abstract class FormBase implements \Arrayable, \JsonSerializable, \Stringable
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Ignoring
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Binding
|
||||
{
|
||||
|
||||
|
||||
public function __construct(public string $field)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return void
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): void
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Email implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return filter_var($data, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Ignore implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class In implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return in_array($data, $this->value);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Length implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return mb_strlen((string)$data) === $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Matching implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function __construct(readonly public string $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data !== null) {
|
||||
return preg_match('/' . preg_quote($this->value) . '/', $data);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Max implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return $data <= $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class MaxLength implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return mb_strlen((string)$data) <= $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Min implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return $data >= $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class MinLength implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return mb_strlen((string)$data) <= $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Must implements ValidatorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __construct(readonly public mixed $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
return $data === $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotEmpty implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
return !empty($data);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotIn implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return !in_array($data, $this->value);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotNull implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
return !($data === null);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Phone implements ValidatorInterface
|
||||
{
|
||||
const REG = '/^1[356789]\d{9}$/';
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data == null || !is_numeric($data)) {
|
||||
return false;
|
||||
}
|
||||
return preg_match(self::REG, $data);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Required implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
return !($data === null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\Inject;
|
||||
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Round implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param object $class
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $data, object $class): bool
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
return round((float)$data, $this->value) === $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class BetweenValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
[$min, $max] = $this->value;
|
||||
|
||||
return $value >= $min && $value <= $max;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
use function Symfony\Component\String\s;
|
||||
|
||||
class EmailValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return filter_var((string)$value, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class InValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return in_array($value, $this->value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class LengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return mb_strlen((string)$value) === $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class MaxLengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return count($value) <= $this->value;
|
||||
}
|
||||
return mb_strlen((string)$value) <= $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class MaxValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return (float)$value <= $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class MinLengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return count($value) >= $this->value;
|
||||
}
|
||||
return mb_strlen((string)$value) >= $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class MinValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return (float)$value >= $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class MustValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return $value === $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class NotBetweenValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
[$min, $max] = $this->value;
|
||||
|
||||
return $value <= $min || $value >= $max;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class NotInValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return !in_array($value, $this->value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
|
||||
class PhoneValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
const string REG = '/^1[356789]\d{9}$/';
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return preg_match(self::REG, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
interface RequestFilterInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
* @param string $property
|
||||
* @return array
|
||||
*/
|
||||
public function dispatch(object $class, string $property): array;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class RequiredValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class RoundValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(mixed $value): bool
|
||||
{
|
||||
return round((float)$value, $this->value) === $value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
abstract class ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
public mixed $value;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $field;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function dispatch(mixed $value): bool;
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,20 @@
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
class ArrayProxy
|
||||
|
||||
class ArrayProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
return $value == ($form->{$field} = $value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
class BoolProxy
|
||||
|
||||
|
||||
class BoolProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
if (in_array($value, ['false', 'true'])) {
|
||||
$form->{$field} = $value === 'true';
|
||||
} else if (in_array($value, ['0', '1'])) {
|
||||
$form->{$field} = $value === '1';
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,17 @@
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
class FloatProxy
|
||||
class FloatProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
return $value == ($form->{$field} = (float)$value);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,19 @@
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
class IntProxy
|
||||
class IntProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
return $value == ($form->{$field} = (int)$value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
|
||||
class MixedProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
public array $types = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
try {
|
||||
return $value == ($form->{$field} = $value);
|
||||
} catch (\Throwable $throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,19 @@
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
class StringProxy
|
||||
|
||||
class StringProxy extends TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
return $value == ($form->{$field} = (string)$value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator\Types;
|
||||
|
||||
abstract class TypesProxy
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public bool $allowsNull = false;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $form
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function dispatch(object $form, string $field, mixed $value): bool;
|
||||
|
||||
}
|
||||
+38
-78
@@ -6,11 +6,13 @@ namespace Kiri\Router\Validator;
|
||||
use Exception;
|
||||
use Kiri\Router\Constrict\ConstrictRequest;
|
||||
use Kiri\Router\Interface\ValidatorInterface;
|
||||
use Kiri\Router\Validator\RequestFilter\RequiredValidatorFilter;
|
||||
use Kiri\Router\Validator\Types\TypesProxy;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ReflectionException;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
use Kiri\Router\Validator\RequestFilter\ValidatorFilter as RValidator;
|
||||
|
||||
|
||||
/**
|
||||
@@ -21,7 +23,7 @@ class Validator
|
||||
|
||||
|
||||
/**
|
||||
* @var ValidatorInterface[]
|
||||
* @var array<array<ValidatorInterface|TypesProxy>>
|
||||
*/
|
||||
protected array $rules = [];
|
||||
|
||||
@@ -38,6 +40,12 @@ class Validator
|
||||
protected object $formData;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $ignoring = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -55,17 +63,6 @@ class Validator
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
* @param ReflectionNamedType|ReflectionUnionType $types
|
||||
* @return void
|
||||
*/
|
||||
public function setTypes(string $property, ReflectionNamedType|ReflectionUnionType $types): void
|
||||
{
|
||||
$this->types[$property] = $types;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*/
|
||||
@@ -76,15 +73,22 @@ class Validator
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param ValidatorInterface $rule
|
||||
* @param array $rule
|
||||
* @return void
|
||||
*/
|
||||
public function addRule(string $name, ValidatorInterface $rule): void
|
||||
public function addRule(string $name, array $rule): void
|
||||
{
|
||||
if (!isset($this->rules[$name])) {
|
||||
$this->rules[$name] = [];
|
||||
}
|
||||
$this->rules[$name][] = $rule;
|
||||
foreach ($rule as $item) {
|
||||
[$dispatch, $isFirst] = $item;
|
||||
if ($isFirst) {
|
||||
array_unshift($this->rules[$name], $dispatch);
|
||||
} else {
|
||||
$this->rules[$name][] = $dispatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,84 +103,40 @@ class Validator
|
||||
return false;
|
||||
}
|
||||
$params = !$request->isPost() ? $request->getQueryParams() : $request->getParsedBody();
|
||||
|
||||
foreach ($params as $name => $value) {
|
||||
if (!isset($this->types[$name])) {
|
||||
continue;
|
||||
}
|
||||
$rules = $this->rules[$name] ?? [];
|
||||
foreach ($rules as $item) {
|
||||
/** @var ValidatorInterface $item */
|
||||
if (!$item->dispatch($value, $this->formData)) {
|
||||
return $this->addError($name);
|
||||
foreach ($this->rules as $name => $rules) {
|
||||
/** @var TypesProxy $typeValidator */
|
||||
if (!isset($params[$name])) {
|
||||
if ($rules[0] instanceof RequiredValidatorFilter) {
|
||||
return $this->addError('The request field ' . $name . ' is mandatory and indispensable');
|
||||
}
|
||||
if (!$typeValidator->allowsNull) {
|
||||
return $this->addError('The request field ' . $name . ' parameter cannot be null');
|
||||
}
|
||||
}
|
||||
|
||||
/** @var ReflectionNamedType|ReflectionUnionType $property */
|
||||
$property = $this->types[$name];
|
||||
if ($property instanceof ReflectionUnionType) {
|
||||
foreach ($property->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;
|
||||
}
|
||||
$typeValidator = array_pop($rules);
|
||||
if (!$typeValidator->dispatch($this->formData, $name, $params[$name])) {
|
||||
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
|
||||
}
|
||||
|
||||
/** @var RValidator $rule */
|
||||
foreach ($rules as $rule) {
|
||||
if (!$rule->dispatch($this->formData->{$name})) {
|
||||
return $this->addError('Request field ' . $name . ' value format error');
|
||||
}
|
||||
if ($this->formData->{$name} != $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
|
||||
};
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionNamedType $property
|
||||
* @param string $name
|
||||
* @param string|array $value
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function arrayCheck(ReflectionNamedType $property, string $name, string|array $value): ?array
|
||||
{
|
||||
if (empty($value) || !is_array($value)) {
|
||||
if ($property->allowsNull()) {
|
||||
return null;
|
||||
}
|
||||
throw new Exception('Cannot assign non null values to property ' . $name . ' of type array');
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @return bool
|
||||
*/
|
||||
private function addError($field): bool
|
||||
{
|
||||
$this->message = 'Field ' . $field . ' value format fail.';
|
||||
$this->message = $field;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user