PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.12
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.12
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Validator / MessageBag.php

MessageBag.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.12, at vendor/wpfluent/framework/src/WPFluent/Validator/MessageBag.php

507 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Validator;
4
5 use FluentBoards\Framework\Support\Arr;
6
7 trait MessageBag
8 {
9 /**
10 * The default message bag.
11 *
12 * @var array
13 */
14 protected $bag = [
15 'array' => 'The :attribute must be an array.',
16 'alpha' => 'The :attribute must contain only alphabetic characters.',
17 'alphanum' => 'The :attribute must contain only alphanumeric characters.',
18 'alphadash' => 'The :attribute must contain only alphanumeric and _- characters.',
19 'email' => 'The :attribute must be a valid email address.',
20 'date_format' => 'Unable to format the :attribute field from the :value format string.',
21 'exists' => 'The selected :attribute is invalid.',
22 'in' => 'The selected :attribute is invalid.',
23 'not_in' => 'The selected :attribute is invalid.',
24 'max' => [
25 'numeric' => 'The :attribute may not be greater than :max.',
26 'file' => 'The :attribute may not be greater than :max kilobytes.',
27 'string' => 'The :attribute may not be greater than :max characters.',
28 'array' => 'The :attribute may not have more than :max items.',
29 ],
30 'mimes' => 'The :attribute must be a file of type: :values.',
31 'mimetypes' => 'The :attribute must be a file of type: :values.',
32 'min' => [
33 'numeric' => 'The :attribute must be at least :min.',
34 'file' => 'The :attribute must be at least :min kilobytes.',
35 'string' => 'The :attribute must be at least :min characters.',
36 'array' => 'The :attribute must have at least :min items.',
37 ],
38 'string' => 'The :attribute must be a string.',
39 'integer' => 'The :attribute must be an integer.',
40 'numeric' => 'The :attribute must be a number.',
41 'required' => 'The :attribute field is required.',
42 'required_if' => 'The :attribute field is required when :other is :value.',
43 'same' => 'The :attribute and :other must match.',
44 'size' => [
45 'numeric' => 'The :attribute must be :size.',
46 'file' => 'The :attribute must be :size kilobytes.',
47 'string' => 'The :attribute must be :size characters.',
48 'array' => 'The :attribute must contain :size items.',
49 ],
50 'url' => 'The :attribute format is invalid.',
51 'unique' => 'The :attribute attribute is already taken and must be unique.',
52 'digits' => 'The :attribute must be :digits characters.'
53 ];
54
55 /**
56 * Generate a validation error message.
57 *
58 * @param $attribute
59 * @param $rule
60 * @param $parameters
61 * @param $originalRuleKey
62 *
63 * @return mixed
64 */
65 protected function generate($attribute, $rule, $parameters, $originalRuleKey = null)
66 {
67 $method = 'replace'.str_replace(
68 ' ', '', ucwords(str_replace(['-', '_'], ' ', $rule))
69 );
70
71 $originalMessageKey = '';
72 if(!empty($originalRuleKey)){
73 $originalMessageKey = $originalRuleKey.'.'.$rule;
74 }
75
76 if ($this->hasMethod($method)) {
77 return $this->$method($attribute, $parameters, $originalMessageKey);
78 } else {
79 return $this->generateDefaultMessage($attribute, $parameters);
80 }
81 }
82
83 /**
84 * Fallback message generator for the failed validation.
85 * @param string $attribute
86 * @param array $parameters
87 * @return string
88 */
89 protected function generateDefaultMessage($attribute, $parameters)
90 {
91 $msg = "The {$attribute} field has been failed the validation";
92
93 if ($parameters) {
94 $msg .= " with parameter \"{$parameters[0]}\"";
95 }
96
97 return ($msg . '.');
98 }
99
100
101 /**
102 * Get the replacement text of the error message.
103 *
104 * @param $customMessagesKey
105 * @param $bagAccessor
106 * @param $originalMessageKey
107 *
108 * @return string
109 */
110 protected function getReplacementText($customMessagesKey, $bagAccessor, $originalMessageKey = null)
111 {
112 if (isset($this->customMessages[$customMessagesKey])) {
113 return $this->customMessages[$customMessagesKey];
114 } elseif (isset($this->customMessages[$originalMessageKey])) {
115 return $this->customMessages[$originalMessageKey];
116 }
117 return Arr::get($this->bag, $bagAccessor, '');
118 }
119
120 /**
121 * Make bag accessor key.
122 *
123 * @param $attribute
124 * @param $rule
125 *
126 * @return string
127 */
128 protected function makeBagKey($attribute, $rule)
129 {
130 $type = $this->deduceType(
131 $this->getValue($attribute), $attribute
132 );
133
134 return $rule.'.'.$type;
135 }
136
137 /**
138 * Replace all place-holders for the string rule.
139 *
140 * @param $attribute
141 * @param $parameters
142 * @param $originalMessageKey
143 * @return string
144 */
145 protected function replaceString($attribute, $parameters, $originalMessageKey)
146 {
147 $text = $this->getReplacementText($attribute.'.string', 'string', $originalMessageKey);
148
149 return str_replace(':attribute', $attribute, $text);
150 }
151
152 /**
153 * Replace all place-holders for the int|integer rule.
154 *
155 * @param $attribute
156 * @param $parameters
157 * @param $originalMessageKey
158 * @return string
159 */
160 protected function replaceInt($attribute, $parameters)
161 {
162 return $this->replaceInteger($attribute, $parameters);
163 }
164
165 /**
166 * Replace all place-holders for the int|integer rule.
167 *
168 * @param $attribute
169 * @param $parameters
170 * @param $originalMessageKey
171 *
172 * @return string
173 */
174 protected function replaceInteger($attribute, $parameters, $originalMessageKey)
175 {
176 $text = $this->getReplacementText($attribute.'.integer', 'integer', $originalMessageKey);
177
178 return str_replace(':attribute', $attribute, $text);
179 }
180
181 /**
182 * Replace all place-holders for the alpha rule.
183 *
184 * @param $attribute
185 * @param $parameters
186 *
187 * @return string
188 */
189 protected function replaceAlpha($attribute, $parameters, $originalMessageKey)
190 {
191 $text = $this->getReplacementText($attribute.'.alpha', 'alpha', $originalMessageKey);
192
193 return str_replace(':attribute', $attribute, $text);
194 }
195
196 /**
197 * Replace all place-holders for the alphanum rule.
198 *
199 * @param $attribute
200 * @param $parameters
201 *
202 * @return string
203 */
204 protected function replaceAlphanum($attribute, $parameters, $originalMessageKey)
205 {
206 $text = $this->getReplacementText($attribute.'.alphanum', 'alphanum', $originalMessageKey);
207
208 return str_replace(':attribute', $attribute, $text);
209 }
210
211 /**
212 * Replace all place-holders for the alphadash rule.
213 *
214 * @param $attribute
215 * @param $parameters
216 *
217 * @return string
218 */
219 protected function replaceAlphadash($attribute, $parameters, $originalMessageKey)
220 {
221 $text = $this->getReplacementText($attribute.'.alphadash', 'alphadash', $originalMessageKey);
222
223 return str_replace(':attribute', $attribute, $text);
224 }
225
226 /**
227 * Replace all place-holders for the required rule.
228 *
229 * @param $attribute
230 * @param $parameters
231 * @param $originalMessageKey
232 *
233 * @return string
234 */
235 protected function replaceRequired($attribute, $parameters, $originalMessageKey)
236 {
237 $text = $this->getReplacementText($attribute.'.required', 'required', $originalMessageKey);
238
239 return str_replace(':attribute', $attribute, $text);
240 }
241
242 /**
243 * Replace all place-holders for the required if rule.
244 *
245 * @param $attribute
246 * @param $parameters
247 *
248 * @return string
249 */
250 protected function replaceRequiredIf($attribute, $parameters, $originalMessageKey)
251 {
252 if (preg_match('/\.\d\./', $attribute, $matches)) {
253 $parameters[0] = str_replace(['.*.'], $matches, $parameters[0]);
254 }
255
256 $text = $this->getReplacementText($attribute.'.required_if', 'required_if', $originalMessageKey);
257
258 $value = end($parameters);
259
260 return str_replace([
261 ':attribute', ':other', ':value'],
262 [$attribute, $parameters[0], $value],
263 $text
264 );
265 }
266
267 /**
268 * Replace all place-holders for the email rule.
269 *
270 * @param $attribute
271 * @param $parameters
272 *
273 * @return string
274 */
275 protected function replaceEmail($attribute, $parameters, $originalMessageKey)
276 {
277 $text = $this->getReplacementText($attribute.'.email', 'email', $originalMessageKey);
278
279 return str_replace(':attribute', $attribute, $text);
280 }
281
282 /**
283 * Replace all place-holders for the email rule.
284 *
285 * @param $attribute
286 * @param $parameters
287 *
288 * @return string
289 */
290 protected function replaceDateformat($attribute, $parameters, $originalMessageKey)
291 {
292 $text = $this->getReplacementText($attribute.'.date_format', 'date_format', $$originalMessageKey);
293
294 return str_replace([':attribute', ':value'], [$attribute, $parameters[0]], $text);
295 }
296
297 /**
298 * Replace all place-holders for the size rule.
299 *
300 * @param $attribute
301 * @param $parameters
302 *
303 * @return string
304 */
305 protected function replaceSize($attribute, $parameters, $originalMessageKey)
306 {
307 $text = $this->getReplacementText($attribute.'.size', $this->makeBagKey($attribute, 'size'), $originalMessageKey);
308
309 return str_replace([':attribute', ':size'], [$attribute, $parameters[0]], $text);
310 }
311
312 /**
313 * Replace all place-holders for the min rule.
314 *
315 * @param $attribute
316 * @param $parameters
317 *
318 * @return string
319 */
320 protected function replaceMin($attribute, $parameters, $originalMessageKey)
321 {
322 $text = $this->getReplacementText($attribute.'.min', $this->makeBagKey($attribute, 'min'), $originalMessageKey);
323
324 return str_replace([':attribute', ':min'], [$attribute, $parameters[0]], $text);
325 }
326
327 /**
328 * Replace all place-holders for the max rule.
329 *
330 * @param $attribute
331 * @param $parameters
332 *
333 * @return string
334 */
335 protected function replaceMax($attribute, $parameters, $originalMessageKey)
336 {
337 $text = $this->getReplacementText($attribute.'.max', $this->makeBagKey($attribute, 'max'), $originalMessageKey);
338
339 return str_replace([':attribute', ':max'], [$attribute, $parameters[0]], $text);
340 }
341
342 /**
343 * Replace all place-holders for the min rule.
344 *
345 * @param $attribute
346 * @param $parameters
347 *
348 * @return string
349 */
350 protected function replaceSame($attribute, $parameters, $originalMessageKey)
351 {
352 $text = $this->getReplacementText($attribute.'.same', 'same', $originalMessageKey);
353
354 return str_replace([':attribute', ':other'], [$attribute, $parameters[0]], $text);
355 }
356
357 /**
358 * Replace all place-holders for the url rule.
359 *
360 * @param $attribute
361 * @param $parameters
362 *
363 * @return string
364 */
365 protected function replaceUrl($attribute, $parameters)
366 {
367 $text = $this->getReplacementText($attribute.'.url', 'url');
368
369 return str_replace(':attribute', $attribute, $text);
370 }
371
372 /**
373 * Replace all place-holders for the numeric rule.
374 *
375 * @param $attribute
376 * @param $parameters
377 *
378 * @return string
379 */
380 protected function replaceNumeric($attribute, $parameters)
381 {
382 $text = $this->getReplacementText($attribute.'.numeric', 'numeric');
383
384 return str_replace(':attribute', $attribute, $text);
385 }
386
387 /**
388 * Replace all place-holders for the mimes rule.
389 *
390 * @param $attribute
391 * @param $parameters
392 *
393 * @return string
394 */
395 protected function replaceMimes($attribute, $parameters)
396 {
397 $text = $this->getReplacementText($attribute.'.mimes', 'mimes');
398
399 return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text);
400 }
401
402 /**
403 * Replace all place-holders for the mimetypes rule.
404 *
405 * @param $attribute
406 * @param $parameters
407 *
408 * @return string
409 */
410 protected function replaceMimetypes($attribute, $parameters)
411 {
412 $text = $this->getReplacementText($attribute.'.mimetypes', 'mimetypes');
413
414 return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text);
415 }
416
417 /**
418 * Replace all place-holders for the unique rule.
419 *
420 * @param $attribute
421 * @param $parameters
422 *
423 * @return string
424 */
425 protected function replaceUnique($attribute, $parameters)
426 {
427 $text = $this->getReplacementText($attribute.'.unique', 'unique');
428
429 return str_replace(':attribute', $attribute, $text);
430 }
431
432 /**
433 * Replace all place-holders for the digits rule.
434 *
435 * @param $attribute
436 * @param $parameters
437 *
438 * @return string
439 */
440 protected function replaceDigits($attribute, $parameters)
441 {
442 $text = $this->getReplacementText($attribute.'.digits', 'digits');
443
444 return str_replace([':attribute', ':digits'], [$attribute, $parameters[0]], $text);
445 }
446
447 /**
448 * Replace all place-holders for the array rule.
449 *
450 * @param $attribute
451 * @param $parameters
452 *
453 * @return string
454 */
455 protected function replaceArray($attribute, $parameters)
456 {
457 $text = $this->getReplacementText($attribute.'.array', 'array');
458
459 return str_replace([':attribute', ':array'], [$attribute], $text);
460 }
461
462 /**
463 * Replace all place-holders for the in rule.
464 *
465 * @param $attribute
466 * @param $parameters
467 *
468 * @return string
469 */
470 protected function replaceIn($attribute, $parameters)
471 {
472 $text = $this->getReplacementText($attribute.'.in', 'in');
473
474 return str_replace([':attribute', ':in'], [$attribute], $text);
475 }
476
477 /**
478 * Replace all place-holders for the not_in rule.
479 *
480 * @param $attribute
481 * @param $parameters
482 *
483 * @return string
484 */
485 protected function replaceNotIn($attribute, $parameters)
486 {
487 $text = $this->getReplacementText($attribute.'.not_in', 'not_in');
488
489 return str_replace([':attribute', ':not_in'], [$attribute], $text);
490 }
491
492 /**
493 * Replace all place-holders for the exista rule.
494 *
495 * @param $attribute
496 * @param $parameters
497 *
498 * @return string
499 */
500 protected function replaceExists($attribute, $parameters)
501 {
502 $text = $this->getReplacementText($attribute.'.exists', 'exists');
503
504 return str_replace([':attribute', ':exists'], [$attribute], $text);
505 }
506 }
507