|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zing\QueryBuilder\Filters; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Query\Expression; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use Zing\QueryBuilder\Concerns\NestedRelation; |
|
11
|
|
|
use Zing\QueryBuilder\Contracts\Filter; |
|
12
|
|
|
|
|
13
|
|
|
class ExactFilter implements Filter |
|
14
|
|
|
{ |
|
15
|
|
|
use NestedRelation; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $relationConstraints = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param mixed $value |
|
24
|
|
|
* @param string|\Illuminate\Database\Query\Expression $property |
|
25
|
|
|
*/ |
|
26
|
23 |
|
public function apply(Builder $query, $value, $property): Builder |
|
27
|
|
|
{ |
|
28
|
23 |
|
if ($property instanceof Expression) { |
|
29
|
1 |
|
return $this->withPropertyConstraint($query, $value, $property); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
22 |
|
if ($this->isRelationProperty($query, $property)) { |
|
33
|
3 |
|
return $this->withRelationConstraint($query, $value, $property); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
22 |
|
return $this->withPropertyConstraint($query, $value, $property); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param mixed $value |
|
41
|
|
|
* @param \Illuminate\Database\Query\Expression|string $property |
|
42
|
|
|
*/ |
|
43
|
8 |
|
protected function withPropertyConstraint(Builder $query, $value, $property): Builder |
|
44
|
|
|
{ |
|
45
|
8 |
|
if (is_array($value)) { |
|
46
|
2 |
|
return $query->whereIn($property, $value); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
return $query->where($property, '=', $value); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
22 |
|
protected function isRelationProperty(Builder $query, string $property): bool |
|
53
|
|
|
{ |
|
54
|
22 |
|
if (! Str::contains($property, '.')) { |
|
55
|
18 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
if (in_array($property, $this->relationConstraints, true)) { |
|
59
|
3 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
return ! Str::startsWith($property, $query->getModel()->getTable() . '.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
*/ |
|
68
|
3 |
|
protected function withRelationConstraint(Builder $query, $value, string $property): Builder |
|
69
|
|
|
{ |
|
70
|
3 |
|
[$relation, $property] = $this->resolveNestedRelation($property); |
|
71
|
|
|
|
|
72
|
3 |
|
return $query->whereHas( |
|
73
|
3 |
|
$relation, |
|
74
|
3 |
|
function ($query) use ($value, $property): void { |
|
75
|
3 |
|
$property = $query->getModel() |
|
76
|
3 |
|
->getTable() . '.' . $property; |
|
77
|
3 |
|
$this->relationConstraints[] = $property; |
|
78
|
|
|
|
|
79
|
3 |
|
$this->apply($query, $value, $property); |
|
80
|
3 |
|
} |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|