From d860269a7cda1d47c7e46f7fcc450faeeac7c70e Mon Sep 17 00:00:00 2001 From: whwyy Date: Tue, 12 Dec 2023 15:35:34 +0800 Subject: [PATCH] eee --- BaseValidator.php | 232 +++++++++++++++++++++--------------------- DateTimeValidator.php | 142 +++++++++++++------------- EmptyValidator.php | 4 +- LengthValidator.php | 4 +- RoundValidator.php | 30 +++--- TypesOfValidator.php | 10 +- Validator.php | 13 ++- 7 files changed, 217 insertions(+), 218 deletions(-) diff --git a/BaseValidator.php b/BaseValidator.php index 2772d09..8589cfc 100644 --- a/BaseValidator.php +++ b/BaseValidator.php @@ -14,150 +14,150 @@ use Exception; abstract class BaseValidator { - public array $field = []; + public array $field = []; - public array $rules = []; + public array $rules = []; - public string $method; + public string $method; - protected bool $isFail = TRUE; + protected bool $isFail = TRUE; - protected string $message = ''; + protected string $message = ''; - protected array $params = []; + protected array $params = []; - protected ?Model $model = null; + protected ?Model $model = null; - /** - * @param $model - */ - public function setModel($model): void - { - $this->model = $model; - } + /** + * @param $model + */ + public function setModel($model): void + { + $this->model = $model; + } - /** - * @return Model|null - */ - public function getModel(): ?Model - { - return $this->model; - } + /** + * @return Model|null + */ + public function getModel(): ?Model + { + return $this->model; + } - /** - * BaseValidator constructor. - * @param array $config - */ - public function __construct(array $config = []) - { - $this->regConfig($config); - } + /** + * BaseValidator constructor. + * @param array $config + */ + public function __construct(array $config = []) + { + $this->regConfig($config); + } - /** - * @param $config - */ - private function regConfig($config): void - { - if (count($config) < 1) { - return; - } - foreach ($config as $key => $val) { - $this->$key = $val; - } - } + /** + * @param $config + */ + private function regConfig($config): void + { + if (count($config) < 1) { + return; + } + foreach ($config as $key => $val) { + $this->$key = $val; + } + } - /** - * @return bool - * @throws Exception - */ - public function trigger(): bool - { - throw new Exception('Child Class must define method of trigger'); - } + /** + * @return bool + * @throws + */ + public function trigger(): bool + { + throw new Exception('Child Class must define method of trigger'); + } - /** - * @return array - */ - protected function getParams(): array - { - return $this->params; - } + /** + * @return array + */ + protected function getParams(): array + { + return $this->params; + } - /** - * @param array $data - * @return $this - */ - public function setParams(array $data): static - { - $this->params = $data; - return $this; - } + /** + * @param array $data + * @return $this + */ + public function setParams(array $data): static + { + $this->params = $data; + return $this; + } /** * @param $field * @param $message * @return bool */ - public function addError($field, $message): bool - { - $this->isFail = FALSE; + public function addError($field, $message): bool + { + $this->isFail = FALSE; - if (!is_null($field)) { - $message = str_replace(':attribute', $field, $message); - } + if (!is_null($field)) { + $message = str_replace(':attribute', $field, $message); + } - \trigger_print_error($message,"mysql"); - $this->message = $message; + \trigger_print_error($message, "mysql"); + $this->message = $message; - return $this->isFail; - } + 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; - } + /** + * @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 - */ - public function getError(): string - { - return $this->message; - } + /** + * @return string + */ + public function getError(): string + { + return $this->message; + } - /** - * @param $name - * @param $value - * @throws Exception - */ - public function __set($name, $value) - { - $method = 'set' . ucfirst($name); - if (method_exists($this, $method)) { - $this->$method($value); - } else if (property_exists($this, $name)) { - $this->$name = $value; - } else { - throw new Exception('unknown property ' . $name . ' in class ' . static::class); - } - } + /** + * @param $name + * @param $value + * @throws + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + if (method_exists($this, $method)) { + $this->$method($value); + } else if (property_exists($this, $name)) { + $this->$name = $value; + } else { + throw new Exception('unknown property ' . $name . ' in class ' . static::class); + } + } } diff --git a/DateTimeValidator.php b/DateTimeValidator.php index 0a57727..412e4ee 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -13,31 +13,31 @@ namespace validator; class DateTimeValidator extends BaseValidator { - const DATE = 'date'; - const DATE_TIME = 'datetime'; - const TIME = 'time'; - const STR_TO_TIME = 'timestamp'; + const string DATE = 'date'; + const string DATE_TIME = 'datetime'; + const string TIME = 'time'; + const string STR_TO_TIME = 'timestamp'; - public string $method; + public string $method; - /** - * @return bool - */ - public function trigger(): bool - { - return $this->_validator($this->field, function ($field, $params, $method) { - $value = $params[$field] ?? null; - return match ($method) { - self::DATE => $this->validatorDate($field, $value), - self::DATE_TIME => $this->validateDatetime($field, $value), - self::TIME => $this->validatorTime($field, $value), - self::STR_TO_TIME => $this->validatorTimestamp($field, $value), - default => true, - }; - }, $this->params, strtolower($this->method)); + /** + * @return bool + */ + public function trigger(): bool + { + return $this->_validator($this->field, function ($field, $params, $method) { + $value = $params[$field] ?? null; + return match ($method) { + self::DATE => $this->validatorDate($field, $value), + self::DATE_TIME => $this->validateDatetime($field, $value), + self::TIME => $this->validatorTime($field, $value), + self::STR_TO_TIME => $this->validatorTimestamp($field, $value), + default => true, + }; + }, $this->params, strtolower($this->method)); - } + } /** * @param $field @@ -46,18 +46,18 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 01:02 or 01-02 */ - public function validatorTime($field, $value): bool - { - if (!is_string($value)) { - return $this->addError($field, 'The param :attribute not is a date value'); - } - $match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError($field, 'The param :attribute format error'); - } - } + public function validatorTime($field, $value): bool + { + if (!is_string($value)) { + return $this->addError($field, 'The param :attribute not is a date value'); + } + $match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError($field, 'The param :attribute format error'); + } + } /** @@ -67,19 +67,19 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 2017-12-22 01:02 */ - public function validateDatetime($field, $value): bool - { - if (!is_string($value)) { - return $this->addError($field, 'The param :attribute not is a date value'); - } - $match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/'; - $match = preg_match($match, $value, $result); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError($field, 'The param :attribute format error'); - } - } + public function validateDatetime($field, $value): bool + { + if (!is_string($value)) { + return $this->addError($field, 'The param :attribute not is a date value'); + } + $match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/'; + $match = preg_match($match, $value, $result); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError($field, 'The param :attribute format error'); + } + } /** * @param $field @@ -88,18 +88,18 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 2017-12-22 */ - public function validatorDate($field, $value): bool - { - if (!is_string($value)) { - return $this->addError($field, 'The param :attribute not is a date value'); - } - $match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError($field, 'The param :attribute format error'); - } - } + public function validatorDate($field, $value): bool + { + if (!is_string($value)) { + return $this->addError($field, 'The param :attribute not is a date value'); + } + $match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError($field, 'The param :attribute format error'); + } + } /** * @param $field @@ -108,17 +108,17 @@ class DateTimeValidator extends BaseValidator * * 效验时间戳 格式如 1521452254 */ - public function validatorTimestamp($field, $value): bool - { - if (!is_numeric($value)) { - return $this->addError($field, 'The param :attribute not is a timestamp value'); - } - if (strlen((string)$value) != 10) { - return $this->addError($field, 'The param :attribute not is a timestamp value'); - } - if (!date('YmdHis', $value)) { - return $this->addError($field, 'The param :attribute format error'); - } - return true; - } + public function validatorTimestamp($field, $value): bool + { + if (!is_numeric($value)) { + return $this->addError($field, 'The param :attribute not is a timestamp value'); + } + if (strlen((string)$value) != 10) { + return $this->addError($field, 'The param :attribute not is a timestamp value'); + } + if (!date('YmdHis', $value)) { + return $this->addError($field, 'The param :attribute format error'); + } + return true; + } } diff --git a/EmptyValidator.php b/EmptyValidator.php index 4989543..73eeb13 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -19,10 +19,10 @@ class EmptyValidator extends BaseValidator { /** @var string [不能为空] */ - const CAN_NOT_EMPTY = 'not empty'; + const string CAN_NOT_EMPTY = 'not empty'; /** @var string [可为空, 不能为null] */ - const CAN_NOT_NULL = 'not null'; + const string CAN_NOT_NULL = 'not null'; public string $method; diff --git a/LengthValidator.php b/LengthValidator.php index f3c5008..8c5ee31 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -13,8 +13,8 @@ namespace validator; class LengthValidator extends BaseValidator { - const MAX_LENGTH = 'max'; - const MIN_LENGTH = 'min'; + const string MAX_LENGTH = 'max'; + const string MIN_LENGTH = 'min'; public string $method; diff --git a/RoundValidator.php b/RoundValidator.php index a049960..4c39641 100644 --- a/RoundValidator.php +++ b/RoundValidator.php @@ -14,23 +14,23 @@ class RoundValidator extends BaseValidator { - public ?int $value = null; + public ?int $value = null; - /** - * @return bool - * @throws Exception - */ - public function trigger(): bool - { - return $this->_validator($this->field, function ($field, $model, $param) { - $value = $model->getAttribute($field); - if ($value == null || round($value, $param) != $value) { - return $this->addError($field,'The param :attribute length error'); - } - return true; - }, $this->model, $this->value); - } + /** + * @return bool + * @throws + */ + public function trigger(): bool + { + return $this->_validator($this->field, function ($field, $model, $param) { + $value = $model->getAttribute($field); + if ($value == null || round($value, $param) != $value) { + return $this->addError($field, 'The param :attribute length error'); + } + return true; + }, $this->model, $this->value); + } } diff --git a/TypesOfValidator.php b/TypesOfValidator.php index 9844e56..c852d07 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -14,11 +14,11 @@ class TypesOfValidator extends BaseValidator { - const JSON = 'json'; - const FLOAT = 'float'; - const ARRAY = 'array'; - const STRING = 'string'; - const INTEGER = 'integer'; + const string JSON = 'json'; + const string FLOAT = 'float'; + const string ARRAY = 'array'; + const string STRING = 'string'; + const string INTEGER = 'integer'; private ?int $min = null; private ?int $max = null; diff --git a/Validator.php b/Validator.php index 1de8605..0878400 100644 --- a/Validator.php +++ b/Validator.php @@ -7,7 +7,6 @@ namespace validator; use Closure; use Database\ModelInterface; -use Exception; use Kiri; /** @@ -40,7 +39,7 @@ class Validator extends BaseValidator * @param $field * @param $rules * @return $this - * @throws Exception + * @throws */ public function make($field, $rules): static { @@ -61,7 +60,7 @@ class Validator extends BaseValidator * @param $rule * @param $model * @param $param - * @throws Exception + * @throws * ['maxLength'=>150, 'required', 'minLength' => 100] */ public function createRule($field, $rule, $model, $param): void @@ -69,7 +68,7 @@ class Validator extends BaseValidator $define = ['field' => $field]; foreach ($rule as $key => $val) { if (is_string($key)) { - $type = strtolower($key); + $type = strtolower($key); $define['value'] = $val; } else { $type = strtolower($val); @@ -77,7 +76,7 @@ class Validator extends BaseValidator if (!isset($this->classMap[$type])) { $this->validators[] = [$model, $val]; } else { - $merge = array_merge($this->classMap[$type], $define, [ + $merge = array_merge($this->classMap[$type], $define, [ 'params' => $param, 'model' => $model ]); @@ -88,7 +87,7 @@ class Validator extends BaseValidator /** * @return bool - * @throws Exception + * @throws */ public function validation(): bool { @@ -113,7 +112,7 @@ class Validator extends BaseValidator /** * @param BaseValidator|array|Closure $val * @return array - * @throws Exception + * @throws */ private function check(BaseValidator|array|Closure $val): array {