diff --git a/src/ArrayValidator.php b/src/ArrayValidator.php index 232e1fe..d1a958e 100644 --- a/src/ArrayValidator.php +++ b/src/ArrayValidator.php @@ -24,14 +24,13 @@ class ArrayValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !is_array($param)) { + if (empty($this->params)) { return true; } - if (!isset($param[$this->field])) { + if (!isset($this->params[$this->field])) { return true; } - if (!is_array($param[$this->field])) { + if (!is_array($this->params[$this->field])) { return $this->addError("The param :attribute must a array"); } return true; diff --git a/src/DateTimeValidator.php b/src/DateTimeValidator.php index 6fe093f..889566c 100644 --- a/src/DateTimeValidator.php +++ b/src/DateTimeValidator.php @@ -25,26 +25,19 @@ class DateTimeValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !is_array($param)) { + if (empty($this->params)) { return true; } - if (!isset($param[$this->field]) || empty($param[$this->field])) { + if (!isset($this->params[$this->field]) || empty($this->params[$this->field])) { return true; } - $value = $param[$this->field]; - switch (strtolower($this->method)) { - case self::DATE: - return $this->validatorDate($value); - case self::DATE_TIME: - return $this->validateDatetime($value); - case self::TIME: - return $this->validatorTime($value); - case self::STR_TO_TIME: - return $this->validatorTimestamp($value); - default: - 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, + }; } /** diff --git a/src/EmailValidator.php b/src/EmailValidator.php index 6ec3df7..c85549e 100644 --- a/src/EmailValidator.php +++ b/src/EmailValidator.php @@ -12,24 +12,21 @@ namespace validator; class EmailValidator extends BaseValidator { - + /** * @return bool * 检查是否存在 */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + if (empty($this->params) || !isset($this->params[$this->field])) { + return true; + } + if (preg_match('/^[a-zA-Z0-9]+([\.\_]{1,})[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $this->params[$this->field])) { return true; } else { - $value = $param[$this->field]; - if (preg_match('/^[a-zA-Z0-9]+([\.\_]{1,})[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $value)) { - return true; - } else { - return $this->addError('The param :attribute format error'); - } + return $this->addError('The param :attribute format error'); } } - + } diff --git a/src/EmptyValidator.php b/src/EmptyValidator.php index de861da..89eab55 100644 --- a/src/EmptyValidator.php +++ b/src/EmptyValidator.php @@ -30,26 +30,21 @@ class EmptyValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + if (empty($this->params) || !isset($this->params[$this->field])) { return $this->addError(':attribute not exists'); } - - $value = $param[$this->field]; - switch (strtolower($this->method)) { case self::CAN_NOT_EMPTY: - if (strlen($value) < 1) { + if (strlen($this->params[$this->field]) < 1) { return $this->addError('The :attribute can not empty.'); } break; case self::CAN_NOT_NULL: - if ($value === null) { + if ($this->params[$this->field] === null) { return $this->addError('The :attribute can not is null.'); } break; } - return true; } } diff --git a/src/EnumValidator.php b/src/EnumValidator.php index b3ae820..5757923 100644 --- a/src/EnumValidator.php +++ b/src/EnumValidator.php @@ -20,22 +20,15 @@ class EnumValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + if (empty($this->params) || !isset($this->params[$this->field])) { return $this->addError('The param :attribute is null'); } - $value = $param[$this->field]; - if (is_null($value)) { + if (is_null($this->params[$this->field])) { return $this->addError('The param :attribute is null'); } - - if (!is_array($this->value)) { - return true; - } - if (!in_array($value, $this->value)) { + if (!in_array($this->params[$this->field], $this->value)) { return $this->addError($this->i()); } - return true; } diff --git a/src/IntegerValidator.php b/src/IntegerValidator.php index 4afc8af..ef5432b 100644 --- a/src/IntegerValidator.php +++ b/src/IntegerValidator.php @@ -24,20 +24,13 @@ class IntegerValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + if (empty($this->params) || !isset($this->params[$this->field])) { return true; } - - $value = $param[$this->field] ?? null; - if ($value === null) { - return $this->addError('The :attribute can not is null.'); - } - if ($this->type !== self::MIN && $value < $this->value) { + 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 && $value > $this->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; diff --git a/src/LengthValidator.php b/src/LengthValidator.php index 9b5ef67..90e475e 100644 --- a/src/LengthValidator.php +++ b/src/LengthValidator.php @@ -25,22 +25,17 @@ class LengthValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + 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; } } - $value = $param[$this->field]; - if (is_null($value)) { - return $this->addError('The param :attribute is null'); - } return match (strtolower($this->method)) { - self::MAX_LENGTH => $this->maxLength($value), - self::MIN_LENGTH => $this->minLength($value), - default => $this->defaultLength($value), + 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]), }; } diff --git a/src/RequiredValidator.php b/src/RequiredValidator.php index f5550ce..a2b6b6e 100644 --- a/src/RequiredValidator.php +++ b/src/RequiredValidator.php @@ -19,8 +19,7 @@ class RequiredValidator extends BaseValidator */ public function trigger(): bool { - $param = $this->getParams(); - if (!is_array($param) || !isset($param[$this->field])) { + if (!isset($this->params[$this->field])) { return $this->addError('The param :attribute not exists'); } else { return true; diff --git a/src/TypesOfValidator.php b/src/TypesOfValidator.php index 4c3586e..426d236 100644 --- a/src/TypesOfValidator.php +++ b/src/TypesOfValidator.php @@ -46,21 +46,13 @@ class TypesOfValidator extends BaseValidator if (!in_array($this->method, $this->types)) { return true; } - - $param = $this->getParams(); - if (empty($param) || !isset($param[$this->field])) { + if (empty($this->params) || !isset($this->params[$this->field])) { return true; } - - $value = $param[$this->field]; - - $method = $this->method . 'Format'; - - if ($value === null) { + if ($this->params[$this->field] === null) { return $this->addError('This ' . $this->field . ' is not an empty data.'); } - - return $this->{$method}($value); + return $this->{$this->method . 'Format'}($this->params[$this->field]); } /** diff --git a/src/UniqueValidator.php b/src/UniqueValidator.php index 2201e03..5e8453f 100644 --- a/src/UniqueValidator.php +++ b/src/UniqueValidator.php @@ -28,12 +28,10 @@ class UniqueValidator extends BaseValidator if (empty($this->model)) { return $this->addError('Model error.'); } - - $model = $this->model; if (!$this->model->getIsNowExample()) { return true; } - if ($model::query()->where([$this->field => $param[$this->field]])->exists()) { + 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; diff --git a/src/Validator.php b/src/Validator.php index f2670bc..6e7c648 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -168,11 +168,10 @@ class Validator extends BaseValidator if (!isset($this->classMap[$type])) { continue; } - $constr = array_merge($this->classMap[$type], $define, [ + $this->validators[] = array_merge($this->classMap[$type], $define, [ 'params' => $param, 'model' => $model ]); - $this->validators[] = $constr; } }