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 */ |
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
|
|
|
public function __construct(string $property, $filterClass, $columnName = null) |
27
|
|
|
{ |
28
|
|
|
$this->property = $property; |
29
|
|
|
|
30
|
|
|
$this->filterClass = $filterClass; |
31
|
|
|
|
32
|
|
|
$this->ignored = Collection::make(); |
33
|
|
|
|
34
|
|
|
$this->columnName = $columnName ?? $property; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function filter(Builder $builder, $value) |
38
|
|
|
{ |
39
|
|
|
$valueToFilter = $this->resolveValueForFiltering($value); |
40
|
|
|
|
41
|
|
|
if (empty($valueToFilter)) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$filterClass = $this->resolveFilterClass(); |
46
|
|
|
|
47
|
|
|
($filterClass)($builder, $valueToFilter, $this->columnName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function exact(string $property, $columnName = null) : self |
51
|
|
|
{ |
52
|
|
|
return new static($property, FiltersExact::class, $columnName); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function partial(string $property, $columnName = null) : self |
56
|
|
|
{ |
57
|
|
|
return new static($property, FiltersPartial::class, $columnName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function scope(string $property, $columnName = null) : self |
61
|
|
|
{ |
62
|
|
|
return new static($property, FiltersScope::class, $columnName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function custom(string $property, $filterClass, $columnName = null) : self |
66
|
|
|
{ |
67
|
|
|
return new static($property, $filterClass, $columnName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getProperty(): string |
71
|
|
|
{ |
72
|
|
|
return $this->property; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function isForProperty(string $property): bool |
76
|
|
|
{ |
77
|
|
|
return $this->property === $property; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function ignore(...$values): self |
81
|
|
|
{ |
82
|
|
|
$this->ignored = $this->ignored |
83
|
|
|
->merge($values) |
84
|
|
|
->flatten(); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getIgnored(): array |
90
|
|
|
{ |
91
|
|
|
return $this->ignored->toArray(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getColumnName(): string |
95
|
|
|
{ |
96
|
|
|
return $this->columnName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function resolveFilterClass(): CustomFilter |
100
|
|
|
{ |
101
|
|
|
if ($this->filterClass instanceof CustomFilter) { |
102
|
|
|
return $this->filterClass; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new $this->filterClass; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function resolveValueForFiltering($property) |
109
|
|
|
{ |
110
|
|
|
if (is_array($property)) { |
111
|
|
|
return array_diff($property, $this->ignored->toArray()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return ! $this->ignored->contains($property) ? $property : null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|