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