Filter::handle()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 8.8333
c 0
b 0
f 0
cc 7
nc 9
nop 3
1
<?php
2
3
namespace Zgabievi\KendoGridState\Filters;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Builder;
7
use Zgabievi\KendoGridState\Contracts\FilterAbstract;
8
9
class Filter extends FilterAbstract
10
{
11
    /**
12
     * @param Builder $builder
13
     * @param $descriptors
14
     * @return mixed
15
     */
16
    public function filter(Builder $builder, $descriptors)
17
    {
18
        $logic = array_get($descriptors, 'logic', 'and');
19
        $filters = array_get($descriptors, 'filters', []);
20
21
        if (! is_array($filters)) {
22
            return $builder;
23
        }
24
25
        foreach ($filters as $filter) {
26
            $this->handle($builder, $filter, $logic);
27
        }
28
    }
29
30
    /**
31
     * @param Builder $builder
32
     * @param $descriptor
33
     * @param $logic
34
     * @return \Illuminate\Database\Query\Builder|Builder
35
     */
36
    protected function handle(Builder $builder, $descriptor, $logic)
37
    {
38
        if (! is_array($descriptor) || ! array_key_exists('field', $descriptor)) {
39
            return $builder;
40
        }
41
42
        $logic = $logic === 'or' ? 'or' : 'and';
43
44
        if (array_key_exists('operator', $descriptor)) {
45
            if ($descriptor['operator'] === 'isnull') {
46
                return $builder->whereNull($descriptor['field'], $logic);
47
            }
48
49
            if ($descriptor['operator'] === 'isnotnull') {
50
                return $builder->whereNotNull($descriptor['field'], $logic);
51
            }
52
        }
53
54
        return $builder->where(
55
            $this->resolveField($descriptor),
56
            $this->resolveOperator($descriptor),
57
            $this->resolveValue($descriptor),
58
            $logic
59
        );
60
    }
61
62
    /**
63
     * @param $descriptor
64
     * @return \Illuminate\Database\Query\Expression
65
     */
66
    protected function resolveField($descriptor)
67
    {
68
        return $this->isBinary($descriptor) ? DB::raw('BINARY '.$descriptor['field']) : $descriptor['field'];
69
    }
70
71
    /**
72
     * @param $descriptor
73
     * @return string
74
     */
75
    protected function resolveOperator($descriptor)
76
    {
77
        if (! array_key_exists('operator', $descriptor)) {
78
            return '=';
79
        }
80
81
        switch ($descriptor['operator']) {
82
            case 'eq':
83
            case 'isempty':
84
                return '=';
85
86
            case 'neq':
87
            case 'isnotempty':
88
                return '!=';
89
90
            case 'lt':
91
                return '<';
92
93
            case 'lte':
94
                return '<=';
95
96
            case 'gt':
97
                return '>';
98
99
            case 'gte':
100
                return '>=';
101
102
            case 'startswith':
103
            case 'endswith':
104
            case 'contains':
105
                return 'LIKE';
106
107
            case 'doesnotcontain':
108
                return 'NOT LIKE';
109
        }
110
    }
111
112
    /**
113
     * @param $descriptor
114
     * @return mixed
115
     */
116
    protected function resolveValue($descriptor)
117
    {
118
        $value = array_get($descriptor, 'value', '');
119
        $operator = array_get($descriptor, 'operator', 'eq');
120
121
        switch ($operator) {
122
            case 'eq':
123
            case 'neq':
124
            case 'lt':
125
            case 'lte':
126
            case 'gt':
127
            case 'gte':
128
                return $value;
129
130
            case 'startswith':
131
                return $value.'%';
132
133
            case 'endswith':
134
                return '%'.$value;
135
136
            case 'contains':
137
            case 'doesnotcontain':
138
                return '%'.$value.'%';
139
140
            case 'isempty':
141
            case 'isnotempty':
142
                return '';
143
        }
144
    }
145
146
    /**
147
     * @param $descriptor
148
     * @param $sensitive
149
     * @return bool
150
     */
151
    protected function isBinary($descriptor)
152
    {
153
        $operator = array_get($descriptor, 'operator', 'eq');
154
        $ignoreCase = array_get($descriptor, 'ignoreCase', 'true');
155
156
        return $ignoreCase === 'false' && in_array($operator, ['eq', 'neq']);
157
    }
158
}
159