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 $columnName; |
21
|
|
|
|
22
|
|
|
public function __construct(string $property, $filterClass, $columnName = null) |
23
|
|
|
{ |
24
|
|
|
$this->property = $property; |
25
|
|
|
|
26
|
|
|
$this->filterClass = $filterClass; |
27
|
|
|
|
28
|
|
|
$this->columnName = $columnName ?? $property; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function filter(Builder $builder, $value) |
32
|
|
|
{ |
33
|
|
|
$filterClass = $this->resolveFilterClass(); |
34
|
|
|
|
35
|
|
|
($filterClass)($builder, $value, $this->columnName); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function exact(string $property, $columnName = null) : self |
39
|
|
|
{ |
40
|
|
|
return new static($property, FiltersExact::class, $columnName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function partial(string $property, $columnName = null) : self |
44
|
|
|
{ |
45
|
|
|
return new static($property, FiltersPartial::class, $columnName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function scope(string $property, $columnName = null) : self |
49
|
|
|
{ |
50
|
|
|
return new static($property, FiltersScope::class, $columnName); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function custom(string $property, $filterClass, $columnName = null) : self |
54
|
|
|
{ |
55
|
|
|
return new static($property, $filterClass, $columnName); |
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 getcolumnName(): string |
69
|
|
|
{ |
70
|
|
|
return $this->columnName; |
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
|
|
|
|