CustomFilterKeyword.php 719 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class CustomFilterKeyword extends Model
  5. {
  6. protected $fillable = [
  7. 'keyword', 'whole_word', 'custom_filter_id',
  8. ];
  9. protected $casts = [
  10. 'whole_word' => 'boolean',
  11. ];
  12. public function customFilter()
  13. {
  14. return $this->belongsTo(CustomFilter::class);
  15. }
  16. public function setKeywordAttribute($value)
  17. {
  18. $this->attributes['keyword'] = mb_strtolower(trim($value));
  19. }
  20. public function toRegex()
  21. {
  22. $pattern = preg_quote($this->keyword, '/');
  23. if ($this->whole_word) {
  24. $pattern = '\b'.$pattern.'\b';
  25. }
  26. return '/'.$pattern.'/i';
  27. }
  28. }