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; - } -}