|
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 is_scalar; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractRelated extends AbstractRule |
|
21
|
|
|
{ |
|
22
|
|
|
public $mandatory = true; |
|
23
|
|
|
public $reference = ''; |
|
24
|
|
|
public $validator; |
|
25
|
|
|
|
|
26
|
|
|
abstract public function hasReference($input); |
|
27
|
|
|
|
|
28
|
|
|
abstract public function getReferenceValue($input); |
|
29
|
|
|
|
|
30
|
10 |
|
public function __construct($reference, Validatable $validator = null, $mandatory = true) |
|
31
|
|
|
{ |
|
32
|
10 |
|
if (is_scalar($reference)) { |
|
33
|
10 |
|
$this->setId($reference); |
|
34
|
10 |
|
if ($validator && !$validator->getId()) { |
|
35
|
5 |
|
$validator->setId($reference); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
10 |
|
$this->reference = $reference; |
|
40
|
10 |
|
$this->validator = $validator; |
|
41
|
10 |
|
$this->mandatory = $mandatory; |
|
42
|
10 |
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
public function setId($name) |
|
45
|
|
|
{ |
|
46
|
3 |
|
parent::setId($name); |
|
47
|
|
|
|
|
48
|
3 |
|
if ($this->validator instanceof Validatable) { |
|
49
|
|
|
$this->validator->setId($name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
private function decision($type, $hasReference, $input) |
|
56
|
|
|
{ |
|
57
|
1 |
|
return (!$this->mandatory && !$hasReference) |
|
58
|
1 |
|
|| (is_null($this->validator) |
|
59
|
1 |
|
|| $this->validator->$type($this->getReferenceValue($input))); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function assert($input): void |
|
63
|
|
|
{ |
|
64
|
|
|
$hasReference = $this->hasReference($input); |
|
65
|
|
|
if ($this->mandatory && !$hasReference) { |
|
66
|
|
|
throw $this->reportError($input, ['hasReference' => false]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$this->decision('assert', $hasReference, $input); |
|
71
|
|
|
} catch (ValidationException $e) { |
|
72
|
|
|
throw $this |
|
73
|
|
|
->reportError($this->reference, ['hasReference' => true]) |
|
74
|
|
|
->addRelated($e); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function check($input): void |
|
79
|
|
|
{ |
|
80
|
|
|
$hasReference = $this->hasReference($input); |
|
81
|
|
|
if ($this->mandatory && !$hasReference) { |
|
82
|
|
|
throw $this->reportError($input, ['hasReference' => false]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->decision('check', $hasReference, $input); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
public function validate($input): bool |
|
89
|
|
|
{ |
|
90
|
2 |
|
$hasReference = $this->hasReference($input); |
|
91
|
2 |
|
if ($this->mandatory && !$hasReference) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $this->decision('validate', $hasReference, $input); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|