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\NestedValidationException; |
17
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
18
|
|
|
use Respect\Validation\Validatable; |
19
|
|
|
use function is_scalar; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
23
|
|
|
* @author Emmerson Siqueira <[email protected]> |
24
|
|
|
* @author Henrique Moody <[email protected]> |
25
|
|
|
* @author Nick Lombard <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractRelated extends AbstractRule |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $mandatory = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
private $reference; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Validatable|null |
41
|
|
|
*/ |
42
|
|
|
private $rule; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $input |
46
|
|
|
*/ |
47
|
|
|
abstract public function hasReference($input): bool; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $input |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
abstract public function getReferenceValue($input); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $reference |
58
|
|
|
*/ |
59
|
|
|
public function __construct($reference, ?Validatable $rule = null, bool $mandatory = true) |
60
|
26 |
|
{ |
61
|
|
|
if (is_scalar($reference)) { |
62
|
26 |
|
$this->setName((string) $reference); |
63
|
26 |
|
if ($rule && !$rule->getName()) { |
64
|
26 |
|
$rule->setName((string) $reference); |
65
|
21 |
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->reference = $reference; |
69
|
26 |
|
$this->rule = $rule; |
70
|
26 |
|
$this->mandatory = $mandatory; |
71
|
26 |
|
} |
72
|
26 |
|
|
73
|
|
|
/** |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function getReference() |
77
|
19 |
|
{ |
78
|
|
|
return $this->reference; |
79
|
19 |
|
} |
80
|
|
|
|
81
|
19 |
|
public function isMandatory(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->mandatory; |
84
|
|
|
} |
85
|
19 |
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function setName(string $name): Validatable |
90
|
|
|
{ |
91
|
13 |
|
parent::setName($name); |
92
|
|
|
|
93
|
13 |
|
if ($this->rule instanceof Validatable) { |
94
|
13 |
|
$this->rule->setName($name); |
95
|
7 |
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
13 |
|
|
100
|
11 |
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
11 |
|
*/ |
103
|
11 |
|
public function assert($input): void |
104
|
|
|
{ |
105
|
11 |
|
$hasReference = $this->hasReference($input); |
106
|
|
|
if ($this->mandatory && !$hasReference) { |
107
|
6 |
|
throw $this->reportError($input, ['hasReference' => false]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->rule === null || !$hasReference) { |
111
|
|
|
return; |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
try { |
115
|
2 |
|
$this->rule->assert($this->getReferenceValue($input)); |
116
|
|
|
} catch (ValidationException $validationException) { |
117
|
|
|
/** @var NestedValidationException $nestedValidationException */ |
118
|
|
|
$nestedValidationException = $this->reportError($this->reference, ['hasReference' => true]); |
119
|
2 |
|
$nestedValidationException->addChild($validationException); |
120
|
1 |
|
|
121
|
|
|
throw $nestedValidationException; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
3 |
|
*/ |
128
|
3 |
|
public function check($input): void |
129
|
2 |
|
{ |
130
|
|
|
$hasReference = $this->hasReference($input); |
131
|
|
|
if ($this->mandatory && !$hasReference) { |
132
|
2 |
|
throw $this->reportError($input, ['hasReference' => false]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->rule === null || !$hasReference) { |
136
|
|
|
return; |
137
|
|
|
} |
138
|
17 |
|
|
139
|
|
|
$this->rule->check($this->getReferenceValue($input)); |
140
|
17 |
|
} |
141
|
17 |
|
|
142
|
17 |
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function validate($input): bool |
146
|
|
|
{ |
147
|
|
|
$hasReference = $this->hasReference($input); |
148
|
|
|
if ($this->mandatory && !$hasReference) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($this->rule === null || !$hasReference) { |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->rule->validate($this->getReferenceValue($input)); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|