1
|
|
|
<?php |
2
|
|
|
namespace NwLaravel\Validation; |
3
|
|
|
|
4
|
|
|
use Illuminate\Validation\Factory; |
5
|
|
|
use Prettus\Validator\AbstractValidator; |
6
|
|
|
use Illuminate\Validation\Rules\Unique; |
7
|
|
|
use Illuminate\Validation\Rule; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BaseValidator |
11
|
|
|
* @abstract |
12
|
|
|
*/ |
13
|
|
|
abstract class BaseValidator extends AbstractValidator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Validator |
17
|
|
|
* |
18
|
|
|
* @var \Illuminate\Validation\Factory |
19
|
|
|
*/ |
20
|
|
|
protected $validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $keyName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $messages = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $attributes = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Construct |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Validation\Factory $validator |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct(Factory $validator) |
43
|
|
|
{ |
44
|
3 |
|
$this->validator = $validator; |
45
|
3 |
|
$this->rules = array_merge_recursive((array) $this->rules, $this->makeRules()); |
46
|
3 |
|
$this->messages = array_merge_recursive((array) $this->messages, $this->makeMessages()); |
47
|
3 |
|
$this->attributes = array_merge_recursive((array) $this->attributes, $this->makeAttributes()); |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* MakeRules |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
3 |
|
protected function makeRules() |
56
|
|
|
{ |
57
|
3 |
|
return []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Make Messages |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
3 |
|
protected function makeMessages() |
66
|
|
|
{ |
67
|
3 |
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Make Attributes |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
3 |
|
protected function makeAttributes() |
76
|
|
|
{ |
77
|
3 |
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get Messages |
82
|
|
|
* |
83
|
|
|
* @param array $messages |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
3 |
|
public function getMessages() |
88
|
|
|
{ |
89
|
3 |
|
return $this->messages; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get Attributes |
94
|
|
|
* |
95
|
|
|
* @param array $attributes |
|
|
|
|
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
3 |
|
public function getAttributes() |
100
|
|
|
{ |
101
|
3 |
|
return $this->attributes; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get Validator |
106
|
|
|
* |
107
|
|
|
* @return \Illuminate\Validation\Factory |
108
|
|
|
*/ |
109
|
1 |
|
public function getValidator() |
110
|
|
|
{ |
111
|
1 |
|
return $this->validator; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set Key Name |
116
|
|
|
* |
117
|
|
|
* @param string $keyName |
118
|
|
|
* |
119
|
|
|
* @return BaseValidator |
120
|
|
|
*/ |
121
|
1 |
|
public function setKeyName($keyName) |
122
|
|
|
{ |
123
|
1 |
|
$this->keyName = $keyName; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE |
128
|
|
|
* |
129
|
|
|
* Default rule: ValidatorInterface::RULE_CREATE |
130
|
|
|
* |
131
|
|
|
* @param string|null $action |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
3 |
|
public function getRules($action = null) |
136
|
|
|
{ |
137
|
3 |
|
$rules = []; |
138
|
|
|
|
139
|
3 |
|
if (isset($this->rules[$action])) { |
140
|
3 |
|
$rules = $this->rules[$action]; |
141
|
3 |
|
} |
142
|
|
|
|
143
|
3 |
|
return $this->parserValidationRules($rules, $this->id); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Pass the data and the rules to the validator |
148
|
|
|
* |
149
|
|
|
* @param string $action |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
2 |
|
public function passes($action = null) |
153
|
|
|
{ |
154
|
2 |
|
$rules = $this->getRules($action); |
155
|
2 |
|
$messages = $this->getMessages(); |
156
|
2 |
|
$attributes = $this->getAttributes(); |
157
|
2 |
|
$validator = $this->validator->make($this->data, $rules, $messages, $attributes); |
158
|
|
|
|
159
|
2 |
|
if ($validator->fails()) { |
160
|
1 |
|
$this->errors = $validator->messages(); |
161
|
1 |
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Parser Validation Rules |
169
|
|
|
* |
170
|
|
|
* @param array $rules |
171
|
|
|
* @param int|null $id |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
3 |
|
protected function parserValidationRules($rules, $id = null) |
176
|
|
|
{ |
177
|
3 |
|
if ($id === null) { |
178
|
3 |
|
return $rules; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
array_walk($rules, function (&$rules, $field) use ($id) { |
182
|
1 |
|
if (!is_array($rules)) { |
183
|
1 |
|
$rules = explode("|", $rules); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
1 |
|
foreach ($rules as $ruleIdx => $rule) { |
187
|
1 |
|
$rule = $this->replaceValuesRules($rule); |
188
|
|
|
|
189
|
1 |
|
$itens = array_pad(explode(":", $rule), 2, null); |
190
|
1 |
|
$nameRule = isset($itens[0]) ? $itens[0] : null; |
191
|
1 |
|
$params = implode(":", array_splice($itens, 1)); |
192
|
|
|
|
193
|
1 |
|
if ($nameRule != "unique") { |
194
|
1 |
|
$rule = $nameRule; |
195
|
1 |
|
if (!empty($params)) { |
196
|
1 |
|
$rule .= ":".$params; |
197
|
1 |
|
} |
198
|
1 |
|
$rules[$ruleIdx] = $rule; |
199
|
1 |
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// ATUALIZA RULES UNIQUE |
203
|
1 |
|
$p = array_map("trim", explode(",", $params)); |
204
|
|
|
|
205
|
1 |
|
$table = $p[0]; |
206
|
|
|
|
207
|
|
|
// set field name to rules key ($field) (laravel convention) |
208
|
1 |
|
if (isset($p[1]) && !empty($p[1])) { |
209
|
|
|
$field = $p[1]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// set 3rd parameter to id given to getValidationRules() |
213
|
1 |
|
if (isset($p[2]) && !empty($p[2]) && strtoupper($p[2]) != 'NULL') { |
214
|
|
|
$id = intval($p[2]); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
1 |
|
if (isset($p[3]) && !empty($p[3])) { |
218
|
|
|
$keyName = $p[3]; |
219
|
1 |
|
} elseif($this->keyName) { |
220
|
1 |
|
$keyName = $this->keyName; |
221
|
1 |
|
} else { |
222
|
|
|
$keyName = 'id'; |
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
if (! $rule instanceof Unique) { |
226
|
1 |
|
$rule = Rule::unique($table, $field); |
227
|
1 |
|
} |
228
|
|
|
|
229
|
1 |
|
if ($rule instanceof Unique) { |
230
|
1 |
|
$rule->where(function ($query) use ($id, $keyName) { |
|
|
|
|
231
|
1 |
|
$query->orWhere($keyName, '<>', $id); |
232
|
1 |
|
$query->orWhereNull($keyName); |
233
|
1 |
|
}); |
234
|
1 |
|
} |
235
|
|
|
|
236
|
1 |
|
$rules[$ruleIdx] = $rule; |
237
|
1 |
|
} |
238
|
1 |
|
}); |
239
|
|
|
|
240
|
1 |
|
return $rules; |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
protected function replaceValuesRules($rule) |
244
|
|
|
{ |
245
|
1 |
|
while (preg_match('/\[([A-Za-z0-9_]+)\]/', $rule, $match)) { |
246
|
1 |
|
if (array_key_exists($match[1], $this->data)) { |
247
|
1 |
|
$rule = str_replace("[{$match[1]}]", $this->getValue($match[1]), $rule); |
248
|
1 |
|
} |
249
|
1 |
|
} |
250
|
|
|
|
251
|
1 |
|
return $rule; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get the value of a given attribute. |
256
|
|
|
* |
257
|
|
|
* @param string $attribute |
258
|
|
|
* |
259
|
|
|
* @return mixed |
260
|
|
|
*/ |
261
|
1 |
|
protected function getValue($attribute) |
262
|
|
|
{ |
263
|
1 |
|
if (is_null($value = array_get($this->data, $attribute))) { |
264
|
|
|
$value = 'NULL'; |
265
|
|
|
} |
266
|
|
|
|
267
|
1 |
|
return $value; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.