|
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
|
|
|
namespace Respect\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use Respect\Validation\Result; |
|
15
|
|
|
use Respect\Validation\Rule; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
|
19
|
|
|
* @author Henrique Moody <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @since 0.3.9 |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractRelated implements Rule |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $reference; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Rule|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private $rule; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $mandatory = true; |
|
39
|
|
|
|
|
40
|
1 |
|
public function __construct($reference, Rule $rule = null, bool $mandatory = true) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->reference = $reference; |
|
43
|
1 |
|
$this->rule = $rule; |
|
44
|
1 |
|
$this->mandatory = $mandatory; |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $input |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function hasReference($input, $reference): bool; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $input |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function getReferenceValue($input, $reference); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
21 |
|
public function validate($input): Result |
|
65
|
|
|
{ |
|
66
|
21 |
|
$properties = ['reference' => $this->reference, 'mandatory' => $this->mandatory]; |
|
67
|
|
|
|
|
68
|
21 |
|
if (!$this->hasReference($input, $this->reference)) { |
|
69
|
8 |
|
return new Result(!$this->mandatory, $input, $this, $properties); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
13 |
|
if ($this->rule === null) { |
|
73
|
4 |
|
return new Result(true, $input, $this, $properties); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
$referenceValue = $this->getReferenceValue($input, $this->reference); |
|
77
|
9 |
|
$referenceValueResult = $this->rule->validate($referenceValue); |
|
78
|
|
|
|
|
79
|
9 |
|
return new Result($referenceValueResult->isValid(), $input, $this, $properties, $referenceValueResult); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|