TextFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 27
c 2
b 1
f 0
dl 0
loc 50
rs 10
ccs 19
cts 24
cp 0.7917
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 26 6
A like() 0 8 1
A render() 0 4 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Filters;
4
5
class TextFilter extends AbstractFilter
6
{
7
    protected $like = [
8
        'left'  => true,
9
        'right' => true,
10
    ];
11
12 2
    public function like($left = true, $right = true)
13
    {
14 2
        $this->like = [
15 2
            'left'  => $left,
16 2
            'right' => $right,
17
        ];
18
19 2
        return $this;
20
    }
21
22 1
    public function render()
23
    {
24 1
        return view('jarboe::crud.filters.text', [
25 1
            'filter' => $this,
26
        ]);
27
    }
28
29 2
    public function apply($query)
30
    {
31 2
        $value = $this->value();
32 2
        if (is_null($value)) {
33 1
            return;
34
        }
35
36 1
        $leftPart  = $this->like['left'] ? '%' : '';
37 1
        $rightPart = $this->like['right'] ? '%' : '';
38 1
        $sign      = array_filter($this->like) ? 'like' : $this->sign;
39
40 1
        if ($this->field()->isRelationField()) {
41
            $query->whereHas($this->field()->getRelationMethod(), function ($query) use ($value, $sign, $leftPart, $rightPart) {
42
                $query->where(
43
                    $this->field()->getRelationTitleField(),
44
                    $sign,
45
                    $leftPart . $value . $rightPart
46
                );
47
            });
48
            return;
49
        }
50
51 1
        $query->where(
52 1
            $this->field->name(),
53
            $sign,
54 1
            $leftPart . $value . $rightPart
55
        );
56 1
    }
57
}
58