From d18994fde234a17b79ef7668c4683a62bf7fc339 Mon Sep 17 00:00:00 2001 From: xl Date: Fri, 7 Jul 2023 17:24:03 +0800 Subject: [PATCH] qqq --- ArrayValidator.php | 3 - DateTimeValidator.php | 3 - EmailValidator.php | 25 ++-- EmptyValidator.php | 36 ++--- EnumValidator.php | 31 ++--- IntegerValidator.php | 10 +- LengthValidator.php | 61 +++++---- RequiredValidator.php | 9 +- RuleTrait.php | 94 ++++++++++++++ TypesOfValidator.php | 84 ++++++------ Validator.php | 296 ++++++++++++++---------------------------- 11 files changed, 310 insertions(+), 342 deletions(-) create mode 100644 RuleTrait.php diff --git a/ArrayValidator.php b/ArrayValidator.php index bf23d8a..239ac84 100644 --- a/ArrayValidator.php +++ b/ArrayValidator.php @@ -29,9 +29,6 @@ class ArrayValidator extends BaseValidator { return $this->_validator($this->field, function ($field, $params) { $value = $params[$field] ?? null; - if (empty($value)) { - return true; - } if (!is_array($value)) { return $this->addError($field, 'The param :attribute must a array'); } diff --git a/DateTimeValidator.php b/DateTimeValidator.php index 310dd6f..3d015c5 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -27,9 +27,6 @@ class DateTimeValidator extends BaseValidator { return $this->_validator($this->field, function ($field, $params, $method) { $value = $params[$field] ?? null; - if (empty($value)) { - return true; - } return match ($method) { self::DATE => $this->validatorDate($field, $value), self::DATE_TIME => $this->validateDatetime($field, $value), diff --git a/EmailValidator.php b/EmailValidator.php index 445c6e4..1b1007e 100644 --- a/EmailValidator.php +++ b/EmailValidator.php @@ -13,26 +13,19 @@ namespace validator; class EmailValidator extends BaseValidator { - /** - * @return bool - * 检查是否存在 - */ + /** + * @return bool + * 检查是否存在 + * @throws \ReflectionException + */ public function trigger(): bool { return $this->_validator($this->field, function ($field, $params) { $value = $params[$field] ?? null; - if (empty($value)) { - return true; - } - $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$"; - if (!preg_match($exp, $value)) { - return $this->addError($field,'The param :attribute format error'); - } - [$account, $domain] = explode("@", $value); - if (checkdnsrr($domain, "MX")) { - return true; - } - return $this->addError($field,'The param :attribute format error'); + if (!filter_var($value,FILTER_VALIDATE_EMAIL)) { + return $this->addError($field,'The param :attribute format error'); + } + return true; }, $this->params); } diff --git a/EmptyValidator.php b/EmptyValidator.php index 17529a2..3c4be39 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -15,13 +15,13 @@ namespace validator; class EmptyValidator extends BaseValidator { - /** @var string [不能为空] */ - const CAN_NOT_EMPTY = 'not empty'; + /** @var string [不能为空] */ + const CAN_NOT_EMPTY = 'not empty'; - /** @var string [可为空, 不能为null] */ - const CAN_NOT_NULL = 'not null'; + /** @var string [可为空, 不能为null] */ + const CAN_NOT_NULL = 'not null'; - public string $method; + public string $method; /** * @return bool @@ -29,17 +29,17 @@ class EmptyValidator extends BaseValidator * 检查参数是否为NULL * @throws \ReflectionException */ - public function trigger(): bool - { - return $this->_validator($this->field, function ($field, $params, $method) { - $value = $params[$field] ?? null; - if (empty($value)) { - return $this->addError($field,':attribute not exists'); - } - return match ($method) { - self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError($field,'The :attribute can not empty.'), - default => $value !== null || $this->addError($field,'The :attribute can not empty.') - }; - }, $this->params, strtolower($this->method)); - } + public function trigger(): bool + { + return $this->_validator($this->field, function ($field, $params, $method) { + $value = $params[$field] ?? null; + if ($value === null) { + return $this->addError($field, 'The :attribute can not null.'); + } + if (empty($value)) { + return $this->addError($field, 'The :attribute can not empty.'); + } + return true; + }, $this->params, strtolower($this->method)); + } } diff --git a/EnumValidator.php b/EnumValidator.php index a1873fd..0d44317 100644 --- a/EnumValidator.php +++ b/EnumValidator.php @@ -12,28 +12,23 @@ namespace validator; class EnumValidator extends BaseValidator { - public array $value = []; + public array $value = []; /** * @return bool * @throws \ReflectionException */ - public function trigger(): bool - { - return $this->_validator($this->field, function ($field, $params, $values) { - $value = $params[$field] ?? null; - if (is_null($value)) { - return true; - } - if ($value === '') { - return $this->addError($field, 'The param :attribute value con\'t empty.'); - } - if (!in_array($value, $values)) { - $message = 'The param :attribute value(' . $value . ') only in ' . implode(',', $values); - return $this->addError($field, $message); - } - return true; - }, $this->params, $this->value); - } + public function trigger(): bool + { + return $this->_validator($this->field, function ($field, $params, $values) { + $value = $params[$field] ?? null; + if (in_array($value, $values)) { + return true; + } + $message = 'The param :attribute value(' . $value . ') only in ' . implode(',', $values); + return $this->addError($field, $message); + + }, $this->params, $this->value); + } } diff --git a/IntegerValidator.php b/IntegerValidator.php index 7686687..f4e175c 100644 --- a/IntegerValidator.php +++ b/IntegerValidator.php @@ -19,16 +19,14 @@ class IntegerValidator extends BaseValidator public ?int $value = null; private string $type = ''; - /** - * @return bool - */ + /** + * @return bool + * @throws \ReflectionException + */ public function trigger(): bool { return $this->_validator($this->field, function ($field, $params, $origin, $type) { $value = $params[$field] ?? null; - if (empty($value)) { - return true; - } if ($type !== self::MIN && $value < $origin) { return $this->addError($field,'The ' . $field . ' cannot be less than the default value.'); } diff --git a/LengthValidator.php b/LengthValidator.php index cf548a9..e6d03f8 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -10,6 +10,8 @@ declare(strict_types=1); namespace validator; +use ReflectionException; + class LengthValidator extends BaseValidator { @@ -20,20 +22,14 @@ class LengthValidator extends BaseValidator public int $value; - /** - * @return bool - */ + /** + * @return bool + * @throws ReflectionException + */ public function trigger(): bool { return $this->_validator($this->field, function ($field, $params, $method, $length) { $value = $params[$field] ?? null; - if (empty($value)) { - if ($method != self::MAX_LENGTH) { - return $this->addError($field, 'The param :attribute not exists'); - } else { - return TRUE; - } - } return match ($method) { self::MAX_LENGTH => $this->maxLength($field, (string)$value), self::MIN_LENGTH => $this->minLength($field, (string)$value), @@ -42,13 +38,14 @@ class LengthValidator extends BaseValidator }, $this->params, strtolower($this->method), $this->value); } - /** - * @param $field - * @param $value - * @return bool - * - * 效验长度是否大于最大长度 - */ + /** + * @param $field + * @param $value + * @return bool + * + * 效验长度是否大于最大长度 + * @throws ReflectionException + */ private function maxLength($field, $value): bool { if (is_array($value)) { @@ -66,13 +63,14 @@ class LengthValidator extends BaseValidator return TRUE; } - /** - * @param $field - * @param $value - * @return bool - * - * 效验长度是否小于最小长度 - */ + /** + * @param $field + * @param $value + * @return bool + * + * 效验长度是否小于最小长度 + * @throws ReflectionException + */ private function minLength($field, $value): bool { if (is_array($value)) { @@ -90,13 +88,14 @@ class LengthValidator extends BaseValidator return TRUE; } - /** - * @param $field - * @param $value - * @return bool - * - * 效验长度是否小于最小长度 - */ + /** + * @param $field + * @param $value + * @return bool + * + * 效验长度是否小于最小长度 + * @throws ReflectionException + */ private function defaultLength($field, $value): bool { if (is_array($value)) { diff --git a/RequiredValidator.php b/RequiredValidator.php index b542710..a6c48ac 100644 --- a/RequiredValidator.php +++ b/RequiredValidator.php @@ -13,10 +13,11 @@ namespace validator; class RequiredValidator extends BaseValidator { - /** - * @return bool - * 检查是否存在 - */ + /** + * @return bool + * 检查是否存在 + * @throws \ReflectionException + */ public function trigger(): bool { return $this->_validator($this->field, function ($field, $params) { diff --git a/RuleTrait.php b/RuleTrait.php new file mode 100644 index 0000000..694632a --- /dev/null +++ b/RuleTrait.php @@ -0,0 +1,94 @@ + [ + 'class' => EmptyValidator::class, + 'method' => EmptyValidator::CAN_NOT_EMPTY, + ], + 'not null' => [ + 'class' => EmptyValidator::class, + 'method' => EmptyValidator::CAN_NOT_NULL, + ], + 'required' => [ + 'class' => RequiredValidator::class, + ], + 'enum' => [ + 'class' => EnumValidator::class, + ], + 'unique' => [ + 'class' => UniqueValidator::class, + ], + 'datetime' => [ + 'class' => DateTimeValidator::class, + 'method' => DateTimeValidator::DATE_TIME, + ], + 'date' => [ + 'class' => DateTimeValidator::class, + 'method' => DateTimeValidator::DATE, + ], + 'time' => [ + 'class' => DateTimeValidator::class, + 'method' => DateTimeValidator::TIME, + ], + 'timestamp' => [ + 'class' => DateTimeValidator::class, + 'method' => DateTimeValidator::STR_TO_TIME, + ], + 'string' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::STRING, + ], + 'int' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::INTEGER, + ], + 'min' => [ + 'class' => IntegerValidator::class + ], + 'max' => [ + 'class' => IntegerValidator::class + ], + 'json' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::JSON, + ], + 'float' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::FLOAT, + ], + 'array' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::ARRAY, + ], + 'serialize' => [ + 'class' => TypesOfValidator::class, + 'method' => TypesOfValidator::SERIALIZE, + ], + 'maxlength' => [ + 'class' => LengthValidator::class, + 'method' => 'max', + ], + 'minlength' => [ + 'class' => LengthValidator::class, + 'method' => 'min', + ], + 'email' => [ + 'class' => EmailValidator::class, + 'method' => 'email', + ], + 'length' => [ + 'class' => LengthValidator::class, + 'method' => 'default', + ], + 'round' => [ + 'class' => RoundValidator::class, + ], + ]; +} \ No newline at end of file diff --git a/TypesOfValidator.php b/TypesOfValidator.php index 08d34cb..93ddf13 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -48,50 +48,45 @@ class TypesOfValidator extends BaseValidator return true; } $value = $params[$field] ?? null; - if (is_null($value)) { - return true; - } + return $this->{$method . 'Format'}($field, $value); }, $this->params, $this->method, $this->types); } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function jsonFormat($field, $value): bool { - if (!is_string($value) || is_numeric($value)) { - return $this->addError($field, 'The ' . $field . ' not is JSON data.'); - } if (is_null(json_decode($value))) { return $this->addError($field, 'The ' . $field . ' not is JSON data.'); } return true; } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function serializeFormat($field, $value): bool { - if (!is_string($value) || is_numeric($value)) { - return $this->addError($field, 'The ' . $field . ' not is serialize data.'); - } - if (false === swoole_unserialize($value)) { + if (false === unserialize($value)) { return $this->addError($field, 'The ' . $field . ' not is serialize data.'); } return true; } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function arrayFormat($field, $value): bool { if (!is_array($value)) { @@ -100,41 +95,40 @@ class TypesOfValidator extends BaseValidator return true; } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function stringFormat($field, $value): bool { - if (is_array($value) || is_object($value) || is_bool($value)) { + if (!is_string($value)) { return $this->addError($field, 'The ' . $field . ' not is string data.'); } return true; } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function integerFormat($field, $value): bool { - if (!is_numeric($value)) { - return $this->addError($field, 'The ' . $field . ' not is number data.'); - } if ((int)$value != $value) { return $this->addError($field, 'The ' . $field . ' not is number data.'); } - return true; } - /** - * @param $field - * @param $value - * @return bool - */ + /** + * @param $field + * @param $value + * @return bool + * @throws \ReflectionException + */ public function floatFormat($field, $value): bool { $trim = (float)$value; diff --git a/Validator.php b/Validator.php index f588e1a..a1b10eb 100644 --- a/Validator.php +++ b/Validator.php @@ -17,216 +17,116 @@ use Kiri; class Validator extends BaseValidator { - /** @var BaseValidator[] */ - private ?array $validators = []; + use RuleTrait; - /** @var ?Validator */ - private static ?Validator $instance = null; - - protected array $classMap = [ - 'not empty' => [ - 'class' => 'validator\EmptyValidator', - 'method' => EmptyValidator::CAN_NOT_EMPTY, - ], - 'not null' => [ - 'class' => 'validator\EmptyValidator', - 'method' => EmptyValidator::CAN_NOT_NULL, - ], - 'required' => [ - 'class' => 'validator\RequiredValidator', - ], - 'enum' => [ - 'class' => 'validator\EnumValidator', - ], - 'unique' => [ - 'class' => 'validator\UniqueValidator', - ], - 'datetime' => [ - 'class' => 'validator\DatetimeValidator', - 'method' => DateTimeValidator::DATE_TIME, - ], - 'date' => [ - 'class' => 'validator\DatetimeValidator', - 'method' => DateTimeValidator::DATE, - ], - 'time' => [ - 'class' => 'validator\DatetimeValidator', - 'method' => DateTimeValidator::TIME, - ], - 'timestamp' => [ - 'class' => 'validator\DatetimeValidator', - 'method' => DateTimeValidator::STR_TO_TIME, - ], - 'string' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::STRING, - ], - 'int' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::INTEGER, - ], - 'min' => [ - 'class' => IntegerValidator::class - ], - 'max' => [ - 'class' => IntegerValidator::class - ], - 'json' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::JSON, - ], - 'float' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::FLOAT, - ], - 'array' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::ARRAY, - ], - 'serialize' => [ - 'class' => 'validator\TypesOfValidator', - 'method' => TypesOfValidator::SERIALIZE, - ], - 'maxlength' => [ - 'class' => 'validator\LengthValidator', - 'method' => 'max', - ], - 'minlength' => [ - 'class' => 'validator\LengthValidator', - 'method' => 'min', - ], - 'email' => [ - 'class' => 'validator\EmailValidator', - 'method' => 'email', - ], - 'length' => [ - 'class' => 'validator\LengthValidator', - 'method' => 'default', - ], - 'round' => [ - 'class' => 'validator\RoundValidator', - ], - ]; - - /** - * @return Validator|null - */ - public static function getInstance(): ?Validator - { - if (static::$instance == null) { - static::$instance = new Validator(); - } - return static::$instance; - } + /** @var BaseValidator[] */ + private ?array $validators = []; - /** - * @param array $params - * @param ModelInterface $model - * @return Validator - */ - public static function instance(array $params, ModelInterface $model): static - { - $validator = static::$instance; - if ($validator == null) { - $validator = static::$instance = new Validator(); - } - $validator->setParams($params); - $validator->setModel($model); - return $validator; - } + /** + * @param array $params + * @param ModelInterface $model + * @return Validator + */ + public static function instance(array $params, ModelInterface $model): static + { + $validator = new Validator(); + $validator->setParams($params); + $validator->setModel($model); + return $validator; + } - /** - * @param $field - * @param $rules - * @return $this - * @throws Exception - */ - public function make($field, $rules): static - { - if (!is_array($field)) { - $field = [$field]; - } + /** + * @param $field + * @param $rules + * @return $this + * @throws Exception + */ + public function make($field, $rules): static + { + if (!is_array($field)) { + $field = [$field]; + } - $param = $this->getParams(); - $model = $this->getModel(); + $param = $this->getParams(); + $model = $this->getModel(); - $this->createRule($field, $rules, $model, $param); + $this->createRule($field, $rules, $model, $param); - return $this; - } + return $this; + } - /** - * @param $field - * @param $rule - * @param $model - * @param $param - * @throws Exception - * ['maxLength'=>150, 'required', 'minLength' => 100] - */ - public function createRule($field, $rule, $model, $param): void - { - $define = ['field' => $field]; - foreach ($rule as $key => $val) { - if (is_string($key)) { - $type = strtolower($key); - $define['value'] = $val; - } else { - $type = strtolower($val); - } - if (!isset($this->classMap[$type])) { - $this->validators[] = [$model, $val]; - } else { - $merge = array_merge($this->classMap[$type], $define, [ - 'params' => $param, - 'model' => $model - ]); - $this->validators[] = $merge; - } - } - } + /** + * @param $field + * @param $rule + * @param $model + * @param $param + * @throws Exception + * ['maxLength'=>150, 'required', 'minLength' => 100] + */ + public function createRule($field, $rule, $model, $param): void + { + $define = ['field' => $field]; + foreach ($rule as $key => $val) { + if (is_string($key)) { + $type = strtolower($key); + $define['value'] = $val; + } else { + $type = strtolower($val); + } + if (!isset($this->classMap[$type])) { + $this->validators[] = [$model, $val]; + } else { + $merge = array_merge($this->classMap[$type], $define, [ + 'params' => $param, + 'model' => $model + ]); + $this->validators[] = $merge; + } + } + } - /** - * @return bool - * @throws Exception - */ - public function validation(): bool - { - if (count($this->validators) < 1) { - return true; - } - foreach ($this->validators as $val) { - [$result, $validator] = $this->check($val); - if ($result === true) { - continue; - } - $isTrue = false; - if ($validator instanceof BaseValidator) { - $this->addError(null, $validator->getError()); - } - break; - } - $this->validators = []; - return !isset($isTrue); - } + /** + * @return bool + * @throws Exception + */ + public function validation(): bool + { + if (count($this->validators) < 1) { + return true; + } + foreach ($this->validators as $val) { + [$result, $validator] = $this->check($val); + if ($result === true) { + continue; + } + $isTrue = false; + if ($validator instanceof BaseValidator) { + $this->addError(null, $validator->getError()); + } + break; + } + $this->validators = []; + return !isset($isTrue); + } - /** - * @param BaseValidator|array|Closure $val - * @return mixed - * @throws Exception - */ - private function check(BaseValidator|array|Closure $val): mixed - { - if (is_callable($val, true)) { - return [call_user_func($val, $this), $val]; - } + /** + * @param BaseValidator|array|Closure $val + * @return mixed + * @throws Exception + */ + private function check(BaseValidator|array|Closure $val): mixed + { + if (is_callable($val, true)) { + return [call_user_func($val, $this), $val]; + } - $class = Kiri::getDi()->get($val['class']); - unset($val['class']); + $class = Kiri::getDi()->get($val['class']); + unset($val['class']); - Kiri::configure($class, $val); + Kiri::configure($class, $val); - return [$class->trigger(), $class]; - } + return [$class->trigger(), $class]; + } }