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