MaxMultiLine.php 824 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Contracts\Validation\InvokableRule;
  5. class MaxMultiLine implements InvokableRule
  6. {
  7. private $maxCharacters;
  8. public function __construct($maxCharacters)
  9. {
  10. $this->maxCharacters = $maxCharacters;
  11. }
  12. /**
  13. * Run the validation rule.
  14. *
  15. * @param string $attribute
  16. * @param mixed $value
  17. * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
  18. * @return void
  19. */
  20. public function __invoke($attribute, $value, $fail)
  21. {
  22. $realCount = Str::length($value) - Str::substrCount($value, "\r\n");
  23. if($realCount > $this->maxCharacters)
  24. {
  25. $fail('validation.max.string')->translate(['max' => $this->maxCharacters]);
  26. }
  27. }
  28. }