From 21aca8b642303b8fe5bf38947a8bb8c23a62c08a Mon Sep 17 00:00:00 2001 From: whwyy Date: Tue, 19 Dec 2023 14:51:13 +0800 Subject: [PATCH] eee --- ArrayValidator.php | 3 +-- BaseValidator.php | 5 +---- DateTimeValidator.php | 4 +--- EmailValidator.php | 3 +-- EmptyValidator.php | 3 +-- EnumValidator.php | 3 +-- IntegerValidator.php | 5 +---- LengthValidator.php | 3 +-- RequiredValidator.php | 3 +-- RoundValidator.php | 4 +--- TypesOfValidator.php | 3 +-- UniqueValidator.php | 10 +++++++--- Validator.php | 8 ++++---- 13 files changed, 22 insertions(+), 35 deletions(-) diff --git a/ArrayValidator.php b/ArrayValidator.php index e62b72d..42a09f3 100644 --- a/ArrayValidator.php +++ b/ArrayValidator.php @@ -18,13 +18,12 @@ class ArrayValidator extends BaseValidator { /** - * @param string $field * @param mixed $value * @return bool * * 检查 */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return is_array($value); } diff --git a/BaseValidator.php b/BaseValidator.php index 6ba961a..60cc25f 100644 --- a/BaseValidator.php +++ b/BaseValidator.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace validator; -use Database\Model; -use Database\ModelInterface; use Exception; @@ -22,12 +20,11 @@ abstract class BaseValidator /** - * @param string $field * @param float $value * @return bool * @throws Exception */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { throw new Exception('Child Class must define method of trigger'); } diff --git a/DateTimeValidator.php b/DateTimeValidator.php index b44eac3..1eaf397 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -21,13 +21,11 @@ class DateTimeValidator extends BaseValidator public string $method; /** - * @param string $field * @param mixed $value * @return bool */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { - $value = $params[$field] ?? null; return match ($this->method) { self::DATE => $this->validatorDate($value), self::DATE_TIME => $this->validateDatetime($value), diff --git a/EmailValidator.php b/EmailValidator.php index d2e69d9..4d2be88 100644 --- a/EmailValidator.php +++ b/EmailValidator.php @@ -16,12 +16,11 @@ class EmailValidator extends BaseValidator { /** - * @param string $field * @param mixed $value * @return bool * 检查是否存在 */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return filter_var($value, FILTER_VALIDATE_EMAIL); } diff --git a/EmptyValidator.php b/EmptyValidator.php index 0c0a90f..e3167e9 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -27,13 +27,12 @@ class EmptyValidator extends BaseValidator public string $method; /** - * @param string $field * @param mixed $value * @return bool * * 检查参数是否为NULL */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return match ($this->method) { self::CAN_NOT_NULL => !is_null($value), diff --git a/EnumValidator.php b/EnumValidator.php index 245eae0..4d1a993 100644 --- a/EnumValidator.php +++ b/EnumValidator.php @@ -13,11 +13,10 @@ class EnumValidator extends BaseValidator { /** - * @param string $field * @param mixed $value * @return bool */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return in_array($value, $this->value); } diff --git a/IntegerValidator.php b/IntegerValidator.php index 952d7b8..558243c 100644 --- a/IntegerValidator.php +++ b/IntegerValidator.php @@ -10,8 +10,6 @@ declare(strict_types=1); namespace validator; -use Database\ModelInterface; - /** * */ @@ -19,11 +17,10 @@ class IntegerValidator extends BaseValidator { /** - * @param string $field * @param float $value * @return bool */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return (float)$value == $value; } diff --git a/LengthValidator.php b/LengthValidator.php index 5c243b7..af193e0 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -22,11 +22,10 @@ class LengthValidator extends BaseValidator public string $method = 'default'; /** - * @param string $field * @param mixed $value * @return bool */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return match ($this->method) { self::MAX_LENGTH => $this->maxLength((string)$value), diff --git a/RequiredValidator.php b/RequiredValidator.php index f076322..2b850c0 100644 --- a/RequiredValidator.php +++ b/RequiredValidator.php @@ -14,12 +14,11 @@ class RequiredValidator extends BaseValidator { /** - * @param string $field * @param mixed $value * @return bool * 检查是否存在 */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return !is_null($value); } diff --git a/RoundValidator.php b/RoundValidator.php index 2462624..9ef2a3f 100644 --- a/RoundValidator.php +++ b/RoundValidator.php @@ -4,8 +4,6 @@ namespace validator; -use Exception; - /** * Class RoundValidator * @package validator @@ -19,7 +17,7 @@ class RoundValidator extends BaseValidator * @return bool * @throws */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return round($value, $this->value) == $value; } diff --git a/TypesOfValidator.php b/TypesOfValidator.php index ec268df..c009d8d 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace validator; -use Database\ModelInterface; use function json_validate; class TypesOfValidator extends BaseValidator @@ -31,7 +30,7 @@ class TypesOfValidator extends BaseValidator * @param mixed $value * @return bool */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { return match ($this->method) { self::INTEGER => $this->integerFormat($value), diff --git a/UniqueValidator.php b/UniqueValidator.php index 2fecbbe..14d4826 100644 --- a/UniqueValidator.php +++ b/UniqueValidator.php @@ -23,15 +23,19 @@ class UniqueValidator extends BaseValidator /** - * @param string $field + * @var string + */ + public string $field; + + /** * @param mixed $value * @return bool * @throws * 检查是否存在 */ - public function trigger(string $field, mixed $value): bool + public function trigger(mixed $value): bool { - return $this->model::query()->where([$field => $value])->exists() === false; + return $this->model::query()->where([$this->field => $value])->exists() === false; } diff --git a/Validator.php b/Validator.php index 49021af..fc9adce 100644 --- a/Validator.php +++ b/Validator.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace validator; -use Closure; use Database\ModelInterface; use Kiri; @@ -63,7 +62,7 @@ class Validator extends BaseValidator if (is_numeric($key) && method_exists($model, $val)) { $this->validators[$field][] = [$model, $val]; } else { - $this->validators[$field][] = $this->mapGen($model, $key, $val); + $this->validators[$field][] = $this->mapGen($model, $field, $key, $val); } } } @@ -78,7 +77,7 @@ class Validator extends BaseValidator * @return array * @throws */ - protected function mapGen(ModelInterface $model, $key, $val): array + protected function mapGen(ModelInterface $model, $field, $key, $val): array { if (is_numeric($key)) { $defined = self::classMap[$val]; @@ -88,6 +87,7 @@ class Validator extends BaseValidator } if ($defined['class'] == UniqueValidator::class) { $defined['model'] = $model; + $defined['field'] = $field; } return [Kiri::createObject($defined), 'trigger']; } @@ -107,7 +107,7 @@ class Validator extends BaseValidator if (isset($this->validators[$field])) { $validator = $this->validators[$field]; foreach ($validator as $value) { - if (!call_user_func($value, $field, $attribute)) { + if (!call_user_func($value, $attribute)) { return $this->addError($field, 'field :attribute data format error.'); } }