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