| 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 |
$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 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
protected function replaceAlpha($attribute, $parameters, $originalKey) |
| 196 |
{ |
| 197 |
$text = $this->getReplacementText( |
| 198 |
$attribute.'.alpha', 'alpha', $originalKey |
| 199 |
); |
| 200 |
|
| 201 |
return str_replace(':attribute', $attribute, $text); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Replace all place-holders for the alphanum rule. |
| 206 |
* |
| 207 |
* @param $attribute |
| 208 |
* @param $parameters |
| 209 |
* |
| 210 |
* @return string |
| 211 |
*/ |
| 212 |
protected function replaceAlphanum($attribute, $parameters, $originalKey) |
| 213 |
{ |
| 214 |
$text = $this->getReplacementText( |
| 215 |
$attribute.'.alphanum', 'alphanum', $originalKey |
| 216 |
); |
| 217 |
|
| 218 |
return str_replace(':attribute', $attribute, $text); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Replace all place-holders for the alphadash rule. |
| 223 |
* |
| 224 |
* @param $attribute |
| 225 |
* @param $parameters |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
protected function replaceAlphadash($attribute, $parameters, $originalKey) |
| 230 |
{ |
| 231 |
$text = $this->getReplacementText( |
| 232 |
$attribute.'.alphadash', 'alphadash', $originalKey |
| 233 |
); |
| 234 |
|
| 235 |
return str_replace(':attribute', $attribute, $text); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Replace all place-holders for the required rule. |
| 240 |
* |
| 241 |
* @param $attribute |
| 242 |
* @param $parameters |
| 243 |
* @param $originalKey |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
protected function replaceRequired($attribute, $parameters, $originalKey) |
| 248 |
{ |
| 249 |
$text = $this->getReplacementText( |
| 250 |
$attribute.'.required', 'required', $originalKey |
| 251 |
); |
| 252 |
|
| 253 |
return str_replace(':attribute', $attribute, $text); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Replace all place-holders for the required if rule. |
| 258 |
* |
| 259 |
* @param $attribute |
| 260 |
* @param $parameters |
| 261 |
* |
| 262 |
* @return string |
| 263 |
*/ |
| 264 |
protected function replaceRequiredIf($attribute, $parameters, $originalKey) |
| 265 |
{ |
| 266 |
if (preg_match('/\.\d\./', $attribute, $matches)) { |
| 267 |
$parameters[0] = str_replace(['.*.'], $matches, $parameters[0]); |
| 268 |
} |
| 269 |
|
| 270 |
$text = $this->getReplacementText($attribute.'.required_if', 'required_if', $originalKey); |
| 271 |
|
| 272 |
$value = end($parameters); |
| 273 |
|
| 274 |
return str_replace([ |
| 275 |
':attribute', ':other', ':value'], |
| 276 |
[$attribute, $parameters[0], $value], |
| 277 |
$text |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Replace all place-holders for the email rule. |
| 283 |
* |
| 284 |
* @param $attribute |
| 285 |
* @param $parameters |
| 286 |
* |
| 287 |
* @return string |
| 288 |
*/ |
| 289 |
protected function replaceEmail($attribute, $parameters, $originalKey) |
| 290 |
{ |
| 291 |
$text = $this->getReplacementText( |
| 292 |
$attribute.'.email', 'email', $originalKey |
| 293 |
); |
| 294 |
|
| 295 |
return str_replace(':attribute', $attribute, $text); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Replace all place-holders for the email rule. |
| 300 |
* |
| 301 |
* @param $attribute |
| 302 |
* @param $parameters |
| 303 |
* |
| 304 |
* @return string |
| 305 |
*/ |
| 306 |
protected function replaceDateformat($attribute, $parameters, $originalKey) |
| 307 |
{ |
| 308 |
$text = $this->getReplacementText($attribute.'.date_format', 'date_format', $$originalKey); |
| 309 |
|
| 310 |
return str_replace( |
| 311 |
[':attribute', ':value'], [$attribute, $parameters[0]], $text |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Replace all place-holders for the size rule. |
| 317 |
* |
| 318 |
* @param $attribute |
| 319 |
* @param $parameters |
| 320 |
* |
| 321 |
* @return string |
| 322 |
*/ |
| 323 |
protected function replaceSize($attribute, $parameters, $originalKey) |
| 324 |
{ |
| 325 |
$text = $this->getReplacementText($attribute.'.size', $this->makeBagKey($attribute, 'size'), $originalKey); |
| 326 |
|
| 327 |
return str_replace( |
| 328 |
[':attribute', ':size'], [$attribute, $parameters[0]], $text |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Replace all place-holders for the min rule. |
| 334 |
* |
| 335 |
* @param $attribute |
| 336 |
* @param $parameters |
| 337 |
* |
| 338 |
* @return string |
| 339 |
*/ |
| 340 |
protected function replaceMin($attribute, $parameters, $originalKey) |
| 341 |
{ |
| 342 |
$text = $this->getReplacementText($attribute.'.min', $this->makeBagKey($attribute, 'min'), $originalKey); |
| 343 |
|
| 344 |
return str_replace( |
| 345 |
[':attribute', ':min'], [$attribute, $parameters[0]], $text |
| 346 |
); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Replace all place-holders for the max rule. |
| 351 |
* |
| 352 |
* @param $attribute |
| 353 |
* @param $parameters |
| 354 |
* |
| 355 |
* @return string |
| 356 |
*/ |
| 357 |
protected function replaceMax($attribute, $parameters, $originalKey) |
| 358 |
{ |
| 359 |
$text = $this->getReplacementText($attribute.'.max', $this->makeBagKey($attribute, 'max'), $originalKey); |
| 360 |
|
| 361 |
return str_replace( |
| 362 |
[':attribute', ':max'], [$attribute, $parameters[0]], $text |
| 363 |
); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Replace all place-holders for the min rule. |
| 368 |
* |
| 369 |
* @param $attribute |
| 370 |
* @param $parameters |
| 371 |
* |
| 372 |
* @return string |
| 373 |
*/ |
| 374 |
protected function replaceSame($attribute, $parameters, $originalKey) |
| 375 |
{ |
| 376 |
$text = $this->getReplacementText($attribute.'.same', 'same', $originalKey); |
| 377 |
|
| 378 |
return str_replace( |
| 379 |
[':attribute', ':other'], [$attribute, $parameters[0]], $text |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Replace all place-holders for the url rule. |
| 385 |
* |
| 386 |
* @param $attribute |
| 387 |
* @param $parameters |
| 388 |
* |
| 389 |
* @return string |
| 390 |
*/ |
| 391 |
protected function replaceUrl($attribute, $parameters) |
| 392 |
{ |
| 393 |
$text = $this->getReplacementText($attribute.'.url', 'url'); |
| 394 |
|
| 395 |
return str_replace(':attribute', $attribute, $text); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Replace all place-holders for the numeric rule. |
| 400 |
* |
| 401 |
* @param $attribute |
| 402 |
* @param $parameters |
| 403 |
* |
| 404 |
* @return string |
| 405 |
*/ |
| 406 |
protected function replaceNumeric($attribute, $parameters) |
| 407 |
{ |
| 408 |
$text = $this->getReplacementText($attribute.'.numeric', 'numeric'); |
| 409 |
|
| 410 |
return str_replace(':attribute', $attribute, $text); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Replace all place-holders for the mimes rule. |
| 415 |
* |
| 416 |
* @param $attribute |
| 417 |
* @param $parameters |
| 418 |
* |
| 419 |
* @return string |
| 420 |
*/ |
| 421 |
protected function replaceMimes($attribute, $parameters) |
| 422 |
{ |
| 423 |
$text = $this->getReplacementText($attribute.'.mimes', 'mimes'); |
| 424 |
|
| 425 |
return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Replace all place-holders for the mimetypes rule. |
| 430 |
* |
| 431 |
* @param $attribute |
| 432 |
* @param $parameters |
| 433 |
* |
| 434 |
* @return string |
| 435 |
*/ |
| 436 |
protected function replaceMimetypes($attribute, $parameters) |
| 437 |
{ |
| 438 |
$text = $this->getReplacementText($attribute.'.mimetypes', 'mimetypes'); |
| 439 |
|
| 440 |
return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Replace all place-holders for the unique rule. |
| 445 |
* |
| 446 |
* @param $attribute |
| 447 |
* @param $parameters |
| 448 |
* |
| 449 |
* @return string |
| 450 |
*/ |
| 451 |
protected function replaceUnique($attribute, $parameters) |
| 452 |
{ |
| 453 |
$text = $this->getReplacementText($attribute.'.unique', 'unique'); |
| 454 |
|
| 455 |
return str_replace(':attribute', $attribute, $text); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Replace all place-holders for the digits rule. |
| 460 |
* |
| 461 |
* @param $attribute |
| 462 |
* @param $parameters |
| 463 |
* |
| 464 |
* @return string |
| 465 |
*/ |
| 466 |
protected function replaceDigits($attribute, $parameters) |
| 467 |
{ |
| 468 |
$text = $this->getReplacementText($attribute.'.digits', 'digits'); |
| 469 |
|
| 470 |
return str_replace([':attribute', ':digits'], [$attribute, $parameters[0]], $text); |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Replace all place-holders for the array rule. |
| 475 |
* |
| 476 |
* @param $attribute |
| 477 |
* @param $parameters |
| 478 |
* |
| 479 |
* @return string |
| 480 |
*/ |
| 481 |
protected function replaceArray($attribute, $parameters) |
| 482 |
{ |
| 483 |
$text = $this->getReplacementText($attribute.'.array', 'array'); |
| 484 |
|
| 485 |
return str_replace([':attribute', ':array'], [$attribute], $text); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Replace all place-holders for the in rule. |
| 490 |
* |
| 491 |
* @param $attribute |
| 492 |
* @param $parameters |
| 493 |
* |
| 494 |
* @return string |
| 495 |
*/ |
| 496 |
protected function replaceIn($attribute, $parameters) |
| 497 |
{ |
| 498 |
$text = $this->getReplacementText($attribute.'.in', 'in'); |
| 499 |
|
| 500 |
return str_replace([':attribute', ':in'], [$attribute], $text); |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Replace all place-holders for the not_in rule. |
| 505 |
* |
| 506 |
* @param $attribute |
| 507 |
* @param $parameters |
| 508 |
* |
| 509 |
* @return string |
| 510 |
*/ |
| 511 |
protected function replaceNotIn($attribute, $parameters) |
| 512 |
{ |
| 513 |
$text = $this->getReplacementText($attribute.'.not_in', 'not_in'); |
| 514 |
|
| 515 |
return str_replace([':attribute', ':not_in'], [$attribute], $text); |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Replace all place-holders for the exista rule. |
| 520 |
* |
| 521 |
* @param $attribute |
| 522 |
* @param $parameters |
| 523 |
* |
| 524 |
* @return string |
| 525 |
*/ |
| 526 |
protected function replaceExists($attribute, $parameters) |
| 527 |
{ |
| 528 |
$text = $this->getReplacementText($attribute.'.exists', 'exists'); |
| 529 |
|
| 530 |
return str_replace([':attribute', ':exists'], [$attribute], $text); |
| 531 |
} |
| 532 |
} |
| 533 |
|