generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FieldValidator.php
94 lines (78 loc) · 3.33 KB
/
FieldValidator.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
<?php
declare(strict_types=1);
namespace Yiisoft\Form\Widget\Validator;
use Yiisoft\Form\FormModelInterface;
use Yiisoft\Form\Widget\Attribute\WidgetAttributes;
use Yiisoft\Form\Widget\Url;
use Yiisoft\Html\Html;
use Yiisoft\Validator\Rule;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\Url as UrlValidator;
/**
* FieldValidator is a base class for validators that can be applied to a field.
*
* @param WidgetAttributes $widget The field widget.
* @param FormModelInterface $formModel The form model instance.
* @param string $attribute The attribute name or expression.
* @param array $attributes The HTML attributes for the field widget.
*
* @return array The attributes for validator html.
*/
final class FieldValidator
{
public function getValidatorAttributes(
WidgetAttributes $widget,
FormModelInterface $formModel,
string $attribute,
array $attributes
): array {
/** @psalm-var array<array-key, Rule> */
$rules = $formModel->getRules()[$attribute] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof Required) {
$attributes['required'] = true;
}
if ($rule instanceof HasLength && $widget instanceof HasLengthInterface) {
/** @var int|null */
$attributes['maxlength'] = $rule->getOptions()['max'] !== null ? $rule->getOptions()['max'] : null;
/** @var int|null */
$attributes['minlength'] = $rule->getOptions()['min'] !== null ? $rule->getOptions()['min'] : null;
}
if ($rule instanceof Regex && $widget instanceof RegexInterface) {
/** @var string */
$pattern = $rule->getOptions()['pattern'];
$attributes['pattern'] = Html::normalizeRegexpPattern($pattern);
}
if ($rule instanceof Number && $widget instanceof NumberInterface) {
/** @var int|null */
$attributes['max'] = $rule->getOptions()['max'] !== null ? $rule->getOptions()['max'] : null;
/** @var int|null */
$attributes['min'] = $rule->getOptions()['min'] !== null ? $rule->getOptions()['min'] : null;
}
if ($rule instanceof UrlValidator && $widget instanceof Url) {
/** @var array<array-key, string> */
$validSchemes = $rule->getOptions()['validSchemes'];
$schemes = [];
foreach ($validSchemes as $scheme) {
$schemes[] = $this->getSchemePattern($scheme);
}
/** @var array<array-key, float|int|string>|string */
$pattern = $rule->getOptions()['pattern'];
$normalizePattern = str_replace('{schemes}', '(' . implode('|', $schemes) . ')', $pattern);
$attributes['pattern'] = Html::normalizeRegexpPattern($normalizePattern);
}
}
return $attributes;
}
private function getSchemePattern(string $scheme): string
{
$result = '';
for ($i = 0, $length = strlen($scheme); $i < $length; $i++) {
$result .= '[' . strtolower($scheme[$i]) . strtoupper($scheme[$i]) . ']';
}
return $result;
}
}