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