1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Zing\QueryBuilder\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Zing\QueryBuilder\Contracts\Filter; |
9
|
|
|
use Zing\QueryBuilder\Filters\BetweenDateFilter; |
10
|
|
|
use Zing\QueryBuilder\Filters\BetweenDateTimeFilter; |
11
|
|
|
use Zing\QueryBuilder\Filters\BetweenFilter; |
12
|
|
|
use Zing\QueryBuilder\Filters\CallbackFilter; |
13
|
|
|
use Zing\QueryBuilder\Filters\DateFilter; |
14
|
|
|
use Zing\QueryBuilder\Filters\ExactFilter; |
15
|
|
|
use Zing\QueryBuilder\Filters\PartialFilter; |
16
|
|
|
use Zing\QueryBuilder\Filters\ScopeFilter; |
17
|
|
|
use Zing\QueryBuilder\QueryConfiguration; |
18
|
|
|
|
19
|
|
|
trait FilterCreator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Zing\QueryBuilder\Contracts\Filter |
23
|
|
|
*/ |
24
|
|
|
protected $filter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $property; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Illuminate\Database\Query\Expression|string |
33
|
|
|
*/ |
34
|
|
|
protected $column; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Illuminate\Support\Collection<int, mixed>|null |
38
|
|
|
*/ |
39
|
|
|
protected $ignored; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
protected $default; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
protected $cast; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @phpstan-var non-empty-string |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $delimiter = ','; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Zing\QueryBuilder\Contracts\Filter $filter |
60
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
61
|
|
|
*/ |
62
|
51 |
|
public function __construct(string $property, $filter, $column = null) |
63
|
|
|
{ |
64
|
51 |
|
$this->property = $property; |
65
|
|
|
|
66
|
51 |
|
$this->filter = $filter; |
67
|
51 |
|
$this->column = $column ?? $property; |
68
|
51 |
|
$this->delimiter = QueryConfiguration::getDelimiter(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The input of property equals the column(or property if column is null). |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
75
|
|
|
*/ |
76
|
19 |
|
public static function exact(string $property, $column = null, bool $autoRelationConstraints = true): self |
77
|
|
|
{ |
78
|
19 |
|
return new self($property, new ExactFilter($autoRelationConstraints), $column); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The input of property in the column(or property if column is null). |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
85
|
|
|
*/ |
86
|
15 |
|
public static function partial(string $property, $column = null, bool $autoRelationConstraints = true): self |
87
|
|
|
{ |
88
|
15 |
|
return new self($property, new PartialFilter($autoRelationConstraints), $column); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Specify a scope(property if column is null) that will execute when the filter is requested. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
95
|
|
|
*/ |
96
|
4 |
|
public static function scope(string $property, $column = null): self |
97
|
|
|
{ |
98
|
4 |
|
return new self($property, new ScopeFilter(), $column); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Specify a custom filter that will execute when the filter is requested. |
103
|
|
|
* |
104
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
105
|
|
|
*/ |
106
|
2 |
|
public static function custom(string $property, Filter $filterClass, $column = null): self |
107
|
|
|
{ |
108
|
2 |
|
return new self($property, $filterClass, $column); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Specify a callable that will execute when the filter is requested. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
115
|
|
|
*/ |
116
|
2 |
|
public static function callback(string $property, callable $callback, $column = null): self |
117
|
|
|
{ |
118
|
2 |
|
return new self($property, new CallbackFilter($callback), $column); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The column(or property if column is null) between the input of property. |
123
|
|
|
* |
124
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
125
|
|
|
*/ |
126
|
3 |
|
public static function between(string $property, $column = null, bool $autoRelationConstraints = true): self |
127
|
|
|
{ |
128
|
3 |
|
return new self($property, new BetweenFilter($autoRelationConstraints), $column); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* The column(or property if column is null) between the datetime of the input of property. |
133
|
|
|
* |
134
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
135
|
|
|
*/ |
136
|
2 |
|
public static function betweenDateTime(string $property, $column = null, bool $autoRelationConstraints = true): self |
137
|
|
|
{ |
138
|
2 |
|
return new self($property, new BetweenDateTimeFilter($autoRelationConstraints), $column); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* The column(or property if column is null) between the date of the input of property. |
143
|
|
|
* |
144
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
145
|
|
|
*/ |
146
|
5 |
|
public static function betweenDate(string $property, $column = null, bool $autoRelationConstraints = true): self |
147
|
|
|
{ |
148
|
5 |
|
return new self($property, new BetweenDateFilter($autoRelationConstraints), $column); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* The column(or property if column is null) equals the date of the input of property. |
153
|
|
|
* |
154
|
|
|
* @param \Illuminate\Database\Query\Expression|string|null $column |
155
|
|
|
*/ |
156
|
1 |
|
public static function date(string $property, $column = null, bool $autoRelationConstraints = true): self |
157
|
|
|
{ |
158
|
1 |
|
return new self($property, new DateFilter($autoRelationConstraints), $column); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Specify a callable that will execute when the filter is requested or default when filter is requested not. |
163
|
|
|
*/ |
164
|
1 |
|
public static function boolean(string $property, callable $callback, ?callable $default = null): self |
165
|
|
|
{ |
166
|
1 |
|
return self::callback($property, function (Builder $query, $value, $property) use ($callback, $default) { |
|
|
|
|
167
|
1 |
|
return $query->when($value, $callback, $default); |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.