-
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.
增加DB验证器 (swoft-cloud/swoft-component#146)
* 增加DB验证器 * bigint validate * 增加bigint、tinyint、smallint Validator的单测 * 完善bigint Validator 以及对应单测 * 完善单测
- Loading branch information
1 parent
a10b862
commit b3ec760
Showing
6 changed files
with
240 additions
and
9 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
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,46 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://doc.swoft.org | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbBigintValidator") | ||
*/ | ||
class BigintValidator 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 | ||
{ | ||
$min = '-9223372036854775808'; | ||
$max = '18446744073709551615'; | ||
|
||
if (!preg_match("/^-?[1-9][0-9]*$/", $value)) { | ||
throw new ValidatorException("数据库字段值验证失败,当前值 {$value} 不为有效整数,column={$column}"); | ||
} | ||
|
||
if (bccomp($value, $min) === -1) { | ||
throw new ValidatorException("数据库字段值验证失败,当前值小于{$min},column={$column}"); | ||
} | ||
|
||
if(bccomp($value, $max) === 1){ | ||
throw new ValidatorException("数据库字段值验证失败,当前值大于{$max},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,37 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://doc.swoft.org | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbSmallintValidator") | ||
*/ | ||
class SmallintValidator 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); | ||
} | ||
if ($value > 65535 || $value < -32768) { | ||
throw new ValidatorException('数据库字段值验证失败,字段超过smallint大小范围,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,37 @@ | ||
<?php | ||
/** | ||
* This file is part of Swoft. | ||
* | ||
* @link https://swoft.org | ||
* @document https://doc.swoft.org | ||
* @contact [email protected] | ||
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE | ||
*/ | ||
namespace Swoft\Db\Validator; | ||
|
||
use Swoft\Bean\Annotation\Bean; | ||
use Swoft\Exception\ValidatorException; | ||
|
||
/** | ||
* @Bean("DbTinyintValidator") | ||
*/ | ||
class TinyintValidator 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); | ||
} | ||
if ($value > 255 || $value < -128) { | ||
throw new ValidatorException('数据库字段值验证失败,字段超过tinyint大小范围,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
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