From ba410c90da79029c22eb9570fd027c2787cecac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 18 Feb 2022 17:16:45 +0800 Subject: [PATCH] modify plugin name --- ArrayValidator.php | 48 +++++++--------------------------- BaseValidator.php | 21 +++++++++++++++ DateTimeValidator.php | 50 ++++++++++++++++++----------------- EmailValidator.php | 21 ++++++++++----- EmptyValidator.php | 32 +++++++++-------------- EnumValidator.php | 29 +++++++------------- IntegerValidator.php | 21 ++++++++------- LengthValidator.php | 25 ++++++++++-------- RequiredValidator.php | 12 +++++---- RoundValidator.php | 12 +++++---- TypesOfValidator.php | 61 +++++++++++++++++++++++++------------------ UniqueValidator.php | 23 ++++++++-------- Validator.php | 5 ++-- 13 files changed, 182 insertions(+), 178 deletions(-) diff --git a/ArrayValidator.php b/ArrayValidator.php index 2728f15..a2ef072 100644 --- a/ArrayValidator.php +++ b/ArrayValidator.php @@ -24,46 +24,16 @@ class ArrayValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params)) { - return true; - } - if (!isset($this->params[$this->field])) { - return true; - } - if (!is_array($this->params[$this->field])) { - return $this->addError("The param :attribute must a array"); - } - return true; - } - - /** - * @param $data - * @return array - * - * 转成数组 - */ - private function toArray($data): array - { - if (is_numeric($data)) { - return []; - } else if (is_null(json_decode($data, true))) { - return []; - } elseif (is_object($data)) { - $data = get_object_vars($data); - } - - $_tmp = []; - foreach ($data as $key => $val) { - if (is_object($val)) { - $_tmp[$key] = $this->toArray($val); - } else if (is_array($val)) { - $_tmp[$key] = $this->toArray($val); - } else { - $_tmp[$key] = $val; + return $this->_validator($this->field, function ($field, $params) { + $value = $params[$field] ?? null; + if (empty($value)) { + return true; } - } - - return $_tmp; + if (!is_array($value)) { + return $this->addError('The param :attribute must a array'); + } + return true; + }, $this->params); } } diff --git a/BaseValidator.php b/BaseValidator.php index ff6ccfa..44e0fae 100644 --- a/BaseValidator.php +++ b/BaseValidator.php @@ -110,6 +110,27 @@ abstract class BaseValidator return $this->isFail; } + + /** + * @param string|array $fields + * @param callable $callback + * @param ...$params + * @return bool + */ + protected function _validator(string|array $fields, callable $callback, ...$params): bool + { + if (is_string($fields)) { + $fields = [$fields]; + } + foreach ($fields as $field) { + if (!$callback($field, ...$params)) { + return false; + } + } + return true; + } + + /** * @return string */ diff --git a/DateTimeValidator.php b/DateTimeValidator.php index 4891826..735cf22 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -12,34 +12,36 @@ namespace validator; class DateTimeValidator extends BaseValidator { - + const DATE = 'date'; const DATE_TIME = 'datetime'; const TIME = 'time'; const STR_TO_TIME = 'timestamp'; public string $method; - + /** * @return bool */ public function trigger(): bool { - if (empty($this->params)) { - return true; - } - if (!isset($this->params[$this->field]) || empty($this->params[$this->field])) { - return true; - } - return match (strtolower($this->method)) { - self::DATE => $this->validatorDate($this->params[$this->field]), - self::DATE_TIME => $this->validateDatetime($this->params[$this->field]), - self::TIME => $this->validatorTime($this->params[$this->field]), - self::STR_TO_TIME => $this->validatorTimestamp($this->params[$this->field]), - default => true, - }; + 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($value), + self::DATE_TIME => $this->validateDatetime($value), + self::TIME => $this->validatorTime($value), + self::STR_TO_TIME => $this->validatorTimestamp($value), + default => true, + }; + }, $this->params, strtolower($this->method)); + + } - + /** * @param $value * @return bool @@ -48,7 +50,7 @@ class DateTimeValidator extends BaseValidator */ public function validatorTime($value): bool { - if (empty($value) || !is_string($value)) { + if (!is_string($value)) { return $this->addError('The param :attribute not is a date value'); } $match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result); @@ -58,8 +60,8 @@ class DateTimeValidator extends BaseValidator return $this->addError('The param :attribute format error'); } } - - + + /** * @param $value * @return bool @@ -68,7 +70,7 @@ class DateTimeValidator extends BaseValidator */ public function validateDatetime($value): bool { - if (empty($value) || !is_string($value)) { + if (!is_string($value)) { return $this->addError('The param :attribute not is a date value'); } $match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/'; @@ -79,7 +81,7 @@ class DateTimeValidator extends BaseValidator return $this->addError('The param :attribute format error'); } } - + /** * @param $value * @return bool @@ -88,7 +90,7 @@ class DateTimeValidator extends BaseValidator */ public function validatorDate($value): bool { - if (empty($value) || !is_string($value)) { + if (!is_string($value)) { return $this->addError('The param :attribute not is a date value'); } $match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result); @@ -98,7 +100,7 @@ class DateTimeValidator extends BaseValidator return $this->addError('The param :attribute format error'); } } - + /** * @param $value * @return bool @@ -107,7 +109,7 @@ class DateTimeValidator extends BaseValidator */ public function validatorTimestamp($value): bool { - if (empty($value) || !is_numeric($value)) { + if (!is_numeric($value)) { return $this->addError('The param :attribute not is a timestamp value'); } if (strlen((string)$value) != 10) { diff --git a/EmailValidator.php b/EmailValidator.php index 911a7f1..842b500 100644 --- a/EmailValidator.php +++ b/EmailValidator.php @@ -19,14 +19,21 @@ class EmailValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params) || !isset($this->params[$this->field])) { - return true; - } - if (preg_match('/^[a-zA-Z0-9]+([._]+)[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $this->params[$this->field])) { - return true; - } else { + 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('The param :attribute format error'); + } + [$account, $domain] = explode("@", $value); + if (checkdnsrr($domain, "MX")) { + return true; + } return $this->addError('The param :attribute format error'); - } + }, $this->params); } } diff --git a/EmptyValidator.php b/EmptyValidator.php index 2ab4ad8..f54aa67 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -14,15 +14,15 @@ namespace validator; class EmptyValidator extends BaseValidator { - + /** @var string [不能为空] */ const CAN_NOT_EMPTY = 'not empty'; - + /** @var string [可为空, 不能为null] */ const CAN_NOT_NULL = 'not null'; public string $method; - + /** * @return bool * @@ -30,21 +30,15 @@ class EmptyValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params) || !isset($this->params[$this->field])) { - return $this->addError(':attribute not exists'); - } - switch (strtolower($this->method)) { - case self::CAN_NOT_EMPTY: - if (strlen($this->params[$this->field]) < 1) { - return $this->addError('The :attribute can not empty.'); - } - break; - case self::CAN_NOT_NULL: - if ($this->params[$this->field] === null) { - return $this->addError('The :attribute can not is null.'); - } - break; - } - return true; + return $this->_validator($this->field, function ($field, $params, $method) { + $value = $params[$field] ?? null; + if (empty($value)) { + return $this->addError(':attribute not exists'); + } + return match ($method) { + self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError('The :attribute can not empty.'), + default => $value !== null || $this->addError('The :attribute can not empty.') + }; + }, $this->params, strtolower($this->method)); } } diff --git a/EnumValidator.php b/EnumValidator.php index bf3986b..a470265 100644 --- a/EnumValidator.php +++ b/EnumValidator.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace validator; - /** * Class EnumValidator * @package validator @@ -20,24 +19,16 @@ class EnumValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params) || !isset($this->params[$this->field])) { - return $this->addError('The param :attribute is null'); - } - if (is_null($this->params[$this->field])) { - return $this->addError('The param :attribute is null'); - } - if (!in_array($this->params[$this->field], $this->value)) { - return $this->addError($this->i()); - } - return true; - } - - /** - * @return string - */ - private function i(): string - { - return 'The param :attribute value only in ' . implode(',', $this->value); + return $this->_validator($this->field, function ($field, $params, $values) { + $value = $params[$field] ?? null; + if (empty($value)) { + return $this->addError('The param :attribute is null'); + } + if (!in_array($value, $values)) { + return $this->addError('The param :attribute value only in ' . implode(',', $values)); + } + return true; + }, $this->params, $this->value); } } diff --git a/IntegerValidator.php b/IntegerValidator.php index 7a5904e..de03a65 100644 --- a/IntegerValidator.php +++ b/IntegerValidator.php @@ -24,15 +24,18 @@ class IntegerValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params) || !isset($this->params[$this->field])) { + 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('The ' . $field . ' cannot be less than the default value.'); + } + if ($type !== self::MAX && $value > $origin) { + return $this->addError('The ' . $field . ' cannot be greater than the default value.'); + } return true; - } - if ($this->type !== self::MIN && $this->params[$this->field] < $this->value) { - return $this->addError('The ' . $this->field . ' cannot be less than the default value.'); - } - if ($this->type !== self::MAX && $this->params[$this->field] > $this->value) { - return $this->addError('The ' . $this->field . ' cannot be greater than the default value.'); - } - return true; + }, $this->params, $this->value, $this->type); } } diff --git a/LengthValidator.php b/LengthValidator.php index 155712a..c10d82c 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -25,18 +25,21 @@ class LengthValidator extends BaseValidator */ public function trigger(): bool { - if (empty($this->params) || !isset($this->params[$this->field])) { - if ($this->method != self::MAX_LENGTH) { - return $this->addError('The param :attribute not exists'); - } else { - return TRUE; + return $this->_validator($this->field, function ($field, $params, $method, $value) { + $value = $params[$this->field] ?? null; + if (empty($value)) { + if ($method != self::MAX_LENGTH) { + return $this->addError('The param :attribute not exists'); + } else { + return TRUE; + } } - } - return match (strtolower($this->method)) { - self::MAX_LENGTH => $this->maxLength($this->params[$this->field]), - self::MIN_LENGTH => $this->minLength($this->params[$this->field]), - default => $this->defaultLength($this->params[$this->field]), - }; + return match ($method) { + self::MAX_LENGTH => $this->maxLength($value), + self::MIN_LENGTH => $this->minLength($value), + default => $this->defaultLength($value), + }; + }, $this->params, strtolower($this->method), $this->value); } /** diff --git a/RequiredValidator.php b/RequiredValidator.php index 0af5b30..4832cbc 100644 --- a/RequiredValidator.php +++ b/RequiredValidator.php @@ -19,11 +19,13 @@ class RequiredValidator extends BaseValidator */ public function trigger(): bool { - if (!isset($this->params[$this->field])) { - return $this->addError('The param :attribute not exists'); - } else { - return true; - } + return $this->_validator($this->field, function ($field, $params) { + if (!isset($params[$field])) { + return $this->addError('The param :attribute not exists'); + } else { + return true; + } + }); } } diff --git a/RoundValidator.php b/RoundValidator.php index 5f72038..bde6af0 100644 --- a/RoundValidator.php +++ b/RoundValidator.php @@ -23,11 +23,13 @@ class RoundValidator extends BaseValidator */ public function trigger(): bool { - $value = $this->model->getAttribute($this->field); - if ($value == null || round($value, $this->value) != $value) { - return $this->addError('The param :attribute length error'); - } - return true; + return $this->_validator($this->field, function ($field, $model, $param) { + $value = $model->getAttribute($field); + if ($value == null || round($value, $param) != $value) { + return $this->addError('The param :attribute length error'); + } + return true; + }, $this->model, $this->value); } diff --git a/TypesOfValidator.php b/TypesOfValidator.php index e2e1734..446bbc8 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -43,97 +43,106 @@ class TypesOfValidator extends BaseValidator */ public function trigger(): bool { - if (!in_array($this->method, $this->types)) { - return true; - } - if (empty($this->params) || !isset($this->params[$this->field])) { - return true; - } - if ($this->params[$this->field] === null) { - return $this->addError('This ' . $this->field . ' is not an empty data.'); - } - return $this->{$this->method . 'Format'}($this->params[$this->field]); + return $this->_validator($this->field, function ($field, $params, $method, $types) { + if (!in_array($method, $types)) { + return true; + } + if (!isset($params[$field])) { + return true; + } + $value = $params[$field] ?? null; + if (empty($value)) { + return $this->addError('This ' . $field . ' is not an empty data.'); + } + return $this->{$method . 'Format'}($field, $value); + }, $this->params, $this->method, $this->types); } /** + * @param $field * @param $value * @return bool */ - public function jsonFormat($value): bool + public function jsonFormat($field, $value): bool { if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is JSON data.'); + return $this->addError('The ' . $field . ' not is JSON data.'); } if (is_null(json_decode($value))) { - return $this->addError('The ' . $this->field . ' not is JSON data.'); + return $this->addError('The ' . $field . ' not is JSON data.'); } return true; } /** + * @param $field * @param $value * @return bool */ - public function serializeFormat($value): bool + public function serializeFormat($field, $value): bool { if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is serialize data.'); + return $this->addError('The ' . $field . ' not is serialize data.'); } if (false === swoole_unserialize($value)) { - return $this->addError('The ' . $this->field . ' not is serialize data.'); + return $this->addError('The ' . $field . ' not is serialize data.'); } return true; } /** + * @param $field * @param $value * @return bool */ - public function arrayFormat($value): bool + public function arrayFormat($field, $value): bool { if (!is_array($value)) { - return $this->addError('The ' . $this->field . ' not is array data.'); + return $this->addError('The ' . $field . ' not is array data.'); } return true; } /** + * @param $field * @param $value * @return bool */ - public function stringFormat($value): bool + public function stringFormat($field, $value): bool { if (is_array($value) || is_object($value) || is_bool($value)) { - return $this->addError('The ' . $this->field . ' not is string data.'); + return $this->addError('The ' . $field . ' not is string data.'); } return true; } /** + * @param $field * @param $value * @return bool */ - public function integerFormat($value): bool + public function integerFormat($field, $value): bool { if (!is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is number data.'); + return $this->addError('The ' . $field . ' not is number data.'); } if ((int)$value != $value) { - return $this->addError('The ' . $this->field . ' not is number data.'); + return $this->addError('The ' . $field . ' not is number data.'); } return true; } /** + * @param $field * @param $value * @return bool */ - public function floatFormat($value): bool + public function floatFormat($field, $value): bool { $trim = (float)$value; - if ($trim != $value || !is_float($trim)) { - return $this->addError('The ' . $this->field . ' not is float data.'); + if ($trim != $value) { + return $this->addError('The ' . $field . ' not is float data.'); } return true; } diff --git a/UniqueValidator.php b/UniqueValidator.php index 6b63ac5..e65d2a0 100644 --- a/UniqueValidator.php +++ b/UniqueValidator.php @@ -20,21 +20,22 @@ class UniqueValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { - return TRUE; - } - - if (empty($this->model)) { + if (empty($model)) { return $this->addError('Model error.'); } - if (!$this->model->getIsNowExample()) { + if (!$model->getIsNowExample()) { return true; } - if ($this->model::query()->where([$this->field => $param[$this->field]])->exists()) { - return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!'); - } - return $this->isFail = TRUE; + return $this->_validator($this->field, function ($field, $params, $model) { + if (!isset($params[$field])) { + return true; + } + $param = $params[$field]; + if ($model::query()->where([$field => $param])->exists()) { + return $this->addError('The :attribute \'' . $param . '\' is exists!'); + } + return $this->isFail = TRUE; + }, $this->params, $this->model); } diff --git a/Validator.php b/Validator.php index d58f7d3..bf17232 100644 --- a/Validator.php +++ b/Validator.php @@ -132,9 +132,8 @@ class Validator extends BaseValidator $param = $this->getParams(); $model = $this->getModel(); - foreach ($field as $val) { - $this->createRule($val, $rules, $model, $param); - } + + $this->createRule($field, $rules, $model, $param); return $this; }