|
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\ValidationException; |
|
17
|
|
|
use Respect\Validation\Validatable; |
|
18
|
|
|
use function array_shift; |
|
19
|
|
|
use function count; |
|
20
|
|
|
use function current; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
|
24
|
|
|
* @author Caio César Tavares <[email protected]> |
|
25
|
|
|
* @author Henrique Moody <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class Not extends AbstractRule |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Validatable |
|
31
|
|
|
*/ |
|
32
|
|
|
private $rule; |
|
33
|
|
|
|
|
34
|
157 |
|
public function __construct(Validatable $rule) |
|
35
|
|
|
{ |
|
36
|
157 |
|
$this->rule = $this->extractNegatedRule($rule); |
|
37
|
157 |
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
public function getNegatedRule(): Validatable |
|
40
|
|
|
{ |
|
41
|
6 |
|
return $this->rule; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
public function setName(string $name): Validatable |
|
45
|
|
|
{ |
|
46
|
6 |
|
$this->rule->setName($name); |
|
47
|
|
|
|
|
48
|
6 |
|
return parent::setName($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
152 |
|
public function validate($input): bool |
|
55
|
|
|
{ |
|
56
|
152 |
|
return $this->rule->validate($input) === false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritDoc} |
|
61
|
|
|
*/ |
|
62
|
151 |
|
public function assert($input): void |
|
63
|
|
|
{ |
|
64
|
151 |
|
if ($this->validate($input)) { |
|
65
|
3 |
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
149 |
|
$rule = $this->rule; |
|
69
|
149 |
|
if ($rule instanceof AllOf) { |
|
70
|
5 |
|
$rule = $this->absorbAllOf($rule, $input); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
149 |
|
$exception = $rule->reportError($input); |
|
74
|
149 |
|
$exception->updateMode(ValidationException::MODE_NEGATIVE); |
|
75
|
|
|
|
|
76
|
149 |
|
throw $exception; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param mixed $input |
|
81
|
|
|
*/ |
|
82
|
5 |
|
private function absorbAllOf(AllOf $rule, $input): Validatable |
|
83
|
|
|
{ |
|
84
|
5 |
|
$rules = $rule->getRules(); |
|
85
|
5 |
|
while (($current = array_shift($rules))) { |
|
86
|
5 |
|
$rule = $current; |
|
87
|
5 |
|
if (!$rule instanceof AllOf) { |
|
88
|
5 |
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
if (!$rule->validate($input)) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$rules = $rule->getRules(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
return $rule; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
157 |
|
private function extractNegatedRule(Validatable $rule): Validatable |
|
102
|
|
|
{ |
|
103
|
157 |
|
if ($rule instanceof self && $rule->getNegatedRule() instanceof self) { |
|
104
|
2 |
|
return $this->extractNegatedRule($rule->getNegatedRule()->getNegatedRule()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
157 |
|
if (!$rule instanceof AllOf) { |
|
108
|
151 |
|
return $rule; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
153 |
|
$rules = $rule->getRules(); |
|
112
|
153 |
|
if (count($rules) === 1) { |
|
113
|
150 |
|
return $this->extractNegatedRule(current($rules)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
7 |
|
return $rule; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|