PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.98
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.98
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Validator / MessageBag.php

MessageBag.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.98, at vendor/wpfluent/framework/src/WPFluent/Validator/MessageBag.php

553 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Validator;
4
5 use FluentCommunity\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 $originalKey = '';
72
73 if (!empty($originalRuleKey)){
74 $originalKey = $originalRuleKey.'.'.$rule;
75 }
76
77 if ($this->hasMethod($method)) {
78 return $this->$method($attribute, $parameters, $originalKey);
79 } else {
80 return $this->generateDefaultMessage($attribute, $parameters);
81 }
82 }
83
84 /**
85 * Fallback message generator for the failed validation.
86 * @param string $attribute
87 * @param array $parameters
88 * @return string
89 */
90 protected function generateDefaultMessage($attribute, $parameters)
91 {
92 $msg = "The {$attribute} field has been failed the validation";
93
94 if ($parameters) {
95 $msg .= " with parameter \"{$parameters[0]}\"";
96 }
97
98 return ($msg . '.');
99 }
100
101
102 /**
103 * Get the replacement text of the error message.
104 *
105 * @param $customKey
106 * @param $bagAccessor
107 * @param $originalKey
108 *
109 * @return string
110 */
111 protected function getReplacementText($customKey, $bagAccessor, $originalKey = null)
112 {
113 if (isset($this->customMessages[$customKey])) {
114 return $this->customMessages[$customKey];
115 } elseif (isset($this->customMessages[$originalKey])) {
116 return $this->customMessages[$originalKey];
117 }
118
119 return Arr::get($this->bag, $bagAccessor, '');
120 }
121
122 /**
123 * Make bag accessor key.
124 *
125 * @param $attribute
126 * @param $rule
127 *
128 * @return string
129 */
130 protected function makeBagKey($attribute, $rule)
131 {
132 $type = $this->deduceType(
133 $this->getValue($attribute), $attribute
134 );
135
136 return $rule.'.'.$type;
137 }
138
139 /**
140 * Replace all place-holders for the string rule.
141 *
142 * @param $attribute
143 * @param $parameters
144 * @param $originalKey
145 * @return string
146 */
147 protected function replaceString($attribute, $parameters, $originalKey)
148 {
149 $text = $this->getReplacementText(
150 $attribute.'.string', 'string', $originalKey
151 );
152
153 return str_replace(':attribute', $attribute, $text);
154 }
155
156 /**
157 * Replace all place-holders for the int|integer rule.
158 *
159 * @param $attribute
160 * @param $parameters
161 * @param $originalKey
162 * @return string
163 */
164 protected function replaceInt($attribute, $parameters, $originalKey)
165 {
166 return $this->replaceInteger($attribute, $parameters, $originalKey);
167 }
168
169 /**
170 * Replace all place-holders for the int|integer rule.
171 *
172 * @param $attribute
173 * @param $parameters
174 * @param $originalKey
175 *
176 * @return string
177 */
178 protected function replaceInteger($attribute, $parameters, $originalKey)
179 {
180 $text = $this->getReplacementText(
181 $attribute.'.integer', 'integer', $originalKey
182 );
183
184 return str_replace(':attribute', $attribute, $text);
185 }
186
187 /**
188 * Replace all place-holders for the alpha rule.
189 *
190 * @param $attribute
191 * @param $parameters
192 * @param $originalKey
193 *
194 * @return string
195 */
196 protected function replaceAlpha($attribute, $parameters, $originalKey)
197 {
198 $text = $this->getReplacementText(
199 $attribute.'.alpha', 'alpha', $originalKey
200 );
201
202 return str_replace(':attribute', $attribute, $text);
203 }
204
205 /**
206 * Replace all place-holders for the alphanum rule.
207 *
208 * @param $attribute
209 * @param $parameters
210 * @param $originalKey
211 *
212 * @return string
213 */
214 protected function replaceAlphanum($attribute, $parameters, $originalKey)
215 {
216 $text = $this->getReplacementText(
217 $attribute.'.alphanum', 'alphanum', $originalKey
218 );
219
220 return str_replace(':attribute', $attribute, $text);
221 }
222
223 /**
224 * Replace all place-holders for the alphadash rule.
225 *
226 * @param $attribute
227 * @param $parameters
228 * @param $originalKey
229 *
230 * @return string
231 */
232 protected function replaceAlphadash($attribute, $parameters, $originalKey)
233 {
234 $text = $this->getReplacementText(
235 $attribute.'.alphadash', 'alphadash', $originalKey
236 );
237
238 return str_replace(':attribute', $attribute, $text);
239 }
240
241 /**
242 * Replace all place-holders for the required rule.
243 *
244 * @param $attribute
245 * @param $parameters
246 * @param $originalKey
247 *
248 * @return string
249 */
250 protected function replaceRequired($attribute, $parameters, $originalKey)
251 {
252 $text = $this->getReplacementText(
253 $attribute.'.required', 'required', $originalKey
254 );
255
256 return str_replace(':attribute', $attribute, $text);
257 }
258
259 /**
260 * Replace all place-holders for the required if rule.
261 *
262 * @param $attribute
263 * @param $parameters
264 * @param $originalKey
265 *
266 * @return string
267 */
268 protected function replaceRequiredIf($attribute, $parameters, $originalKey)
269 {
270 if (preg_match('/\.\d\./', $attribute, $matches)) {
271 $parameters[0] = str_replace(['.*.'], $matches, $parameters[0]);
272 }
273
274 $text = $this->getReplacementText($attribute.'.required_if', 'required_if', $originalKey);
275
276 $value = end($parameters);
277
278 return str_replace([
279 ':attribute', ':other', ':value'],
280 [$attribute, $parameters[0], $value],
281 $text
282 );
283 }
284
285 /**
286 * Replace all place-holders for the email rule.
287 *
288 * @param $attribute
289 * @param $parameters
290 * @param $originalKey
291 *
292 * @return string
293 */
294 protected function replaceEmail($attribute, $parameters, $originalKey)
295 {
296 $text = $this->getReplacementText(
297 $attribute.'.email', 'email', $originalKey
298 );
299
300 return str_replace(':attribute', $attribute, $text);
301 }
302
303 /**
304 * Replace all place-holders for the email rule.
305 *
306 * @param $attribute
307 * @param $parameters
308 * @param $originalKey
309 *
310 * @return string
311 */
312 protected function replaceDateformat($attribute, $parameters, $originalKey)
313 {
314 $text = $this->getReplacementText($attribute.'.date_format', 'date_format', $$originalKey);
315
316 return str_replace(
317 [':attribute', ':value'], [$attribute, $parameters[0]], $text
318 );
319 }
320
321 /**
322 * Replace all place-holders for the size rule.
323 *
324 * @param $attribute
325 * @param $parameters
326 * @param $originalKey
327 *
328 * @return string
329 */
330 protected function replaceSize($attribute, $parameters, $originalKey)
331 {
332 $text = $this->getReplacementText($attribute.'.size', $this->makeBagKey($attribute, 'size'), $originalKey);
333
334 return str_replace(
335 [':attribute', ':size'], [$attribute, $parameters[0]], $text
336 );
337 }
338
339 /**
340 * Replace all place-holders for the min rule.
341 *
342 * @param $attribute
343 * @param $parameters
344 * @param $originalKey
345 *
346 * @return string
347 */
348 protected function replaceMin($attribute, $parameters, $originalKey)
349 {
350 $text = $this->getReplacementText($attribute.'.min', $this->makeBagKey($attribute, 'min'), $originalKey);
351
352 return str_replace(
353 [':attribute', ':min'], [$attribute, $parameters[0]], $text
354 );
355 }
356
357 /**
358 * Replace all place-holders for the max rule.
359 *
360 * @param $attribute
361 * @param $parameters
362 * @param $originalKey
363 *
364 * @return string
365 */
366 protected function replaceMax($attribute, $parameters, $originalKey)
367 {
368 $text = $this->getReplacementText($attribute.'.max', $this->makeBagKey($attribute, 'max'), $originalKey);
369
370 return str_replace(
371 [':attribute', ':max'], [$attribute, $parameters[0]], $text
372 );
373 }
374
375 /**
376 * Replace all place-holders for the min rule.
377 *
378 * @param $attribute
379 * @param $parameters
380 * @param $originalKey
381 *
382 * @return string
383 */
384 protected function replaceSame($attribute, $parameters, $originalKey)
385 {
386 $text = $this->getReplacementText($attribute.'.same', 'same', $originalKey);
387
388 return str_replace(
389 [':attribute', ':other'], [$attribute, $parameters[0]], $text
390 );
391 }
392
393 /**
394 * Replace all place-holders for the url rule.
395 *
396 * @param $attribute
397 * @param $parameters
398 * @param $originalKey
399 *
400 * @return string
401 */
402 protected function replaceUrl($attribute, $parameters, $originalKey)
403 {
404 $text = $this->getReplacementText($attribute.'.url', 'url', $originalKey);
405
406 return str_replace(':attribute', $attribute, $text);
407 }
408
409 /**
410 * Replace all place-holders for the numeric rule.
411 *
412 * @param $attribute
413 * @param $parameters
414 * @param $originalKey
415 *
416 * @return string
417 */
418 protected function replaceNumeric($attribute, $parameters, $originalKey)
419 {
420 $text = $this->getReplacementText($attribute.'.numeric', 'numeric', $originalKey);
421
422 return str_replace(':attribute', $attribute, $text);
423 }
424
425 /**
426 * Replace all place-holders for the mimes rule.
427 *
428 * @param $attribute
429 * @param $parameters
430 * @param $originalKey
431 *
432 * @return string
433 */
434 protected function replaceMimes($attribute, $parameters, $originalKey)
435 {
436 $text = $this->getReplacementText($attribute.'.mimes', 'mimes', $originalKey);
437
438 return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text);
439 }
440
441 /**
442 * Replace all place-holders for the mimetypes rule.
443 *
444 * @param $attribute
445 * @param $parameters
446 * @param $originalKey
447 *
448 * @return string
449 */
450 protected function replaceMimetypes($attribute, $parameters, $originalKey)
451 {
452 $text = $this->getReplacementText($attribute.'.mimetypes', 'mimetypes', $originalKey);
453
454 return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text);
455 }
456
457 /**
458 * Replace all place-holders for the unique rule.
459 *
460 * @param $attribute
461 * @param $parameters
462 * @param $originalKey
463 *
464 * @return string
465 */
466 protected function replaceUnique($attribute, $parameters, $originalKey)
467 {
468 $text = $this->getReplacementText($attribute.'.unique', 'unique', $originalKey);
469
470 return str_replace(':attribute', $attribute, $text);
471 }
472
473 /**
474 * Replace all place-holders for the digits rule.
475 *
476 * @param $attribute
477 * @param $parameters
478 * @param $originalKey
479 *
480 * @return string
481 */
482 protected function replaceDigits($attribute, $parameters, $originalKey)
483 {
484 $text = $this->getReplacementText($attribute.'.digits', 'digits', $originalKey);
485
486 return str_replace([':attribute', ':digits'], [$attribute, $parameters[0]], $text);
487 }
488
489 /**
490 * Replace all place-holders for the array rule.
491 *
492 * @param $attribute
493 * @param $parameters
494 * @param $originalKey
495 *
496 * @return string
497 */
498 protected function replaceArray($attribute, $parameters, $originalKey)
499 {
500 $text = $this->getReplacementText($attribute.'.array', 'array', $originalKey);
501
502 return str_replace([':attribute', ':array'], [$attribute], $text);
503 }
504
505 /**
506 * Replace all place-holders for the in rule.
507 *
508 * @param $attribute
509 * @param $parameters
510 * @param $originalKey
511 *
512 * @return string
513 */
514 protected function replaceIn($attribute, $parameters, $originalKey)
515 {
516 $text = $this->getReplacementText($attribute.'.in', 'in', $originalKey);
517
518 return str_replace([':attribute', ':in'], [$attribute], $text);
519 }
520
521 /**
522 * Replace all place-holders for the not_in rule.
523 *
524 * @param $attribute
525 * @param $parameters
526 * @param $originalKey
527 *
528 * @return string
529 */
530 protected function replaceNotIn($attribute, $parameters, $originalKey)
531 {
532 $text = $this->getReplacementText($attribute.'.not_in', 'not_in', $originalKey);
533
534 return str_replace([':attribute', ':not_in'], [$attribute], $text);
535 }
536
537 /**
538 * Replace all place-holders for the exista rule.
539 *
540 * @param $attribute
541 * @param $parameters
542 * @param $originalKey
543 *
544 * @return string
545 */
546 protected function replaceExists($attribute, $parameters, $originalKey)
547 {
548 $text = $this->getReplacementText($attribute.'.exists', 'exists', $originalKey);
549
550 return str_replace([':attribute', ':exists'], [$attribute], $text);
551 }
552 }
553