1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder\Filters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
|
9
|
|
|
class FiltersExact implements Filter |
10
|
|
|
{ |
11
|
|
|
protected $relationConstraints = []; |
12
|
|
|
|
13
|
|
|
public function __invoke(Builder $query, $value, string $property) : Builder |
14
|
|
|
{ |
15
|
|
|
if ($this->isRelationProperty($property)) { |
16
|
|
|
return $this->withRelationConstraint($query, $value, $property); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if (is_array($value)) { |
20
|
|
|
return $query->whereIn($property, $value); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $query->where($property, '=', $value); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function isRelationProperty(string $property) : bool |
27
|
|
|
{ |
28
|
|
|
return Str::contains($property, '.') && ! in_array($property, $this->relationConstraints); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function withRelationConstraint(Builder $query, $value, string $property) : Builder |
32
|
|
|
{ |
33
|
|
|
[$relation, $property] = collect(explode('.', $property)) |
|
|
|
|
34
|
|
|
->pipe(function (Collection $parts) { |
35
|
|
|
return [ |
36
|
|
|
$parts->except(count($parts) - 1)->map('camel_case')->implode('.'), |
37
|
|
|
$parts->last(), |
38
|
|
|
]; |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return $query->whereHas($relation, function (Builder $query) use ($value, $relation, $property) { |
42
|
|
|
$this->relationConstraints[] = $property = $query->getModel()->getTable().'.'.$property; |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->__invoke($query, $value, $property); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.