Просмотр файла app/Models/Antimat.php

Размер файла: 1.17Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Capsule\Manager as DB;
  8.  
  9. /**
  10. * Class Antimat
  11. *
  12. * @property int id
  13. * @property string string
  14. */
  15. class Antimat extends BaseModel
  16. {
  17. /**
  18. * The table associated with the model.
  19. *
  20. * @var string
  21. */
  22. protected $table = 'antimat';
  23.  
  24. /**
  25. * Indicates if the model should be timestamped.
  26. *
  27. * @var bool
  28. */
  29. public $timestamps = false;
  30.  
  31. /**
  32. * The attributes that aren't mass assignable.
  33. *
  34. * @var array
  35. */
  36. protected $guarded = [];
  37.  
  38. /**
  39. * Очищает строку от мата по базе слов
  40. *
  41. * @param string $str строка
  42. * @return string обработанная строка
  43. */
  44. public static function replace($str): string
  45. {
  46. $words = self::query()
  47. ->orderByDesc(DB::connection()->raw('CHAR_LENGTH(string)'))
  48. ->pluck('string')
  49. ->all();
  50.  
  51. if ($words) {
  52. foreach ($words as $word) {
  53. $str = preg_replace('/' . preg_quote($word, '/') . '/iu', '***', $str);
  54. }
  55. }
  56.  
  57. return $str;
  58. }
  59. }