1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lavibi\Popoya; |
4
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ValidatorChain |
9
|
|
|
* |
10
|
|
|
* @package Lavibi\Popoya |
11
|
|
|
* |
12
|
|
|
* @method ValidatorChain sameAs(mixed $value) |
13
|
|
|
* @method ValidatorChain notSameAs(mixed $value) |
14
|
|
|
*/ |
15
|
|
|
class ValidatorChain extends AbstractValidator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ValidatorInterface[] |
19
|
|
|
*/ |
20
|
|
|
protected $validators = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* List of special methods of validators to set option value. |
24
|
|
|
* Each method belongs only validator. |
25
|
|
|
* Use as shortcut for addValidator method |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $setOptionMethods = [ |
30
|
|
|
'sameAs' => '\Lavibi\Popoya\Same', |
31
|
|
|
'notSameAs' => '\Lavibi\Popoya\NotSame' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List of special methods of no option validators |
36
|
|
|
* Each method belongs only validator. |
37
|
|
|
* Use as shortcut for addValidator method |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $noOptionValidator = [ |
42
|
|
|
'isUpload' => '\Lavibi\Popoya\Upload', |
43
|
|
|
'isImage' => '\Lavibi\Popoya\Image' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @param $name |
49
|
|
|
* @param $arguments |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
public function __call($name, $arguments) |
54
|
|
|
{ |
55
|
2 |
|
if (isset($this->noOptionValidator[$name])) { |
56
|
1 |
|
$validatorClass = $this->noOptionValidator[$name]; |
57
|
1 |
|
$this->validators[$validatorClass] = new $validatorClass(); |
58
|
1 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (!isset($this->setOptionMethods[$name])) { |
62
|
1 |
|
throw new InvalidArgumentException('Set option method ' . $name . ' is not support'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$validatorClass = $this->setOptionMethods[$name]; |
66
|
|
|
|
67
|
|
|
if (!isset($this->validators[$validatorClass])) { |
68
|
|
|
$this->validators[$validatorClass] = new $validatorClass(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
call_user_func_array([$this->validators[$validatorClass], $name], $arguments); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function addValidator(ValidatorInterface $validator) |
77
|
|
|
{ |
78
|
4 |
|
$this->validators[get_class($validator)] = $validator; |
79
|
|
|
|
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
public function reset() |
84
|
|
|
{ |
85
|
6 |
|
$this->validators = []; |
86
|
|
|
|
87
|
6 |
|
parent::reset(); |
88
|
|
|
|
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
public function isValid($value) |
93
|
|
|
{ |
94
|
5 |
|
$this->value = $this->standardValue = $value; |
95
|
|
|
|
96
|
5 |
|
foreach ($this->validators as $validator) { |
97
|
5 |
|
$result = $validator->isValid($this->standardValue); |
98
|
|
|
|
99
|
4 |
|
if (!$result) { |
100
|
2 |
|
$this->setErrorCode($validator->getMessageCode())->setErrorMessage($validator->getMessage()); |
101
|
2 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
$this->standardValue = $validator->getStandardValue(); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
protected function setErrorMessage($message) |
111
|
|
|
{ |
112
|
2 |
|
$this->message = $message; |
113
|
|
|
|
114
|
2 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
protected function setErrorCode($code) |
118
|
|
|
{ |
119
|
2 |
|
$this->messageCode = $code; |
120
|
|
|
|
121
|
2 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|