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