1 | <?php |
||
22 | class ValidatorExtension extends AbstractExtension |
||
23 | { |
||
24 | /** |
||
25 | * Array of names for Twig functions. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $functionsNames; |
||
30 | |||
31 | /** |
||
32 | * Validator service. |
||
33 | * |
||
34 | * @var Validator |
||
35 | */ |
||
36 | protected $validator; |
||
37 | |||
38 | /** |
||
39 | * Constructor. |
||
40 | * |
||
41 | * @param Validator $validator The validator instance |
||
42 | * @param array $functionsNames An array of names for Twig functions |
||
43 | */ |
||
44 | public function __construct(Validator $validator, $functionsNames = []) |
||
45 | { |
||
46 | $this->validator = $validator; |
||
47 | |||
48 | $this->functionsNames['error'] = $functionsNames['error'] ?? 'error'; |
||
49 | $this->functionsNames['errors'] = $functionsNames['errors'] ?? 'errors'; |
||
50 | $this->functionsNames['has_error'] = $functionsNames['has_error'] ?? 'has_error'; |
||
51 | $this->functionsNames['has_errors'] = $functionsNames['has_errors'] ?? 'has_errors'; |
||
52 | $this->functionsNames['val'] = $functionsNames['val'] ?? 'val'; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function getFunctions() |
||
59 | { |
||
60 | return [ |
||
61 | new TwigFunction($this->functionsNames['error'], [$this, 'getError']), |
||
62 | new TwigFunction($this->functionsNames['errors'], [$this, 'getErrors']), |
||
63 | new TwigFunction($this->functionsNames['has_error'], [$this, 'hasError']), |
||
64 | new TwigFunction($this->functionsNames['has_errors'], [$this, 'hasErrors']), |
||
65 | new TwigFunction($this->functionsNames['val'], [$this, 'getValue']) |
||
66 | ]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Gets the first validation error of a parameter. |
||
71 | * |
||
72 | * @param string $key |
||
73 | * @param string $index |
||
74 | * @param string $group |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getError($key, $index = null, $group = null) |
||
79 | { |
||
80 | return $this->validator->getError($key, $index, $group); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Gets validation errors. |
||
85 | * |
||
86 | * @param string $key |
||
87 | * @param string $group |
||
88 | * |
||
89 | * @return string[] |
||
90 | */ |
||
91 | public function getErrors($key = null, $group = null) |
||
92 | { |
||
93 | return $this->validator->getErrors($key, $group); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Gets a value from the validated data. |
||
98 | * |
||
99 | * @param string $key |
||
100 | * @param string $group |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getValue($key, $group = null) |
||
108 | |||
109 | /** |
||
110 | * Tells whether there are validation errors for a parameter. |
||
111 | * |
||
112 | * @param string $key |
||
113 | * @param string $group |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function hasError($key, $group = null) |
||
121 | |||
122 | /** |
||
123 | * Tells whether there are validation errors. |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function hasErrors() |
||
131 | } |
||
132 |