diff --git a/.gitignore b/.gitignore index efca283..2ea4619 100644 --- a/.gitignore +++ b/.gitignore @@ -1,34 +1,34 @@ -# Created by .ignore support plugin (hsz.mobi) -### Yii template -assets/* -!assets/.gitignore -protected/runtime/* -!protected/runtime/.gitignore -protected/data/*.db -themes/classic/views/ - -### Example user template template -### Example user template - -# IntelliJ project files -.idea -*.iml -out -gen - -composer.lock - -*.log -commands/result -config/setting.php -tests/ -vendor/ -runtime/ - -*.xml -*.lock - -oot -d - -composer.lock +# Created by .ignore support plugin (hsz.mobi) +### Yii template +assets/* +!assets/.gitignore +protected/runtime/* +!protected/runtime/.gitignore +protected/data/*.db +themes/classic/views/ + +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen + +composer.lock + +*.log +commands/result +config/setting.php +tests/ +vendor/ +runtime/ + +*.xml +*.lock + +oot +d + +composer.lock diff --git a/ArrayValidator.php b/ArrayValidator.php index d1a958e..2728f15 100644 --- a/ArrayValidator.php +++ b/ArrayValidator.php @@ -1,69 +1,69 @@ -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 $_tmp; - } - -} +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 $_tmp; + } + +} diff --git a/BaseValidator.php b/BaseValidator.php index 23f8489..ff6ccfa 100644 --- a/BaseValidator.php +++ b/BaseValidator.php @@ -1,137 +1,137 @@ -model = $model; - } - - /** - * @return Model|null - */ - public function getModel(): ?Model - { - return $this->model; - } - - - /** - * BaseValidator constructor. - * @param array $config - */ - public function __construct(array $config = []) - { - $this->regConfig($config); - } - - - /** - * @param $config - */ - private function regConfig($config) - { - if (empty($config) || !is_array($config)) { - 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 array - */ - protected function getParams(): array - { - return $this->params; - } - - /** - * @param array|null $data - * @return $this - */ - public function setParams(?array $data): static - { - if (is_null($data)) { - $data = []; - } - $this->params = $data; - return $this; - } - - /** - * @param $message - * @return bool - */ - public function addError($message): bool - { - $this->isFail = FALSE; - - $message = str_replace(':attribute', $this->field, $message); - - $this->message = $message; - - return $this->isFail; - } - - /** - * @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); - } - } -} +model = $model; + } + + /** + * @return Model|null + */ + public function getModel(): ?Model + { + return $this->model; + } + + + /** + * BaseValidator constructor. + * @param array $config + */ + public function __construct(array $config = []) + { + $this->regConfig($config); + } + + + /** + * @param $config + */ + private function regConfig($config) + { + if (empty($config) || !is_array($config)) { + 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 array + */ + protected function getParams(): array + { + return $this->params; + } + + /** + * @param array|null $data + * @return $this + */ + public function setParams(?array $data): static + { + if (is_null($data)) { + $data = []; + } + $this->params = $data; + return $this; + } + + /** + * @param $message + * @return bool + */ + public function addError($message): bool + { + $this->isFail = FALSE; + + $message = str_replace(':attribute', $this->field, $message); + + $this->message = $message; + + return $this->isFail; + } + + /** + * @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); + } + } +} diff --git a/DateTimeValidator.php b/DateTimeValidator.php index 889566c..4891826 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -1,121 +1,121 @@ -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, - }; - } - - /** - * @param $value - * @return bool - * - * 效验分秒 格式如 01:02 or 01-02 - */ - public function validatorTime($value): bool - { - if (empty($value) || !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); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError('The param :attribute format error'); - } - } - - - /** - * @param $value - * @return bool - * - * 效验分秒 格式如 2017-12-22 01:02 - */ - public function validateDatetime($value): bool - { - if (empty($value) || !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}$/'; - $match = preg_match($match, $value, $result); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError('The param :attribute format error'); - } - } - - /** - * @param $value - * @return bool - * - * 效验分秒 格式如 2017-12-22 - */ - public function validatorDate($value): bool - { - if (empty($value) || !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); - if ($match && $result[0] == $value) { - return true; - } else { - return $this->addError('The param :attribute format error'); - } - } - - /** - * @param $value - * @return bool - * - * 效验时间戳 格式如 1521452254 - */ - public function validatorTimestamp($value): bool - { - if (empty($value) || !is_numeric($value)) { - return $this->addError('The param :attribute not is a timestamp value'); - } - if (strlen((string)$value) != 10) { - return $this->addError('The param :attribute not is a timestamp value'); - } - if (!date('YmdHis', $value)) { - return $this->addError('The param :attribute format error'); - } - return true; - } -} +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, + }; + } + + /** + * @param $value + * @return bool + * + * 效验分秒 格式如 01:02 or 01-02 + */ + public function validatorTime($value): bool + { + if (empty($value) || !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); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError('The param :attribute format error'); + } + } + + + /** + * @param $value + * @return bool + * + * 效验分秒 格式如 2017-12-22 01:02 + */ + public function validateDatetime($value): bool + { + if (empty($value) || !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}$/'; + $match = preg_match($match, $value, $result); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError('The param :attribute format error'); + } + } + + /** + * @param $value + * @return bool + * + * 效验分秒 格式如 2017-12-22 + */ + public function validatorDate($value): bool + { + if (empty($value) || !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); + if ($match && $result[0] == $value) { + return true; + } else { + return $this->addError('The param :attribute format error'); + } + } + + /** + * @param $value + * @return bool + * + * 效验时间戳 格式如 1521452254 + */ + public function validatorTimestamp($value): bool + { + if (empty($value) || !is_numeric($value)) { + return $this->addError('The param :attribute not is a timestamp value'); + } + if (strlen((string)$value) != 10) { + return $this->addError('The param :attribute not is a timestamp value'); + } + if (!date('YmdHis', $value)) { + return $this->addError('The param :attribute format error'); + } + return true; + } +} diff --git a/EmailValidator.php b/EmailValidator.php index c85549e..d9ac918 100644 --- a/EmailValidator.php +++ b/EmailValidator.php @@ -1,32 +1,32 @@ -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 { - return $this->addError('The param :attribute format error'); - } - } - -} +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 { + return $this->addError('The param :attribute format error'); + } + } + +} diff --git a/EmptyValidator.php b/EmptyValidator.php index 89eab55..2ab4ad8 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -1,50 +1,50 @@ -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; - } -} +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; + } +} diff --git a/EnumValidator.php b/EnumValidator.php index 5757923..bf3986b 100644 --- a/EnumValidator.php +++ b/EnumValidator.php @@ -1,43 +1,43 @@ -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); - } - -} +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); + } + +} diff --git a/IntegerValidator.php b/IntegerValidator.php index ef5432b..7a5904e 100644 --- a/IntegerValidator.php +++ b/IntegerValidator.php @@ -1,38 +1,38 @@ -params) || !isset($this->params[$this->field])) { - 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; - } -} +params) || !isset($this->params[$this->field])) { + 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; + } +} diff --git a/LengthValidator.php b/LengthValidator.php index 90e475e..155712a 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -1,110 +1,110 @@ -params) || !isset($this->params[$this->field])) { - if ($this->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]), - }; - } - - /** - * @param $value - * @return bool - * - * 效验长度是否大于最大长度 - */ - private function maxLength($value): bool - { - if (is_array($value)) { - if (count($value) > $value) { - return $this->addError('The param :attribute length overflow'); - } - } else { - if (is_numeric($value) && strlen((string)$value) > $this->value) { - return $this->addError('The param :attribute length overflow'); - } - if (strlen($value) > $this->value) { - return $this->addError('The param :attribute length overflow'); - } - } - return TRUE; - } - - /** - * @param $value - * @return bool - * - * 效验长度是否小于最小长度 - */ - private function minLength($value): bool - { - if (is_array($value)) { - if (count($value) < $value) { - return $this->addError('The param :attribute length error'); - } - } else { - if (is_numeric($value) && strlen((string)$value) < $this->value) { - return $this->addError('The param :attribute length overflow'); - } - if (strlen($value) < $this->value) { - return $this->addError('The param :attribute length error'); - } - } - return TRUE; - } - - /** - * @param $value - * @return bool - * - * 效验长度是否小于最小长度 - */ - private function defaultLength($value): bool - { - if (is_array($value)) { - if (count($value) !== $value) { - return $this->addError('The param :attribute length error'); - } - } else { - if (is_numeric($value) && strlen((string)$value) !== $this->value) { - return $this->addError('The param :attribute length overflow'); - } - if (mb_strlen($value) !== $this->value) { - return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value); - } - } - return TRUE; - } -} +params) || !isset($this->params[$this->field])) { + if ($this->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]), + }; + } + + /** + * @param $value + * @return bool + * + * 效验长度是否大于最大长度 + */ + private function maxLength($value): bool + { + if (is_array($value)) { + if (count($value) > $value) { + return $this->addError('The param :attribute length overflow'); + } + } else { + if (is_numeric($value) && strlen((string)$value) > $this->value) { + return $this->addError('The param :attribute length overflow'); + } + if (strlen($value) > $this->value) { + return $this->addError('The param :attribute length overflow'); + } + } + return TRUE; + } + + /** + * @param $value + * @return bool + * + * 效验长度是否小于最小长度 + */ + private function minLength($value): bool + { + if (is_array($value)) { + if (count($value) < $value) { + return $this->addError('The param :attribute length error'); + } + } else { + if (is_numeric($value) && strlen((string)$value) < $this->value) { + return $this->addError('The param :attribute length overflow'); + } + if (strlen($value) < $this->value) { + return $this->addError('The param :attribute length error'); + } + } + return TRUE; + } + + /** + * @param $value + * @return bool + * + * 效验长度是否小于最小长度 + */ + private function defaultLength($value): bool + { + if (is_array($value)) { + if (count($value) !== $value) { + return $this->addError('The param :attribute length error'); + } + } else { + if (is_numeric($value) && strlen((string)$value) !== $this->value) { + return $this->addError('The param :attribute length overflow'); + } + if (mb_strlen($value) !== $this->value) { + return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value); + } + } + return TRUE; + } +} diff --git a/RequiredValidator.php b/RequiredValidator.php index a2b6b6e..0af5b30 100644 --- a/RequiredValidator.php +++ b/RequiredValidator.php @@ -1,29 +1,29 @@ -params[$this->field])) { - return $this->addError('The param :attribute not exists'); - } else { - return true; - } - } - -} +params[$this->field])) { + return $this->addError('The param :attribute not exists'); + } else { + return true; + } + } + +} diff --git a/RoundValidator.php b/RoundValidator.php index 66fa63f..5f72038 100644 --- a/RoundValidator.php +++ b/RoundValidator.php @@ -1,34 +1,34 @@ -model->getAttribute($this->field); - if ($value == null || round($value, $this->value) != $value) { - return $this->addError('The param :attribute length error'); - } - return true; - } - - -} +model->getAttribute($this->field); + if ($value == null || round($value, $this->value) != $value) { + return $this->addError('The param :attribute length error'); + } + return true; + } + + +} diff --git a/TypesOfValidator.php b/TypesOfValidator.php index 426d236..e2e1734 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -1,141 +1,141 @@ - 'json', - self::FLOAT => 'float', - self::ARRAY => 'array', - self::STRING => 'string', - self::INTEGER => 'integer', - self::SERIALIZE => 'serialize', - ]; - - /** @var string */ - public string $method; - - - /** - * @return bool - */ - 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]); - } - - /** - * @param $value - * @return bool - */ - public function jsonFormat($value): bool - { - if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is JSON data.'); - } - if (is_null(json_decode($value))) { - return $this->addError('The ' . $this->field . ' not is JSON data.'); - } - return true; - } - - /** - * @param $value - * @return bool - */ - public function serializeFormat($value): bool - { - if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is serialize data.'); - } - if (false === swoole_unserialize($value)) { - return $this->addError('The ' . $this->field . ' not is serialize data.'); - } - return true; - } - - /** - * @param $value - * @return bool - */ - public function arrayFormat($value): bool - { - if (!is_array($value)) { - return $this->addError('The ' . $this->field . ' not is array data.'); - } - return true; - } - - /** - * @param $value - * @return bool - */ - public function stringFormat($value): bool - { - if (is_array($value) || is_object($value) || is_bool($value)) { - return $this->addError('The ' . $this->field . ' not is string data.'); - } - return true; - } - - /** - * @param $value - * @return bool - */ - public function integerFormat($value): bool - { - if (!is_numeric($value)) { - return $this->addError('The ' . $this->field . ' not is number data.'); - } - if ((int)$value != $value) { - return $this->addError('The ' . $this->field . ' not is number data.'); - } - - return true; - } - - /** - * @param $value - * @return bool - */ - public function floatFormat($value): bool - { - $trim = (float)$value; - if ($trim != $value || !is_float($trim)) { - return $this->addError('The ' . $this->field . ' not is float data.'); - } - return true; - } - -} + 'json', + self::FLOAT => 'float', + self::ARRAY => 'array', + self::STRING => 'string', + self::INTEGER => 'integer', + self::SERIALIZE => 'serialize', + ]; + + /** @var string */ + public string $method; + + + /** + * @return bool + */ + 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]); + } + + /** + * @param $value + * @return bool + */ + public function jsonFormat($value): bool + { + if (!is_string($value) || is_numeric($value)) { + return $this->addError('The ' . $this->field . ' not is JSON data.'); + } + if (is_null(json_decode($value))) { + return $this->addError('The ' . $this->field . ' not is JSON data.'); + } + return true; + } + + /** + * @param $value + * @return bool + */ + public function serializeFormat($value): bool + { + if (!is_string($value) || is_numeric($value)) { + return $this->addError('The ' . $this->field . ' not is serialize data.'); + } + if (false === swoole_unserialize($value)) { + return $this->addError('The ' . $this->field . ' not is serialize data.'); + } + return true; + } + + /** + * @param $value + * @return bool + */ + public function arrayFormat($value): bool + { + if (!is_array($value)) { + return $this->addError('The ' . $this->field . ' not is array data.'); + } + return true; + } + + /** + * @param $value + * @return bool + */ + public function stringFormat($value): bool + { + if (is_array($value) || is_object($value) || is_bool($value)) { + return $this->addError('The ' . $this->field . ' not is string data.'); + } + return true; + } + + /** + * @param $value + * @return bool + */ + public function integerFormat($value): bool + { + if (!is_numeric($value)) { + return $this->addError('The ' . $this->field . ' not is number data.'); + } + if ((int)$value != $value) { + return $this->addError('The ' . $this->field . ' not is number data.'); + } + + return true; + } + + /** + * @param $value + * @return bool + */ + public function floatFormat($value): bool + { + $trim = (float)$value; + if ($trim != $value || !is_float($trim)) { + return $this->addError('The ' . $this->field . ' not is float data.'); + } + return true; + } + +} diff --git a/UniqueValidator.php b/UniqueValidator.php index 5e8453f..6b63ac5 100644 --- a/UniqueValidator.php +++ b/UniqueValidator.php @@ -1,41 +1,41 @@ -getParams(); - if (empty($param) || !isset($param[$this->field])) { - return TRUE; - } - - if (empty($this->model)) { - return $this->addError('Model error.'); - } - if (!$this->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; - } - - -} +getParams(); + if (empty($param) || !isset($param[$this->field])) { + return TRUE; + } + + if (empty($this->model)) { + return $this->addError('Model error.'); + } + if (!$this->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; + } + + +} diff --git a/Validator.php b/Validator.php index 6e7c648..0711bc8 100644 --- a/Validator.php +++ b/Validator.php @@ -1,221 +1,221 @@ - [ - '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; - } - - /** - * @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(); - foreach ($field as $val) { - $this->createRule($val, $rules, $model, $param); - } - - return $this; - } - - /** - * @param $field - * @param $rule - * @param $model - * @param $param - * @throws Exception - * ['maxLength'=>150, 'required', 'minLength' => 100] - */ - public function createRule($field, $rule, $model, $param) - { - $define = ['field' => $field]; - - $is_model = is_null($model); - foreach ($rule as $key => $val) { - if (!$is_model) { - if (is_string($val) && method_exists($model, $val)) { - $this->validators[] = [$model, $val]; - continue; - } - } - if (is_string($key)) { - $type = strtolower($key); - $define['value'] = $val; - } else { - $type = strtolower($val); - } - if (!isset($this->classMap[$type])) { - continue; - } - $this->validators[] = array_merge($this->classMap[$type], $define, [ - 'params' => $param, - 'model' => $model - ]); - } - } - - /** - * @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($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]; - } - - $class = Kiri::getDi()->get($val['class']); - unset($val['class']); - - Kiri::configure($class, $val); - - return [$class->trigger(), $class]; - } - -} + [ + '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; + } + + /** + * @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(); + foreach ($field as $val) { + $this->createRule($val, $rules, $model, $param); + } + + return $this; + } + + /** + * @param $field + * @param $rule + * @param $model + * @param $param + * @throws Exception + * ['maxLength'=>150, 'required', 'minLength' => 100] + */ + public function createRule($field, $rule, $model, $param) + { + $define = ['field' => $field]; + + $is_model = is_null($model); + foreach ($rule as $key => $val) { + if (!$is_model) { + if (is_string($val) && method_exists($model, $val)) { + $this->validators[] = [$model, $val]; + continue; + } + } + if (is_string($key)) { + $type = strtolower($key); + $define['value'] = $val; + } else { + $type = strtolower($val); + } + if (!isset($this->classMap[$type])) { + continue; + } + $this->validators[] = array_merge($this->classMap[$type], $define, [ + 'params' => $param, + 'model' => $model + ]); + } + } + + /** + * @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($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]; + } + + $class = Kiri::getDi()->get($val['class']); + unset($val['class']); + + Kiri::configure($class, $val); + + return [$class->trigger(), $class]; + } + +}