-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45846dd
commit 797a81e
Showing
15 changed files
with
200 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbBooleanValidator") | ||
*/ | ||
class BooleanValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! \is_bool($value)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是bool类型,column=' . $column); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbDatetimeValidator") | ||
*/ | ||
class DatetimeValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (strtotime($value) === false) { | ||
throw new ValidatorException('数据库字段值验证失败,不是dateTime类型,column=' . $column); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbEnumValidator") | ||
*/ | ||
class EnumValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! isset($params[0][0]) || ! \in_array($value, $params[0][0], false)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是在枚举集合的里面,column=' . $column . ' enum=' . json_encode($params[0][0])); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbFloatValidator") | ||
*/ | ||
class FloatValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! \is_float($value)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是float类型,column=' . $column); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbIntegerValidator") | ||
*/ | ||
class IntegerValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! \is_int($value)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是int类型,column=' . $column); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbNumberValidator") | ||
*/ | ||
class NumberValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! \is_numeric($value)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是number类型,column=' . $column); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbStringValidator") | ||
*/ | ||
class StringValidator implements ValidatorInterface | ||
{ | ||
/** | ||
* @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 $column, $value, ...$params): bool | ||
{ | ||
if (! \is_string($value)) { | ||
throw new ValidatorException('数据库字段值验证失败,不是string类型,column=' . $column); | ||
} | ||
if (isset($params[0]) && \is_int($params[0]) && mb_strlen($value) > $params[0]) { | ||
throw new ValidatorException('数据库字段值验证失败,string类型,column=' . $column . ',字符串超过最大长度,length=' . $params[0]); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,18 @@ | |
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* 通用验证器接口 | ||
* Interface ValidatorInterface | ||
* | ||
* @uses ValidatorInterface | ||
* @version 2017年09月12日 | ||
* @author stelin <[email protected]> | ||
* @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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.