Skip to content

Commit d1d4916

Browse files
authored
Merge pull request #26 from xingshuwangluo/master
增加 闭包验证类 功能
2 parents eb5fa57 + 276fd3a commit d1d4916

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,30 @@ $v = Validation::make($_POST,[
324324
}]
325325
```
326326

327+
- **方式4**定义一个闭包验证类进行验证,这种方法能提高验证方法的复用性
328+
329+
> 别忘了继承 `\Inhere\Validate\Validator\AbstractValidator`,和实现必须方法`validate`
330+
331+
```php
332+
333+
class AdemoValidator extends \Inhere\Validate\Validator\AbstractValidator
334+
{
335+
336+
337+
public function validate($value, $data): bool
338+
{
339+
if ($value == 1) {
340+
return true;
341+
}
342+
return false;
343+
}
344+
345+
}
346+
347+
['status', new AdemoValidator()]
348+
```
349+
350+
327351
## 一个完整的规则示例
328352

329353
一个完整的规则示例, 包含了所有可添加的项。

src/Validator/AbstractValidator.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Inhere\Validate\Validator;
4+
5+
abstract class AbstractValidator implements ValidatorInterface
6+
{
7+
8+
/**
9+
* 魔术方法,在试图函数式使用对象是调用
10+
* @param type $value
11+
* @param type $data
12+
* @return bool
13+
*/
14+
public function __invoke($value, $data): bool {
15+
return (bool) $this->validate($value, $data);
16+
}
17+
18+
}

src/Validator/ValidatorInterface.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Inhere\Validate\Validator;
4+
5+
/**
6+
*
7+
* 验证器借口
8+
*/
9+
interface ValidatorInterface
10+
{
11+
12+
/**
13+
* 验证方法,进行验证返回bool类型
14+
* @param type $value 当前值
15+
* @param type $data 全部的值
16+
* @return bool
17+
*/
18+
public function validate($value, $data): bool;
19+
20+
}

test/RuleValidationTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Runner\Version;
1010
use Throwable;
1111
use function version_compare;
12+
use Inhere\ValidateTest\Validator\AdemoValidatorTest as AdemoValidator;
1213

1314
/**
1415
* Class RuleValidationTest
@@ -432,9 +433,32 @@ function () {
432433
},
433434
'msg' => 'userId check failure by closure!'
434435
],
436+
435437
];
436438
}
437439

440+
/**
441+
* 测试自定义验证器
442+
*/
443+
public function testValidator()
444+
{
445+
$rule=[
446+
[
447+
'user',
448+
new AdemoValidator(),
449+
'msg' => 'userId check failure by closure!'
450+
],
451+
];
452+
$data=[
453+
'user'=>1
454+
];
455+
$validation = Validation::makeAndValidate($data, $rule);
456+
$this->assertTrue($validation->isOk());
457+
$validation = Validation::makeAndValidate(['user'=>2], $rule);
458+
$this->assertTrue($validation->isFail());
459+
}
460+
461+
438462
public function testArrayValidate(): void
439463
{
440464
$data = [

test/Validator/AdemoValidatorTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Inhere\ValidateTest\Validator;
4+
5+
/**
6+
* Class ClassValidator
7+
* @package Inhere\ValidateTest\Validator
8+
*/
9+
class AdemoValidatorTest extends \Inhere\Validate\Validator\AbstractValidator
10+
{
11+
12+
13+
public function validate($value, $data): bool
14+
{
15+
if ($value == 1) {
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)