-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractValidator.hh
382 lines (312 loc) · 9.5 KB
/
AbstractValidator.hh
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Validate;
use Titon\Validate\Exception\MissingConstraintException;
use Titon\Validate\Exception\MissingMessageException;
use Titon\Utility\Str;
use \Indexish;
use \ReflectionClass;
use \InvalidArgumentException;
/**
* Defines shared functionality for validators.
*
* @package Titon\Validate
*/
abstract class AbstractValidator implements Validator {
/**
* Constraint callbacks mapped by rule name.
*
* @var \Titon\Validate\ConstraintMap
*/
protected ConstraintMap $constraints = Map {};
/**
* Data to validate against.
*
* @var \Titon\Validate\DataMap
*/
protected DataMap $data = Map {};
/**
* Errors gathered during validation.
*
* @var \Titon\Validate\ErrorMap
*/
protected ErrorMap $errors = Map {};
/**
* Mapping of fields and titles.
*
* @var \Titon\Validate\FieldMap
*/
protected FieldMap $fields = Map {};
/**
* Fallback mapping of error messages.
*
* @var \Titon\Validate\MessageMap
*/
protected MessageMap $messages = Map {};
/**
* Mapping of fields and validation rules.
*
* @var \Titon\Validate\RuleContainer
*/
protected RuleContainer $rules = Map {};
/**
* Store the data to validate.
*
* @param \Titon\Validate\DataMap $data
*/
public function __construct(DataMap $data = Map {}) {
$this->setData($data);
}
/**
* {@inheritdoc}
*/
public function addConstraint(string $key, ConstraintCallback $callback): this {
$this->constraints[$key] = $callback;
return $this;
}
/**
* {@inheritdoc}
*/
public function addConstraintProvider(ConstraintProvider $provider): this {
$this->constraints->setAll($provider->getConstraints());
return $this;
}
/**
* {@inheritdoc}
*/
public function addError(string $field, string $message): this {
$this->errors[$field] = $message;
return $this;
}
/**
* {@inheritdoc}
*/
public function addField(string $field, string $title, Map<string, OptionList> $rules = Map {}): this {
$this->fields[$field] = $title;
if (!$rules->isEmpty()) {
foreach ($rules as $rule => $options) {
$this->addRule($field, $rule, '', $options);
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addMessages(MessageMap $messages): this {
$this->messages->setAll($messages);
return $this;
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException
*/
public function addRule(string $field, string $rule, string $message, OptionList $options = Vector{}): this {
if (!$this->fields->contains($field)) {
throw new InvalidArgumentException(sprintf('Field %s does not exist', $field));
}
if ($this->messages->contains($rule)) {
$message = $message ?: $this->messages[$rule];
} else {
$this->messages[$rule] = $message;
}
if (!$this->rules->contains($field)) {
$this->rules[$field] = Map {};
}
$this->rules[$field][$rule] = shape(
'rule' => $rule,
'message' => $message,
'options' => $options
);
return $this;
}
/**
* Format an error message by inserting tokens for the current field, rule, and rule options.
*
* @param string $field
* @param \Titon\Validate\Rule $rule
* @return string
* @throws \Titon\Validate\Exception\MissingMessageException
*/
public function formatMessage(string $field, Rule $rule): string {
$message = $rule['message'] ?: $this->getMessages()->get($rule['rule']);
if (!$message) {
throw new MissingMessageException(sprintf('Error message for rule %s does not exist', $rule['rule']));
}
$tokens = Map {
'field' => $field,
'title' => $this->getFields()->get($field)
};
foreach ($rule['options'] as $i => $option) {
$tokens[(string) $i] = ($option instanceof Indexish) ? implode(', ', $option) : $option;
}
return Str::insert($message, $tokens);
}
/**
* {@inheritdoc}
*/
public function getConstraints(): ConstraintMap {
return $this->constraints;
}
/**
* {@inheritdoc}
*/
public function getData(): DataMap {
return $this->data;
}
/**
* {@inheritdoc}
*/
public function getErrors(): ErrorMap {
return $this->errors;
}
/**
* {@inheritdoc}
*/
public function getFields(): FieldMap {
return $this->fields;
}
/**
* {@inheritdoc}
*/
public function getMessages(): MessageMap {
return $this->messages;
}
/**
* {@inheritdoc}
*/
public function getRules(): RuleContainer {
return $this->rules;
}
/**
* {@inheritdoc}
*/
public function reset(): this {
$this->data->clear();
$this->errors->clear();
return $this;
}
/**
* {@inheritdoc}
*/
public function setData(DataMap $data): this {
$this->data = $data;
return $this;
}
/**
* {@inheritdoc}
*
* @throws \Titon\Validate\Exception\MissingConstraintException
*/
public function validate(DataMap $data = Map {}): bool {
if ($data) {
$this->setData($data);
} else if (!$this->data) {
return false;
}
$fieldRules = $this->getRules();
$constraints = $this->getConstraints();
foreach ($this->getData() as $field => $value) {
if (!$fieldRules->contains($field)) {
continue;
}
$rules = $fieldRules[$field];
foreach ($rules as $rule => $params) {
if (!$constraints->contains($rule)) {
throw new MissingConstraintException(sprintf('Validation constraint %s does not exist', $rule));
}
$arguments = $params['options']->toArray();
// Add the input to validate as the 1st argument
array_unshift($arguments, $value);
// Execute the constraint
if (!call_user_func_array($constraints[$rule], $arguments)) {
$this->addError($field, $this->formatMessage($field, $params));
}
}
}
return (count($this->errors) === 0);
}
/**
* Create a validator instance from a set of shorthand or expanded rule sets.
*
* @param \Titon\Validate\DataMap $data
* @param Map<string, mixed> $fields
* @return $this
*/
public static function makeFromShorthand(DataMap $data = Map {}, Map<string, mixed> $fields = Map {}): Validator {
$class = new ReflectionClass(static::class);
/** @var \Titon\Validate\Validator $obj */
$obj = $class->newInstanceArgs([$data]);
foreach ($fields as $field => $options) {
$title = $field;
// A string of rule(s)
if (is_string($options)) {
$options = Map {'rules' => $options};
// List of rules
} else if ($options instanceof Vector) {
$options = Map {'rules' => $options};
// Ignore anything else not a map
} else if (!$options instanceof Map) {
continue;
}
// Prepare for parsing
if ($options->contains('title')) {
$title = $options['title'];
}
if (is_string($options['rules'])) {
$options['rules'] = new Vector(explode('|', $options['rules']));
}
$obj->addField($field, (string) $title);
// Dereference for the type checker
$rules = $options['rules'];
if ($rules instanceof Vector) {
foreach ($rules as $ruleOpts) {
$shorthand = static::splitShorthand($ruleOpts);
$obj->addRule($field, $shorthand['rule'], $shorthand['message'], $shorthand['options']);
}
}
}
return $obj;
}
/**
* Split a shorthand rule into multiple parts.
*
* @param string $shorthand
* @return \Titon\Validate\Rule
*/
public static function splitShorthand(string $shorthand): Rule {
$rule = '';
$message = '';
$opts = Vector {};
// rule:o1,o2,o3
// rule:o1,o2:The message here!
if (strpos($shorthand, ':') !== false) {
foreach (explode(':', $shorthand, 3) as $index => $part) {
if ($index == 0) {
$rule = $part;
} else if ($index == 1) {
if (strpos($part, ',') !== false) {
$opts = new Vector(explode(',', $part));
} else if ($part) {
$opts = new Vector([$part]);
}
} else if ($index == 2) {
$message = $part;
}
}
// rule
} else {
$rule = $shorthand;
}
return shape(
'rule' => $rule,
'message' => $message,
'options' => $opts
);
}
}