|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gregoriohc\Protean\Common\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Gregoriohc\Protean\Common\Concerns\Parametrizable; |
|
6
|
|
|
use Illuminate\Translation\ArrayLoader; |
|
7
|
|
|
use Illuminate\Translation\Translator; |
|
8
|
|
|
use Illuminate\Validation\Factory as ValidatorFactory; |
|
9
|
|
|
|
|
10
|
|
|
class Validator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Illuminate\Validation\Validator |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $validator; |
|
16
|
|
|
|
|
17
|
|
|
protected $validationMessages = [ |
|
18
|
|
|
/* |
|
19
|
|
|
|-------------------------------------------------------------------------- |
|
20
|
|
|
| Validation Language Lines |
|
21
|
|
|
|-------------------------------------------------------------------------- |
|
22
|
|
|
| |
|
23
|
|
|
| The following language lines contain the default error messages used by |
|
24
|
|
|
| the validator class. Some of these rules have multiple versions such |
|
25
|
|
|
| as the size rules. Feel free to tweak each of these messages here. |
|
26
|
|
|
| |
|
27
|
|
|
*/ |
|
28
|
|
|
"accepted" => "The :attribute must be accepted.", |
|
29
|
|
|
"active_url" => "The :attribute is not a valid URL.", |
|
30
|
|
|
"after" => "The :attribute must be a date after :date.", |
|
31
|
|
|
"alpha" => "The :attribute may only contain letters.", |
|
32
|
|
|
"alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.", |
|
33
|
|
|
"alpha_num" => "The :attribute may only contain letters and numbers.", |
|
34
|
|
|
"array" => "The :attribute must be an array.", |
|
35
|
|
|
"before" => "The :attribute must be a date before :date.", |
|
36
|
|
|
"between" => [ |
|
37
|
|
|
"numeric" => "The :attribute must be between :min and :max.", |
|
38
|
|
|
"file" => "The :attribute must be between :min and :max kilobytes.", |
|
39
|
|
|
"string" => "The :attribute must be between :min and :max characters.", |
|
40
|
|
|
"array" => "The :attribute must have between :min and :max items.", |
|
41
|
|
|
], |
|
42
|
|
|
"confirmed" => "The :attribute confirmation does not match.", |
|
43
|
|
|
"date" => "The :attribute is not a valid date.", |
|
44
|
|
|
"date_format" => "The :attribute does not match the format :format.", |
|
45
|
|
|
"different" => "The :attribute and :other must be different.", |
|
46
|
|
|
"digits" => "The :attribute must be :digits digits.", |
|
47
|
|
|
"digits_between" => "The :attribute must be between :min and :max digits.", |
|
48
|
|
|
"email" => "The :attribute format is invalid.", |
|
49
|
|
|
"exists" => "The selected :attribute is invalid.", |
|
50
|
|
|
"image" => "The :attribute must be an image.", |
|
51
|
|
|
"in" => "The selected :attribute is invalid.", |
|
52
|
|
|
"integer" => "The :attribute must be an integer.", |
|
53
|
|
|
"ip" => "The :attribute must be a valid IP address.", |
|
54
|
|
|
"max" => [ |
|
55
|
|
|
"numeric" => "The :attribute may not be greater than :max.", |
|
56
|
|
|
"file" => "The :attribute may not be greater than :max kilobytes.", |
|
57
|
|
|
"string" => "The :attribute may not be greater than :max characters.", |
|
58
|
|
|
"array" => "The :attribute may not have more than :max items.", |
|
59
|
|
|
], |
|
60
|
|
|
"mimes" => "The :attribute must be a file of type: :values.", |
|
61
|
|
|
"min" => [ |
|
62
|
|
|
"numeric" => "The :attribute must be at least :min.", |
|
63
|
|
|
"file" => "The :attribute must be at least :min kilobytes.", |
|
64
|
|
|
"string" => "The :attribute must be at least :min characters.", |
|
65
|
|
|
"array" => "The :attribute must have at least :min items.", |
|
66
|
|
|
], |
|
67
|
|
|
"not_in" => "The selected :attribute is invalid.", |
|
68
|
|
|
"numeric" => "The :attribute must be a number.", |
|
69
|
|
|
"regex" => "The :attribute format is invalid.", |
|
70
|
|
|
"required" => "The :attribute field is required.", |
|
71
|
|
|
"required_if" => "The :attribute field is required when :other is :value.", |
|
72
|
|
|
"required_with" => "The :attribute field is required when :values is present.", |
|
73
|
|
|
"required_without" => "The :attribute field is required when :values is not present.", |
|
74
|
|
|
"required_without_all" => "The :attribute field is required when :values are not present.", |
|
75
|
|
|
"same" => "The :attribute and :other must match.", |
|
76
|
|
|
"size" => [ |
|
77
|
|
|
"numeric" => "The :attribute must be :size.", |
|
78
|
|
|
"file" => "The :attribute must be :size kilobytes.", |
|
79
|
|
|
"string" => "The :attribute must be :size characters.", |
|
80
|
|
|
"array" => "The :attribute must contain :size items.", |
|
81
|
|
|
], |
|
82
|
|
|
"unique" => "The :attribute has already been taken.", |
|
83
|
|
|
"url" => "The :attribute format is invalid.", |
|
84
|
|
|
'custom' => [], |
|
85
|
|
|
'attributes' => [], |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
protected $validationMessagesCustom = [ |
|
89
|
|
|
"is_valid" => "The :attribute must be a validable object.", |
|
90
|
|
|
"is_model" => "The :attribute must be a model of type :model.", |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
protected $context; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Validator constructor. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $parameters |
|
99
|
|
|
* @param array $rules |
|
100
|
|
|
* @param mixed $context |
|
101
|
|
|
*/ |
|
102
|
27 |
|
public function __construct($parameters, $rules, $context = null) |
|
103
|
|
|
{ |
|
104
|
27 |
|
$this->context = $context; |
|
105
|
27 |
|
$this->validator = $this->getValidator($parameters, $rules); |
|
106
|
27 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param array $parameters |
|
110
|
|
|
* @param array $rules |
|
111
|
|
|
* @param mixed $context |
|
112
|
|
|
* @return static|\Illuminate\Validation\Validator |
|
113
|
|
|
*/ |
|
114
|
27 |
|
public static function make($parameters, $rules, $context = null) |
|
115
|
|
|
{ |
|
116
|
27 |
|
return new static($parameters, $rules, $context); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param array $parameters |
|
121
|
|
|
* @param array $rules |
|
122
|
|
|
* @return \Illuminate\Validation\Validator |
|
123
|
|
|
*/ |
|
124
|
27 |
|
public function getValidator($parameters, $rules) |
|
125
|
|
|
{ |
|
126
|
27 |
|
if (!class_exists('\Validator')) { |
|
127
|
27 |
|
$loader = new ArrayLoader(); |
|
128
|
27 |
|
$loader->addMessages('en_US', 'validation', $this->validationMessages); |
|
129
|
27 |
|
$translator = new Translator($loader, 'en_US'); |
|
130
|
27 |
|
$validatorFactory = new ValidatorFactory($translator); |
|
131
|
27 |
|
$validator = $validatorFactory->make($parameters, $rules, $this->validationMessagesCustom); |
|
132
|
|
|
} else { |
|
133
|
|
|
// @codeCoverageIgnoreStart |
|
134
|
|
|
$validator = \Validator::make($parameters, $rules, $this->validationMessagesCustom); |
|
135
|
|
|
// @codeCoverageIgnoreEnd |
|
136
|
|
|
} |
|
137
|
27 |
|
$this->bootValidatorExtensions($validator, ['is_valid', 'is_model']); |
|
138
|
|
|
|
|
139
|
27 |
|
return $validator; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param \Illuminate\Validation\Validator $validator |
|
144
|
|
|
* @param array $names |
|
145
|
|
|
*/ |
|
146
|
27 |
|
public function bootValidatorExtensions(&$validator, $names) |
|
147
|
|
|
{ |
|
148
|
27 |
|
$context = $this->context; |
|
149
|
|
|
|
|
150
|
27 |
|
foreach ($names as $name) { |
|
151
|
27 |
|
$validator->addExtension($name, function ($attribute, $value, $parameters, $validator) use ($name, $context) { |
|
152
|
18 |
|
$parameters[] = $context; |
|
153
|
18 |
|
$method = 'validate' . studly_case($name); |
|
154
|
18 |
|
return call_user_func_array([static::class, $method], [$attribute, $value, $parameters, $validator]); |
|
155
|
27 |
|
}); |
|
156
|
|
|
|
|
157
|
27 |
|
$validator->addReplacer($name, function ($message, $attribute, $rule, $parameters) use ($name, $context) { |
|
158
|
6 |
|
$parameters[] = $context; |
|
159
|
6 |
|
$method = 'replace' . studly_case($name); |
|
160
|
6 |
|
if (!method_exists(static::class, $method)) { |
|
161
|
3 |
|
return $message; |
|
162
|
|
|
} |
|
163
|
6 |
|
return call_user_func_array([static::class, $method], [$message, $attribute, $rule, $parameters]); |
|
164
|
27 |
|
}); |
|
165
|
|
|
} |
|
166
|
27 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $attribute |
|
170
|
|
|
* @param mixed $value |
|
171
|
|
|
* @param array $parameters |
|
172
|
|
|
* @param \Illuminate\Validation\Validator $validator |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
18 |
|
protected static function validateIsValid($attribute, $value, $parameters, $validator) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
18 |
|
if (!is_object($value)) { |
|
178
|
3 |
|
return "Parameter '$attribute' must be an object"; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
15 |
|
if (!method_exists($value, 'validateParameters')) { |
|
182
|
3 |
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** @var Parametrizable $parameterValue */ |
|
186
|
12 |
|
$value->validateParameters(); |
|
187
|
|
|
|
|
188
|
9 |
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $attribute |
|
193
|
|
|
* @param mixed $value |
|
194
|
|
|
* @param array $parameters |
|
195
|
|
|
* @param \Illuminate\Validation\Validator $validator |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
18 |
|
protected static function validateIsModel($attribute, $value, $parameters, $validator) |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
18 |
|
$basePackageNamespace = implode('\\', array_slice(explode('\\', static::class), 0, 2)); |
|
201
|
18 |
|
if (count($parameters) == 1) { |
|
202
|
3 |
|
array_unshift($parameters, $basePackageNamespace . '\\Common\\Models\\AbstractModel'); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
18 |
|
$contextClass = get_class($parameters[1]); |
|
206
|
18 |
|
$baseContextNamespace = implode('\\', array_slice(explode('\\', $contextClass), 0, 2)); |
|
207
|
18 |
|
if (false === strstr($parameters[0], '\\')) { |
|
208
|
|
|
// @codeCoverageIgnoreStart |
|
209
|
|
|
$parameters[0] = $baseContextNamespace . '\\Common\\Models\\' . $parameters[0]; |
|
210
|
|
|
// @codeCoverageIgnoreEnd |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
18 |
|
if (!is_a($value, $parameters[0], true)) { |
|
214
|
6 |
|
return false; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
12 |
|
return true; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $message |
|
222
|
|
|
* @param string $attribute |
|
223
|
|
|
* @param string $rule |
|
224
|
|
|
* @param array $parameters |
|
225
|
|
|
* @return bool |
|
226
|
|
|
*/ |
|
227
|
6 |
|
protected static function replaceIsModel($message, $attribute, $rule, $parameters) |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
6 |
|
if (count($parameters) == 1) { |
|
230
|
3 |
|
array_unshift($parameters, 'AbstractModel'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
6 |
|
return str_replace(':model', $parameters[0], $message); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $name |
|
238
|
|
|
* @param array $arguments |
|
239
|
|
|
* @return mixed |
|
240
|
|
|
*/ |
|
241
|
27 |
|
public function __call($name, $arguments) |
|
242
|
|
|
{ |
|
243
|
27 |
|
return call_user_func_array([$this->validator, $name], $arguments); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.