1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavidePastore\Slim\Validation; |
4
|
|
|
|
5
|
|
|
use Respect\Validation\Exceptions\NestedValidationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Validation for Slim. |
9
|
|
|
*/ |
10
|
|
|
class Validation |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Validators. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $validators = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The translator to use fro the exception message. |
21
|
|
|
* |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
protected $translator = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Errors from the validation. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $errors = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create new Validator service provider. |
35
|
|
|
* |
36
|
|
|
* @param null|array|ArrayAccess $validators |
37
|
|
|
* @param null|callable $translator |
38
|
|
|
*/ |
39
|
16 |
|
public function __construct($validators = null, $translator = null) |
40
|
|
|
{ |
41
|
|
|
// Set the validators |
42
|
16 |
|
if (is_array($validators) || $validators instanceof ArrayAccess) { |
|
|
|
|
43
|
15 |
|
$this->validators = $validators; |
|
|
|
|
44
|
1 |
|
} elseif (is_null($validators)) { |
45
|
1 |
|
$this->validators = []; |
46
|
|
|
} |
47
|
16 |
|
$this->translator = $translator; |
48
|
16 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Validation middleware invokable class. |
52
|
|
|
* |
53
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request |
54
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response |
55
|
|
|
* @param callable $next Next middleware |
56
|
|
|
* |
57
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
58
|
|
|
*/ |
59
|
16 |
|
public function __invoke($request, $response, $next) |
60
|
|
|
{ |
61
|
16 |
|
$this->errors = []; |
62
|
16 |
|
$params = $request->getParams(); |
63
|
16 |
|
$this->validate($params, $this->validators); |
64
|
|
|
|
65
|
16 |
|
return $next($request, $response); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate the parameters by the given params, validators and actual keys. |
70
|
|
|
* This method populates the $errors attribute. |
71
|
|
|
* |
72
|
|
|
* @param array $params The array of parameters. |
73
|
|
|
* @param array $validators The array of validators. |
74
|
|
|
* @param array $actualKeys An array that will save all the keys of the tree to retrieve the correct value. |
75
|
|
|
*/ |
76
|
16 |
|
private function validate($params = [], $validators = [], $actualKeys = []) |
77
|
|
|
{ |
78
|
|
|
//Validate every parameters in the validators array |
79
|
16 |
|
foreach ($validators as $key => $validator) { |
80
|
15 |
|
$actualKeys[] = $key; |
81
|
15 |
|
$param = $this->getNestedParam($params, $actualKeys); |
82
|
15 |
|
if (is_array($validator)) { |
83
|
4 |
|
$this->validate($params, $validator, $actualKeys); |
84
|
|
|
} else { |
85
|
|
|
try { |
86
|
15 |
|
$validator->assert($param); |
87
|
9 |
|
} catch (NestedValidationException $exception) { |
88
|
9 |
|
if ($this->translator) { |
89
|
2 |
|
$exception->setParam('translator', $this->translator); |
90
|
|
|
} |
91
|
9 |
|
$this->errors[implode('.', $actualKeys)] = $exception->getMessages(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//Remove the key added in this foreach |
96
|
15 |
|
array_pop($actualKeys); |
97
|
|
|
} |
98
|
16 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the nested parameter value. |
102
|
|
|
* |
103
|
|
|
* @param array $params An array that represents the values of the parameters. |
104
|
|
|
* @param array $keys An array that represents the tree of keys to use. |
105
|
|
|
* |
106
|
|
|
* @return mixed The nested parameter value by the given params and tree of keys. |
107
|
|
|
*/ |
108
|
15 |
|
private function getNestedParam($params = [], $keys = []) |
109
|
|
|
{ |
110
|
15 |
|
if (empty($keys)) { |
111
|
13 |
|
return $params; |
112
|
|
|
} else { |
113
|
15 |
|
$firstKey = array_shift($keys); |
114
|
15 |
|
if (array_key_exists($firstKey, $params)) { |
115
|
13 |
|
$paramValue = $params[$firstKey]; |
116
|
|
|
|
117
|
13 |
|
return $this->getNestedParam($paramValue, $keys); |
118
|
|
|
} else { |
119
|
2 |
|
return; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check if there are any errors. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
13 |
|
public function hasErrors() |
130
|
|
|
{ |
131
|
13 |
|
return !empty($this->errors); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get errors. |
136
|
|
|
* |
137
|
|
|
* @return array The errors array. |
138
|
|
|
*/ |
139
|
12 |
|
public function getErrors() |
140
|
|
|
{ |
141
|
12 |
|
return $this->errors; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get validators. |
146
|
|
|
* |
147
|
|
|
* @return array The validators array. |
148
|
|
|
*/ |
149
|
14 |
|
public function getValidators() |
150
|
|
|
{ |
151
|
14 |
|
return $this->validators; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set validators. |
156
|
|
|
* |
157
|
|
|
* @param array $validators The validators array. |
158
|
|
|
*/ |
159
|
1 |
|
public function setValidators($validators) |
160
|
|
|
{ |
161
|
1 |
|
$this->validators = $validators; |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get translator. |
166
|
|
|
* |
167
|
|
|
* @return callable The translator. |
168
|
|
|
*/ |
169
|
2 |
|
public function getTranslator() |
170
|
|
|
{ |
171
|
2 |
|
return $this->translator; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set translator. |
176
|
|
|
* |
177
|
|
|
* @param callable $translator The translator. |
178
|
|
|
*/ |
179
|
1 |
|
public function setTranslator($translator) |
180
|
|
|
{ |
181
|
1 |
|
$this->translator = $translator; |
182
|
1 |
|
} |
183
|
|
|
} |
184
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.