Skip to content

Commit

Permalink
Merge pull request #26 from xingshuwangluo/master
Browse files Browse the repository at this point in the history
增加 闭包验证类 功能
  • Loading branch information
inhere committed Mar 10, 2020
2 parents eb5fa57 + 276fd3a commit d1d4916
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ $v = Validation::make($_POST,[
}]
```

- **方式4**定义一个闭包验证类进行验证,这种方法能提高验证方法的复用性

> 别忘了继承 `\Inhere\Validate\Validator\AbstractValidator`,和实现必须方法`validate`
```php

class AdemoValidator extends \Inhere\Validate\Validator\AbstractValidator
{


public function validate($value, $data): bool
{
if ($value == 1) {
return true;
}
return false;
}

}

['status', new AdemoValidator()]
```


## 一个完整的规则示例

一个完整的规则示例, 包含了所有可添加的项。
Expand Down
18 changes: 18 additions & 0 deletions src/Validator/AbstractValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Inhere\Validate\Validator;

abstract class AbstractValidator implements ValidatorInterface
{

/**
* 魔术方法,在试图函数式使用对象是调用
* @param type $value
* @param type $data
* @return bool
*/
public function __invoke($value, $data): bool {
return (bool) $this->validate($value, $data);
}

}
20 changes: 20 additions & 0 deletions src/Validator/ValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Inhere\Validate\Validator;

/**
*
* 验证器借口
*/
interface ValidatorInterface
{

/**
* 验证方法,进行验证返回bool类型
* @param type $value 当前值
* @param type $data 全部的值
* @return bool
*/
public function validate($value, $data): bool;

}
24 changes: 24 additions & 0 deletions test/RuleValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Runner\Version;
use Throwable;
use function version_compare;
use Inhere\ValidateTest\Validator\AdemoValidatorTest as AdemoValidator;

/**
* Class RuleValidationTest
Expand Down Expand Up @@ -432,9 +433,32 @@ function () {
},
'msg' => 'userId check failure by closure!'
],

];
}

/**
* 测试自定义验证器
*/
public function testValidator()
{
$rule=[
[
'user',
new AdemoValidator(),
'msg' => 'userId check failure by closure!'
],
];
$data=[
'user'=>1
];
$validation = Validation::makeAndValidate($data, $rule);
$this->assertTrue($validation->isOk());
$validation = Validation::makeAndValidate(['user'=>2], $rule);
$this->assertTrue($validation->isFail());
}


public function testArrayValidate(): void
{
$data = [
Expand Down
21 changes: 21 additions & 0 deletions test/Validator/AdemoValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Inhere\ValidateTest\Validator;

/**
* Class ClassValidator
* @package Inhere\ValidateTest\Validator
*/
class AdemoValidatorTest extends \Inhere\Validate\Validator\AbstractValidator
{


public function validate($value, $data): bool
{
if ($value == 1) {
return true;
}
return false;
}

}

0 comments on commit d1d4916

Please sign in to comment.