From 797a81e50401ba8391f6c635cbfa989094354520 Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Sat, 10 Mar 2018 03:02:38 +0800 Subject: [PATCH] Rename db validator --- src/Validator/BooleanValidator.php | 27 ++++++++++++++ src/Validator/DatetimeValidator.php | 27 ++++++++++++++ src/Validator/EnumValidator.php | 27 ++++++++++++++ src/Validator/FloatValidator.php | 27 ++++++++++++++ src/Validator/IntegerValidator.php | 27 ++++++++++++++ src/Validator/NumberValidator.php | 27 ++++++++++++++ src/Validator/StringValidator.php | 30 +++++++++++++++ src/Validator/ValidatorInterface.php | 22 ++++------- src/Validator/ValidatorInterfaceBoolean.php | 36 ------------------ src/Validator/ValidatorInterfaceDatetime.php | 36 ------------------ src/Validator/ValidatorInterfaceEnum.php | 36 ------------------ src/Validator/ValidatorInterfaceFloat.php | 36 ------------------ src/Validator/ValidatorInterfaceInt.php | 36 ------------------ src/Validator/ValidatorInterfaceNumber.php | 36 ------------------ src/Validator/ValidatorInterfaceString.php | 39 -------------------- 15 files changed, 200 insertions(+), 269 deletions(-) create mode 100644 src/Validator/BooleanValidator.php create mode 100644 src/Validator/DatetimeValidator.php create mode 100644 src/Validator/EnumValidator.php create mode 100644 src/Validator/FloatValidator.php create mode 100644 src/Validator/IntegerValidator.php create mode 100644 src/Validator/NumberValidator.php create mode 100644 src/Validator/StringValidator.php delete mode 100644 src/Validator/ValidatorInterfaceBoolean.php delete mode 100644 src/Validator/ValidatorInterfaceDatetime.php delete mode 100644 src/Validator/ValidatorInterfaceEnum.php delete mode 100644 src/Validator/ValidatorInterfaceFloat.php delete mode 100644 src/Validator/ValidatorInterfaceInt.php delete mode 100644 src/Validator/ValidatorInterfaceNumber.php delete mode 100644 src/Validator/ValidatorInterfaceString.php diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php new file mode 100644 index 0000000..6b16a81 --- /dev/null +++ b/src/Validator/BooleanValidator.php @@ -0,0 +1,27 @@ + $params[0]) { + throw new ValidatorException('数据库字段值验证失败,string类型,column=' . $column . ',字符串超过最大长度,length=' . $params[0]); + } + return true; + } +} diff --git a/src/Validator/ValidatorInterface.php b/src/Validator/ValidatorInterface.php index e6c1749..2cbbeda 100644 --- a/src/Validator/ValidatorInterface.php +++ b/src/Validator/ValidatorInterface.php @@ -5,24 +5,18 @@ use Swoft\Exception\ValidatorException; /** - * 通用验证器接口 + * Interface ValidatorInterface * - * @uses ValidatorInterface - * @version 2017年09月12日 - * @author stelin - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} + * @package Swoft\Db\Validator */ interface ValidatorInterface { /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 + * @param string $column Colunm name + * @param mixed $value Column value + * @param array ...$params Other parameters + * @throws ValidatorException When validation failures, will throw an Exception + * @return bool When validation successful */ - public function validate(string $cloum, $value, ...$params): bool; + public function validate(string $column, $value, ...$params): bool; } diff --git a/src/Validator/ValidatorInterfaceBoolean.php b/src/Validator/ValidatorInterfaceBoolean.php deleted file mode 100644 index 38d3533..0000000 --- a/src/Validator/ValidatorInterfaceBoolean.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceBoolean implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! \is_bool($value)) { - throw new ValidatorException('数据库字段值验证失败,不是bool类型,column=' . $cloum); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceDatetime.php b/src/Validator/ValidatorInterfaceDatetime.php deleted file mode 100644 index c073f14..0000000 --- a/src/Validator/ValidatorInterfaceDatetime.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceDatetime implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (strtotime($value) === false) { - throw new ValidatorException('数据库字段值验证失败,不是dateTime类型,column=' . $cloum); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceEnum.php b/src/Validator/ValidatorInterfaceEnum.php deleted file mode 100644 index a2117ac..0000000 --- a/src/Validator/ValidatorInterfaceEnum.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceEnum implements ValidatorInterface -{ - /** - * 枚举类型验证 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! isset($params[0][0]) || ! \in_array($value, $params[0][0])) { - throw new ValidatorException('数据库字段值验证失败,不是在枚举集合的里面,column=' . $cloum . ' enum=' . json_encode($params[0][0])); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceFloat.php b/src/Validator/ValidatorInterfaceFloat.php deleted file mode 100644 index a3f90d4..0000000 --- a/src/Validator/ValidatorInterfaceFloat.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceFloat implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! \is_float($value)) { - throw new ValidatorException('数据库字段值验证失败,不是float类型,column=' . $cloum); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceInt.php b/src/Validator/ValidatorInterfaceInt.php deleted file mode 100644 index 2e275c2..0000000 --- a/src/Validator/ValidatorInterfaceInt.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceInt implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! \is_int($value)) { - throw new ValidatorException('数据库字段值验证失败,不是int类型,column=' . $cloum); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceNumber.php b/src/Validator/ValidatorInterfaceNumber.php deleted file mode 100644 index 87a9914..0000000 --- a/src/Validator/ValidatorInterfaceNumber.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceNumber implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! is_integer($value)) { - throw new ValidatorException('数据库字段值验证失败,不是number类型,column=' . $cloum); - } - return true; - } -} diff --git a/src/Validator/ValidatorInterfaceString.php b/src/Validator/ValidatorInterfaceString.php deleted file mode 100644 index dc20664..0000000 --- a/src/Validator/ValidatorInterfaceString.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright Copyright 2010-2016 swoft software - * @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} - */ -class ValidatorInterfaceString implements ValidatorInterface -{ - /** - * 验证结果 - * - * @param string $cloum 字段名称 - * @param mixed $value 字段传入值 - * @param array ...$params 其它参数数组传入 - * @throws ValidatorException - * @return bool 成功返回true,失败抛异常 - */ - public function validate(string $cloum, $value, ...$params): bool - { - if (! \is_string($value)) { - throw new ValidatorException('数据库字段值验证失败,不是string类型,column=' . $cloum); - } - if (isset($params[0]) && \is_int($params[0]) && mb_strlen($value) > $params[0]) { - throw new ValidatorException('数据库字段值验证失败,string类型,column=' . $cloum . ',字符串超过最大长度,length=' . $params[0]); - } - return true; - } -}