Completed
Pull Request — master (#225)
by
unknown
01:19
created

Filter   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
wmc 23
lcom 3
cbo 1
dl 0
loc 131
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A filter() 0 16 4
A exact() 0 4 1
A partial() 0 4 1
A scope() 0 4 1
A custom() 0 4 1
A getProperty() 0 4 1
A isForProperty() 0 4 1
A ignore() 0 8 1
A getIgnored() 0 4 1
A default() 0 6 1
A getDefault() 0 4 1
A defaultSet() 0 4 1
A getColumnName() 0 4 1
A resolveFilterClass() 0 8 2
A resolveValueForFiltering() 0 10 4
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Builder;
7
use Spatie\QueryBuilder\Filters\FiltersExact;
8
use Spatie\QueryBuilder\Filters\FiltersScope;
9
use Spatie\QueryBuilder\Filters\FiltersPartial;
10
use Spatie\QueryBuilder\Filters\Filter as CustomFilter;
11
12
class Filter
13
{
14
    /** @var string|\Spatie\QueryBuilder\Filters\Filter */
15
    protected $filterClass;
16
17
    /** @var string */
18
    protected $property;
19
20
    /** @var string */
21
    protected $columnName;
22
23
    /** @var Collection */
24
    protected $ignored;
25
26
    /** @var */
27
    protected $default;
28
29
    public function __construct(string $property, $filterClass, ?string $columnName = null)
30
    {
31
        $this->property = $property;
32
33
        $this->filterClass = $filterClass;
34
35
        $this->ignored = Collection::make();
36
37
        $this->columnName = $columnName ?? $property;
38
    }
39
40
    public function filter(Builder $builder, $value)
41
    {
42
        $valueToFilter = $this->resolveValueForFiltering($value);
43
44
        if (isset($this->default) && is_null($valueToFilter)) {
45
            return $this->default;
46
        }
47
48
        if (is_null($valueToFilter)) {
49
            return;
50
        }
51
52
        $filterClass = $this->resolveFilterClass();
53
54
        ($filterClass)($builder, $valueToFilter, $this->columnName);
55
    }
56
57
    public static function exact(string $property, ?string $columnName = null) : self
58
    {
59
        return new static($property, FiltersExact::class, $columnName);
60
    }
61
62
    public static function partial(string $property, $columnName = null) : self
63
    {
64
        return new static($property, FiltersPartial::class, $columnName);
65
    }
66
67
    public static function scope(string $property, $columnName = null) : self
68
    {
69
        return new static($property, FiltersScope::class, $columnName);
70
    }
71
72
    public static function custom(string $property, $filterClass, $columnName = null) : self
73
    {
74
        return new static($property, $filterClass, $columnName);
75
    }
76
77
    public function getProperty(): string
78
    {
79
        return $this->property;
80
    }
81
82
    public function isForProperty(string $property): bool
83
    {
84
        return $this->property === $property;
85
    }
86
87
    public function ignore(...$values): self
88
    {
89
        $this->ignored = $this->ignored
90
            ->merge($values)
91
            ->flatten();
92
93
        return $this;
94
    }
95
96
    public function getIgnored(): array
97
    {
98
        return $this->ignored->toArray();
99
    }
100
101
    public function default($value): self
102
    {
103
        $this->default = $value;
104
105
        return $this;
106
    }
107
108
    public function getDefault()
109
    {
110
        return $this->default;
111
    }
112
113
    public function defaultSet(): bool
114
    {
115
        return isset($this->default);
116
    }
117
118
    public function getColumnName(): string
119
    {
120
        return $this->columnName;
121
    }
122
123
    private function resolveFilterClass(): CustomFilter
124
    {
125
        if ($this->filterClass instanceof CustomFilter) {
126
            return $this->filterClass;
127
        }
128
129
        return new $this->filterClass;
130
    }
131
132
    private function resolveValueForFiltering($property)
133
    {
134
        if (is_array($property)) {
135
            $remainingProperties = array_diff($property, $this->ignored->toArray());
136
137
            return ! empty($remainingProperties) ? $remainingProperties : null;
138
        }
139
140
        return ! $this->ignored->contains($property) ? $property : null;
141
    }
142
}
143