1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\AnyOfException; |
17
|
|
|
use Respect\Validation\Exceptions\GroupedValidationException; |
18
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
22
|
|
|
* @author Henrique Moody <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class AnyOf extends AbstractComposite |
25
|
|
|
{ |
26
|
2 |
|
public function assert($input): void |
27
|
|
|
{ |
28
|
2 |
|
$validators = $this->getRules(); |
29
|
2 |
|
$exceptions = $this->getAllThrownExceptions($input); |
30
|
2 |
|
$numRules = count($validators); |
31
|
2 |
|
$numExceptions = count($exceptions); |
32
|
2 |
|
if ($numExceptions === $numRules) { |
33
|
|
|
/** @var AnyOfException $anyOfException */ |
34
|
1 |
|
$anyOfException = $this->reportError($input); |
35
|
1 |
|
$anyOfException->addChildren($exceptions); |
36
|
|
|
|
37
|
1 |
|
throw $anyOfException; |
38
|
|
|
} |
39
|
1 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function validate($input): bool |
42
|
|
|
{ |
43
|
4 |
|
foreach ($this->getRules() as $v) { |
44
|
4 |
|
if ($v->validate($input)) { |
45
|
2 |
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function check($input): void |
53
|
|
|
{ |
54
|
2 |
|
foreach ($this->getRules() as $v) { |
55
|
|
|
try { |
56
|
2 |
|
$v->check($input); |
57
|
|
|
|
58
|
1 |
|
return; |
59
|
2 |
|
} catch (ValidationException $e) { |
60
|
2 |
|
if (!isset($firstException)) { |
61
|
2 |
|
$firstException = $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if (isset($firstException)) { |
67
|
1 |
|
throw $firstException; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw $this->reportError($input); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|