1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/slim-validation package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Awurth\SlimValidation; |
13
|
|
|
|
14
|
|
|
use Twig\Extension\AbstractExtension; |
15
|
|
|
use Twig\TwigFunction; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ValidatorExtension. |
19
|
|
|
* |
20
|
|
|
* @author Alexis Wurth <[email protected]> |
21
|
|
|
*/ |
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) |
105
|
|
|
{ |
106
|
|
|
return $this->validator->getValue($key, $group); |
107
|
|
|
} |
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) |
118
|
|
|
{ |
119
|
|
|
return !empty($this->validator->getErrors($key, $group)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Tells whether there are validation errors. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function hasErrors() |
128
|
|
|
{ |
129
|
|
|
return !$this->validator->isValid(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|