• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visavi / rotor / 26723576697

31 May 2026 08:27PM UTC coverage: 14.172% (-0.4%) from 14.618%
26723576697

push

github

visavi
Перенес файлы обновлений бд в отдельную папку

785 of 5539 relevant lines covered (14.17%)

1.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/app/Traits/SearchableTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Traits;
6

7
use App\Models\Search;
8

9
trait SearchableTrait
10
{
11
    /**
12
     * Поля модели, которые должны быть включены в поиск
13
     */
14
    protected function searchableFields(): array
×
15
    {
16
        return [];
×
17
    }
18

19
    /**
20
     * Boot the trait
21
     */
22
    public static function bootSearchableTrait(): void
60✔
23
    {
24
        static::created(static function ($model) {
60✔
25
            if ($model->shouldBeSearchable()) {
3✔
26
                $model->updateSearchIndex();
3✔
27
            }
28
        });
60✔
29

30
        static::updated(static function ($model) {
60✔
31
            if ($model->shouldBeSearchable()) {
×
32
                $model->updateSearchIndex();
×
33
            } else {
34
                $model->removeFromSearchIndex();
×
35
            }
36
        });
60✔
37

38
        static::deleted(static function ($model) {
60✔
39
            $model->removeFromSearchIndex();
×
40
        });
60✔
41
    }
42

43
    /**
44
     * Проверяет, нужно ли добавлять запись в поисковый индекс
45
     */
46
    public function shouldBeSearchable(): bool
3✔
47
    {
48
        return (bool) ($this->active ?? true);
3✔
49
    }
50

51
    /**
52
     * Обновляет запись в поисковом индексе
53
     */
54
    public function updateSearchIndex(): void
3✔
55
    {
56
        if (! $this->searchableFields()) {
3✔
57
            return;
×
58
        }
59

60
        $text = $this->buildSearchText();
3✔
61

62
        Search::query()->updateOrCreate(
3✔
63
            [
3✔
64
                'relate_type' => $this->getMorphClass(),
3✔
65
                'relate_id'   => $this->getKey(),
3✔
66
            ],
3✔
67
            [
3✔
68
                'text'       => $text,
3✔
69
                'created_at' => $this->created_at,
3✔
70
            ]
3✔
71
        );
3✔
72
    }
73

74
    /**
75
     * Удаляет запись из поискового индекса
76
     */
77
    public function removeFromSearchIndex(): void
×
78
    {
79
        Search::query()
×
80
            ->where('relate_type', $this->getMorphClass())
×
81
            ->where('relate_id', $this->getKey())
×
82
            ->delete();
×
83
    }
84

85
    /**
86
     * Подготавливает текст для поиска из указанных полей
87
     */
88
    public function buildSearchText(): string
3✔
89
    {
90
        $values = [];
3✔
91
        $fields = $this->searchableFields();
3✔
92

93
        foreach ($fields as $field) {
3✔
94
            if (isset($this->$field)) {
3✔
95
                $values[] = $this->$field;
3✔
96
            }
97
        }
98

99
        $searchText = strip_tags(implode(' ', $values));
3✔
100
        $searchText = html_entity_decode($searchText, ENT_QUOTES, 'UTF-8');
3✔
101

102
        // убираем BB-код скобки и символы которые мешают fulltext
103
        $searchText = preg_replace('/\[(.*?)]/u', ' ', $searchText); // Удалить после перехода на теги
3✔
104
        $searchText = preg_replace('/[<>{}\[\]|\\\\^~*?+\-()":;!@#$%\/=\'`]/u', ' ', $searchText);
3✔
105

106
        // схлопываем множественные пробелы
107
        $searchText = preg_replace('/\s+/u', ' ', $searchText);
3✔
108

109
        return trim($searchText);
3✔
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc