1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Zing\QueryBuilder; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Zing\QueryBuilder\Concerns\FilterCreator; |
10
|
|
|
use Zing\QueryBuilder\Enums\CastType; |
11
|
|
|
|
12
|
|
|
class Filter |
13
|
|
|
{ |
14
|
1 |
|
use FilterCreator; |
15
|
|
|
|
16
|
28 |
|
public function filter($query, $value) |
17
|
|
|
{ |
18
|
28 |
|
$value = $this->resolveValueForFiltering($value); |
19
|
28 |
|
if ($value === null || $value === '') { |
20
|
1 |
|
return $query; |
21
|
|
|
} |
22
|
|
|
|
23
|
27 |
|
return $this->filter->apply($query, $value, $this->column); |
24
|
|
|
} |
25
|
|
|
|
26
|
28 |
|
public function getProperty(): string |
27
|
|
|
{ |
28
|
28 |
|
return $this->property; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function isForProperty(string $property): bool |
32
|
|
|
{ |
33
|
1 |
|
return $this->property === $property; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function getColumn(): string |
37
|
|
|
{ |
38
|
1 |
|
return $this->column; |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
public function withCast($cast) |
42
|
|
|
{ |
43
|
7 |
|
$this->cast = $cast; |
44
|
|
|
|
45
|
7 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function hasCast(): bool |
49
|
|
|
{ |
50
|
1 |
|
return $this->cast !== null; |
51
|
|
|
} |
52
|
|
|
|
53
|
20 |
|
public function getCast() |
54
|
|
|
{ |
55
|
20 |
|
return $this->cast; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function ignore(...$values): self |
59
|
|
|
{ |
60
|
1 |
|
$this->ignored = $this->ignored |
61
|
1 |
|
->merge($values) |
62
|
1 |
|
->flatten(); |
63
|
|
|
|
64
|
1 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
29 |
|
public function getIgnored(): Collection |
68
|
|
|
{ |
69
|
29 |
|
return $this->ignored; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function default($value): self |
73
|
|
|
{ |
74
|
1 |
|
$this->default = $value; |
75
|
|
|
|
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function getDefault() |
80
|
|
|
{ |
81
|
1 |
|
return $this->default; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function hasDefault(): bool |
85
|
|
|
{ |
86
|
1 |
|
return $this->default !== null; |
87
|
|
|
} |
88
|
|
|
|
89
|
20 |
|
protected function castValue($value) |
90
|
|
|
{ |
91
|
20 |
|
switch ($this->getCast()) { |
92
|
20 |
|
case CastType::CAST_STRING: |
93
|
1 |
|
return $value; |
94
|
19 |
|
case CastType::CAST_BOOLEAN: |
95
|
2 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
96
|
18 |
|
case CastType::CAST_INTEGER: |
97
|
1 |
|
return filter_var($value, FILTER_VALIDATE_INT); |
98
|
17 |
|
case CastType::CAST_ARRAY: |
99
|
1 |
|
return explode(',', $value); |
100
|
|
|
default: |
101
|
16 |
|
if (in_array(strtolower($value), ['true', 'false'], true)) { |
102
|
1 |
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
103
|
|
|
} |
104
|
|
|
|
105
|
16 |
|
if (Str::contains($value, ',')) { |
106
|
6 |
|
$value = explode(',', $value); |
107
|
|
|
} |
108
|
|
|
|
109
|
16 |
|
return $value; |
110
|
|
|
}//end switch |
111
|
|
|
} |
112
|
|
|
|
113
|
29 |
|
protected function resolveValueForFiltering($value) |
114
|
|
|
{ |
115
|
29 |
|
if (is_string($value)) { |
116
|
20 |
|
$value = $this->castValue($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
29 |
|
if (is_array($value)) { |
120
|
13 |
|
$remainingProperties = array_diff($value, $this->getIgnored()->toArray()); |
121
|
|
|
|
122
|
13 |
|
return ! empty($remainingProperties) ? $remainingProperties : null; |
123
|
|
|
} |
124
|
|
|
|
125
|
17 |
|
return ! $this->getIgnored()->contains($value) ? $value : null; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|