FilterSpamKeyword.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. use App\Models\Config;
  5. class FilterSpamKeyword implements Rule
  6. {
  7. /**
  8. * Create a new rule instance.
  9. *
  10. * @return void
  11. */
  12. public function __construct()
  13. {
  14. //
  15. }
  16. /**
  17. * Determine if the validation rule passes.
  18. *
  19. * @param string $attribute
  20. * @param mixed $value
  21. * @return bool
  22. */
  23. public function passes($attribute, $value)
  24. {
  25. $spamWord = array_filter(explode(',', trim((new Config)->item("spam_word"))));
  26. if ($spamWord) {
  27. for ($i = 0; $i < count($spamWord); $i++) {
  28. $str = trim($spamWord[$i]);
  29. if ($value) {
  30. $pos = stripos($value, $str);
  31. if ($pos !== false) {
  32. return false;
  33. }
  34. }
  35. }
  36. }
  37. return true;
  38. }
  39. /**
  40. * Get the validation error message.
  41. *
  42. * @return string
  43. */
  44. public function message()
  45. {
  46. return ':attribute 에 금지 단어가 포함되었습니다.';
  47. }
  48. }