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