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