1 | <?php |
||
2 | |||
3 | namespace TopviewDigital\TranslationHelper\Model; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | |||
7 | class VocabCite extends Model |
||
8 | { |
||
9 | protected $fillable = [ |
||
10 | 'file', 'line', 'function', 'class', 'code', |
||
11 | ]; |
||
12 | |||
13 | public function __construct(array $attributes = []) |
||
14 | { |
||
15 | $connection = config('trans-helper.database.connection') ?: config('database.default'); |
||
16 | $this->setConnection($connection); |
||
17 | $this->setTable('_trans_helper_cites'); |
||
18 | parent::__construct($attributes); |
||
19 | } |
||
20 | |||
21 | public function terms() |
||
22 | { |
||
23 | return $this->belongsToMany( |
||
24 | VocabTerm::class, |
||
25 | '_trans_helper_links', |
||
26 | 'cited', |
||
27 | 'vocab', |
||
28 | 'id', |
||
29 | 'id' |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | public function sweep() |
||
34 | { |
||
35 | $line = explode("\n", file_get_contents(base_path().$this->file)); |
||
0 ignored issues
–
show
|
|||
36 | $line = $line[$this->line - 1] ?? ''; |
||
0 ignored issues
–
show
|
|||
37 | $count = 0; |
||
38 | if ($line) { |
||
39 | foreach ($this->terms()->get() as $term) { |
||
40 | $keywords = ["localize('{$term->term}')", 'localize("'.$term->term.'")']; |
||
41 | $matched = array_filter(array_map(function ($u) use ($line) { |
||
42 | return strpos($line, $u); |
||
43 | }, $keywords), 'strlen'); |
||
44 | if (empty($matched)) { |
||
45 | $this->terms()->detach($term->id); |
||
46 | } |
||
47 | $count += count($matched); |
||
48 | } |
||
49 | } |
||
50 | if ($count == 0) { |
||
0 ignored issues
–
show
|
|||
51 | $this->delete(); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.