|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Spatie\QueryBuilder\Filters\FiltersExact; |
|
7
|
|
|
use Spatie\QueryBuilder\Filters\FiltersScope; |
|
8
|
|
|
use Spatie\QueryBuilder\Filters\FiltersPartial; |
|
9
|
|
|
use Spatie\QueryBuilder\Filters\Filter as CustomFilter; |
|
10
|
|
|
|
|
11
|
|
|
class Filter |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $filterClass; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $property; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $propertyColumnName; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(string $property, $filterClass, $propertyColumnName = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->property = $property; |
|
25
|
|
|
|
|
26
|
|
|
$this->filterClass = $filterClass; |
|
27
|
|
|
|
|
28
|
|
|
$this->propertyColumnName = $propertyColumnName ?? $property; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function filter(Builder $builder, $value) |
|
32
|
|
|
{ |
|
33
|
|
|
$filterClass = $this->resolveFilterClass(); |
|
34
|
|
|
|
|
35
|
|
|
($filterClass)($builder, $value, $this->propertyColumnName); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function exact(string $property, $propertyColumnName = null) : self |
|
39
|
|
|
{ |
|
40
|
|
|
return new static($property, FiltersExact::class, $propertyColumnName); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function partial(string $property, $propertyColumnName = null) : self |
|
44
|
|
|
{ |
|
45
|
|
|
return new static($property, FiltersPartial::class, $propertyColumnName); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function scope(string $property, $propertyColumnName = null) : self |
|
49
|
|
|
{ |
|
50
|
|
|
return new static($property, FiltersScope::class, $propertyColumnName); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function custom(string $property, $filterClass, $propertyColumnName = null) : self |
|
54
|
|
|
{ |
|
55
|
|
|
return new static($property, $filterClass, $propertyColumnName); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getProperty(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->property; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function isForProperty(string $property): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->property === $property; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getPropertyColumnName(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->propertyColumnName; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function resolveFilterClass(): CustomFilter |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->filterClass instanceof CustomFilter) { |
|
76
|
|
|
return $this->filterClass; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new $this->filterClass; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|