-
Notifications
You must be signed in to change notification settings - Fork 29
/
ValueValidator.php
304 lines (264 loc) · 8.3 KB
/
ValueValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace Sirius\Validation;
use Sirius\Validation\Rule\AbstractRule;
class ValueValidator
{
/**
* The error messages generated after validation or set manually
*
* @var array
*/
protected $messages = array();
/**
* Will be used to construct the rules
*
* @var \Sirius\Validation\RuleFactory
*/
protected $ruleFactory;
/**
* The prototype that will be used to generate the error message
*
* @var \Sirius\Validation\ErrorMessage
*/
protected $errorMessagePrototype;
/**
* The rule collections for the validation
*
* @var \Sirius\Validation\RuleCollection
*/
protected $rules;
/**
* The label of the value to be validated
*
* @var string
*/
protected $label;
public function __construct(
RuleFactory $ruleFactory = null,
ErrorMessage $errorMessagePrototype = null,
$label = null
) {
if (!$ruleFactory) {
$ruleFactory = new RuleFactory();
}
$this->ruleFactory = $ruleFactory;
if (!$errorMessagePrototype) {
$errorMessagePrototype = new ErrorMessage();
}
$this->errorMessagePrototype = $errorMessagePrototype;
if ($label) {
$this->label = $label;
}
$this->rules = new RuleCollection;
}
public function setLabel($label = null)
{
$this->label = $label;
return $this;
}
/**
* Add 1 or more validation rules
*
* @example
* // add multiple rules at once
* $validator->add(array(
* 'required',
* array('required', array('email', null, '{label} must be an email', 'Field B')),
* ));
*
* // add multiple rules using a string
* $validator->add('required | email');
*
* // add validator with options
* $validator->add('minlength', array('min' => 2), '{label} should have at least {min} characters', 'Field label');
*
* // add validator with string and parameters as JSON string
* $validator->add('minlength({"min": 2})({label} should have at least {min} characters)(Field label)');
*
* // add validator with string and parameters as query string
* $validator->add('minlength(min=2)({label} should have at least {min} characters)(Field label)');
*
* @param string|callback $name
* @param string|array $options
* @param string $messageTemplate
* @param string $label
*
* @return ValueValidator
*/
public function add($name, $options = null, $messageTemplate = null, $label = null)
{
if (is_array($name) && !is_callable($name)) {
return $this->addMultiple($name);
}
if (is_string($name)) {
// rule was supplied like 'required | email'
if (strpos($name, ' | ') !== false) {
return $this->addMultiple(explode(' | ', $name));
}
// rule was supplied like this 'length(2,10)(error message template)(label)'
if (strpos($name, '(') !== false) {
list($name, $options, $messageTemplate, $label) = $this->parseRule($name);
}
}
// check for the default label
if (!$label && $this->label) {
$label = $this->label;
}
$validator = $this->ruleFactory->createRule($name, $options, $messageTemplate, $label);
return $this->addRule($validator);
}
/**
* @param array $rules
*
* @return ValueValidator
*/
public function addMultiple($rules)
{
foreach ($rules as $singleRule) {
// make sure the rule is an array (the parameters of subsequent calls);
$singleRule = is_array($singleRule) ? $singleRule : array(
$singleRule
);
call_user_func_array(
array(
$this,
'add'
),
$singleRule
);
}
return $this;
}
/**
* @param AbstractValidator $validationRule
*
* @return ValueValidator
*/
public function addRule(AbstractRule $validationRule)
{
$validationRule->setErrorMessagePrototype($this->errorMessagePrototype);
$this->rules->attach($validationRule);
return $this;
}
/**
* Remove validation rule
*
* @param mixed $name
* rule name or true if all rules should be deleted for that selector
* @param mixed $options
* rule options, necessary for rules that depend on params for their ID
*
* @throws \InvalidArgumentException
* @internal param string $selector data selector
* @return self
*/
public function remove($name = true, $options = null)
{
if ($name === true) {
$this->rules = new RuleCollection();
return $this;
}
$validator = $this->ruleFactory->createRule($name, $options);
$this->rules->detach($validator);
return $this;
}
/**
* Converts a rule that was supplied as string into a set of options that define the rule
*
* @example 'minLength({"min":2})({label} must have at least {min} characters)(Street)'
*
* will be converted into
*
* array(
* 'minLength', // validator name
* array('min' => 2'), // validator options
* '{label} must have at least {min} characters',
* 'Street' // label
* )
*
* @param string $ruleAsString
*
* @return array
*/
protected function parseRule($ruleAsString)
{
$ruleAsString = trim($ruleAsString);
$options = array();
$messageTemplate = null;
$label = null;
$name = substr($ruleAsString, 0, strpos($ruleAsString, '('));
$ruleAsString = substr($ruleAsString, strpos($ruleAsString, '('));
$matches = array();
preg_match_all('/\(([^\)]*)\)/', $ruleAsString, $matches);
if (isset($matches[1])) {
if (isset($matches[1][0]) && $matches[1][0] !== '') {
$options = $matches[1][0];
}
if (isset($matches[1][1]) && $matches[1][1]) {
$messageTemplate = $matches[1][1];
}
if (isset($matches[1][2]) && $matches[1][2]) {
$label = $matches[1][2];
}
}
return array(
$name,
$options,
$messageTemplate,
$label
);
}
public function validate($value, $valueIdentifier = null, DataWrapper\WrapperInterface $context = null)
{
$this->messages = array();
$isRequired = false;
// evaluate the required rules
/* @var $rule \Sirius\Validation\Rule\AbstractValidator */
foreach ($this->rules as $rule) {
if ($rule instanceof Rule\Required) {
$isRequired = true;
if (!$this->validateRule($rule, $value, $valueIdentifier, $context)) {
return false;
}
}
}
// avoid future rule evaluations if value is null
if ($value === null) {
return true;
}
// evaluate the non-required rules
foreach ($this->rules as $rule) {
if (!($rule instanceof Rule\Required)) {
$this->validateRule($rule, $value, $valueIdentifier, $context);
// if field is required and we have an error,
// do not continue with the rest of rules
if ($isRequired && count($this->messages)) {
break;
}
}
}
return count($this->messages) === 0;
}
private function validateRule($rule, $value, $valueIdentifier, $context)
{
$rule->setContext($context);
if (!$rule->validate($value, $valueIdentifier)) {
$this->addMessage($rule->getMessage());
return false;
}
return true;
}
public function getMessages()
{
return $this->messages;
}
public function addMessage($message)
{
array_push($this->messages, $message);
return $this;
}
public function getRules()
{
return $this->rules;
}
}