Skip to content

Commit

Permalink
Rename db validator
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhhui committed Mar 9, 2018
1 parent 45846dd commit 797a81e
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 269 deletions.
27 changes: 27 additions & 0 deletions src/Validator/BooleanValidator.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Validator/DatetimeValidator.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Validator/EnumValidator.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Validator/FloatValidator.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Validator/IntegerValidator.php
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;
}
}
27 changes: 27 additions & 0 deletions src/Validator/NumberValidator.php
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;
}
}
30 changes: 30 additions & 0 deletions src/Validator/StringValidator.php
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;
}
}
22 changes: 8 additions & 14 deletions src/Validator/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
36 changes: 0 additions & 36 deletions src/Validator/ValidatorInterfaceBoolean.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Validator/ValidatorInterfaceDatetime.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Validator/ValidatorInterfaceEnum.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Validator/ValidatorInterfaceFloat.php

This file was deleted.

Loading

0 comments on commit 797a81e

Please sign in to comment.