Skip to content

Commit 6f6a815

Browse files
committed
feat: add new validator class for support multi validator
1 parent 4eb0813 commit 6f6a815

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

src/Validator/AbstractValidator.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
namespace Toolkit\PFlag\Validator;
44

5-
use Toolkit\PFlag\AbstractFlags;
65
use Toolkit\PFlag\Contract\ValidatorInterface;
76

87
/**
98
* class AbstractValidator
109
*/
1110
abstract class AbstractValidator implements ValidatorInterface
1211
{
13-
/**
14-
* @var AbstractFlags
15-
*/
16-
protected $fs;
17-
1812
/**
1913
* @param mixed $value
2014
* @param string $name
@@ -33,20 +27,4 @@ public function __invoke($value, string $name): bool
3327
* @return bool
3428
*/
3529
abstract public function checkInput($value, string $name): bool;
36-
37-
/**
38-
* @return AbstractFlags
39-
*/
40-
public function getFs(): AbstractFlags
41-
{
42-
return $this->fs;
43-
}
44-
45-
/**
46-
* @param AbstractFlags $fs
47-
*/
48-
public function setFs(AbstractFlags $fs): void
49-
{
50-
$this->fs = $fs;
51-
}
5230
}

src/Validator/CondValidator.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
*/
1111
abstract class CondValidator extends AbstractValidator
1212
{
13+
/**
14+
* @var AbstractFlags
15+
*/
16+
protected $fs;
17+
1318
/**
1419
* Before condition check.
1520
* if return false, will skip call checkInput();
@@ -35,6 +40,22 @@ public function __invoke($value, string $name): bool
3540
return $this->checkInput($value, $name);
3641
}
3742

43+
/**
44+
* @return AbstractFlags
45+
*/
46+
public function getFs(): AbstractFlags
47+
{
48+
return $this->fs;
49+
}
50+
51+
/**
52+
* @param AbstractFlags $fs
53+
*/
54+
public function setFs(AbstractFlags $fs): void
55+
{
56+
$this->fs = $fs;
57+
}
58+
3859
/**
3960
* @param mixed $cond
4061
*

src/Validator/MultiValidator.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\PFlag\Validator;
4+
5+
use Toolkit\PFlag\Contract\ValidatorInterface;
6+
7+
/**
8+
* class MultiValidator
9+
*/
10+
class MultiValidator extends AbstractValidator
11+
{
12+
/**
13+
* @var ValidatorInterface[]
14+
*/
15+
private $validators;
16+
17+
/**
18+
* @param array $validators
19+
*
20+
* @return static
21+
*/
22+
public static function new(array $validators): self
23+
{
24+
return new self($validators);
25+
}
26+
27+
/**
28+
* Class constructor.
29+
*
30+
* @param array $validators
31+
*/
32+
public function __construct(array $validators)
33+
{
34+
$this->validators = $validators;
35+
}
36+
37+
/**
38+
* @param mixed $value
39+
* @param string $name
40+
*
41+
* @return bool
42+
*/
43+
public function checkInput($value, string $name): bool
44+
{
45+
foreach ($this->validators as $validator) {
46+
$ok = $validator($value, $name);
47+
if ($ok === false) {
48+
return false;
49+
}
50+
}
51+
52+
return true;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function __toString(): string
59+
{
60+
return '';
61+
}
62+
}

0 commit comments

Comments
 (0)