1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lavibi\Popoya; |
4
|
|
|
|
5
|
|
|
use Lavibi\Popoya\Exception\MissingOptionException; |
6
|
|
|
|
7
|
|
|
abstract class AbstractValidator implements ValidatorInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Input value |
11
|
|
|
* |
12
|
|
|
* @var mixed |
13
|
|
|
*/ |
14
|
|
|
protected $value; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Filter value for valid input value. |
18
|
|
|
* |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $standardValue; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Error messages |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $messages = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Error message |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $message; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Error code |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $messageCode; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Options. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $options = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Default options. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $defaultOptions = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Required options. |
60
|
|
|
* |
61
|
|
|
* @var string[] |
62
|
|
|
*/ |
63
|
|
|
protected $requiredOptions = []; |
64
|
|
|
|
65
|
14 |
|
public function __construct() |
66
|
|
|
{ |
67
|
14 |
|
$this->reset(); |
68
|
14 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Run validator |
72
|
|
|
* |
73
|
|
|
* @param mixed $value |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function __invoke($value) |
78
|
|
|
{ |
79
|
|
|
return $this->isValid($value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set options. |
84
|
|
|
* |
85
|
|
|
* @param array $options |
86
|
|
|
* |
87
|
|
|
* @return AbstractValidator |
88
|
|
|
*/ |
89
|
2 |
|
public function setOptions($options) |
90
|
|
|
{ |
91
|
2 |
|
$this->options = array_merge($this->options, $options); |
92
|
|
|
|
93
|
2 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get options. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
2 |
|
public function getOptions() |
102
|
|
|
{ |
103
|
2 |
|
return $this->options; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check missing options. |
108
|
|
|
* |
109
|
|
|
* @throws MissingOptionException |
110
|
|
|
*/ |
111
|
12 |
|
public function checkMissingOptions() |
112
|
|
|
{ |
113
|
12 |
|
foreach ($this->requiredOptions as $option) { |
114
|
12 |
|
if (!array_key_exists($option, $this->options)) { |
115
|
4 |
|
throw new MissingOptionException('Missing option ' . $option . ' for validator'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
9 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Reset validator. |
122
|
|
|
* |
123
|
|
|
* @return AbstractValidator |
124
|
|
|
*/ |
125
|
14 |
|
public function reset() |
126
|
|
|
{ |
127
|
14 |
|
$this->init(); |
128
|
|
|
|
129
|
14 |
|
$this->options = $this->defaultOptions; |
130
|
14 |
|
$this->standardValue = null; |
131
|
|
|
|
132
|
14 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set error. |
137
|
|
|
* |
138
|
|
|
* @param string $code |
139
|
|
|
* |
140
|
|
|
* @return AbstractValidator |
141
|
|
|
*/ |
142
|
5 |
|
protected function setError($code) |
143
|
|
|
{ |
144
|
5 |
|
$this->message = $this->messages[$code]; |
145
|
5 |
|
$this->messageCode = $code; |
146
|
|
|
|
147
|
5 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
3 |
|
public function getMessage() |
154
|
|
|
{ |
155
|
3 |
|
return $this->message; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
161
|
3 |
|
public function getMessageCode() |
162
|
|
|
{ |
163
|
3 |
|
return $this->messageCode; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get standard value |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
2 |
|
public function getStandardValue() |
172
|
|
|
{ |
173
|
2 |
|
return $this->standardValue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Init method. |
178
|
|
|
*/ |
179
|
14 |
|
protected function init() |
180
|
|
|
{ |
181
|
14 |
|
} |
182
|
|
|
} |
183
|
|
|
|