Skip to content

Commit

Permalink
Text 注解支持字符验证 #46
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Feb 14, 2020
1 parent 6936546 commit 327f8d1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
20 changes: 18 additions & 2 deletions doc/components/validation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,30 @@ class TestValidate

文本验证

必须>=6位长度,最长不限制:
#### 字节验证

必须>=6位长度字节,最长不限制:

`@Text(min=6)`

长度必须>=6 && <=12:
字节长度必须>=6 && <=12:

`@Text(min=6, max=12)`

> PHP 中,UTF-8 编码的中文字,每个字节长度为 3
#### 字符验证

必须>=6位长度字符,最长不限制:

`@Text(char=true, min=6)`

字符长度必须>=6 && <=12:

`@Text(char=true, min=6, max=12)`

> 一个字母是一个字符,一个中文也是一个字符
### @Integer

整数验证
Expand Down
12 changes: 11 additions & 1 deletion src/Validate/Annotation/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class Text extends Condition
{
/**
* 是否为字符模式,默认为 false
* 设为 true 则使用字符判断长度
* 设为 false 则使用字节判断长度
*
* @var boolean
*/
public $char = false;

/**
* 最短长度
*
Expand All @@ -32,7 +41,7 @@ class Text extends Condition
*
* @var callable
*/
public $callable = '\Imi\Validate\ValidatorHelper::length';
public $callable = '\Imi\Validate\ValidatorHelper::text';

/**
* 参数名数组
Expand All @@ -43,5 +52,6 @@ class Text extends Condition
'{:value}',
'{min}',
'{max}',
'{char}',
];
}
22 changes: 22 additions & 0 deletions src/Validate/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,26 @@ public static function cnIdcard($id_card)
return false;
}
}

/**
* 文本验证
*
* @param string $str
* @param integer $min
* @param integer|null $max
* @param boolean $char
* @return bool
*/
public static function text(string $str, int $min, ?int $max = null, bool $char = false): bool
{
if($char)
{
return static::lengthChar($str, $min, $max);
}
else
{
return static::length($str, $min, $max);
}
}

}
18 changes: 18 additions & 0 deletions tests/unit/Component/Tests/ValidatorAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testValidatorAnnotation()
$this->requiredFail();
$this->numberFail();
$this->textFail();
$this->textCharFail();
$this->validateValueFail();
$this->optional();
}
Expand Down Expand Up @@ -81,6 +82,7 @@ private function initData()
'required' => '',
'number' => 1,
'text' => 'imiphp.com',
'chars' => 'imiphp.com',
'validateValue' => -1,
'optional' => 1,
];
Expand Down Expand Up @@ -168,6 +170,22 @@ private function textFail()
$this->assertEquals('text参数长度必须>=6 && <=12', $this->tester->getMessage());
}

private function textCharFail()
{
$this->initData();

$this->data['chars'] = '这个可以通过';
$this->assertTrue($this->tester->validate());

$this->data['chars'] = '测试不通过';
$this->assertFalse($this->tester->validate());
$this->assertEquals('chars参数长度必须>=6 && <=12', $this->tester->getMessage());

$this->data['chars'] = '测试不通过测试不通过测试不通过';
$this->assertFalse($this->tester->validate());
$this->assertEquals('chars参数长度必须>=6 && <=12', $this->tester->getMessage());
}

private function validateValueFail()
{
$this->initData();
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Component/Validate/Classes/TestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @Required(name="required", message="{name}为必须参数")
* @Number(name="number", min=0.01, max=999.99, accuracy=2, message="数值必须大于等于{min},小于等于{max},小数点最多保留{accuracy}位小数,当前值为{:value}")
* @Text(name="text", min=6, max=12, message="{name}参数长度必须>={min} && <={max}")
* @Text(name="chars", char=true, min=6, max=12, message="{name}参数长度必须>={min} && <={max}")
* @Compare(name="validateValue", value=@ValidateValue("{:data.compare}"), operation="==")
* @Integer(name="optional", min=0, max=100, message="{:value} 不符合大于等于{min}且小于等于{max}", optional=true)
*/
Expand Down

0 comments on commit 327f8d1

Please sign in to comment.