Completed
Push — master ( 187043...48cde4 )
by
unknown
01:30
created

FiltersQuery::allowedFilters()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Spatie\QueryBuilder\AllowedFilter;
6
use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery;
7
8
trait FiltersQuery
9
{
10
    /** @var \Illuminate\Support\Collection */
11
    protected $allowedFilters;
12
13
    public function allowedFilters($filters): self
14
    {
15
        $filters = is_array($filters) ? $filters : func_get_args();
16
17
        $this->allowedFilters = collect($filters)->map(function ($filter) {
18
            if ($filter instanceof AllowedFilter) {
19
                return $filter;
20
            }
21
22
            return AllowedFilter::partial($filter);
23
        });
24
25
        if ($this->ensureAllFiltersExist()) {
26
            $this->addFiltersToQuery();
27
        }
28
29
        return $this;
30
    }
31
32
    protected function addFiltersToQuery()
33
    {
34
        $this->allowedFilters->each(function (AllowedFilter $filter) {
35
            if ($this->isFilterRequested($filter)) {
36
                $value = $this->request->filters()->get($filter->getName());
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
                $filter->filter($this, $value);
38
39
                return;
40
            }
41
42
            if ($filter->hasDefault()) {
43
                $filter->filter($this, $filter->getDefault());
44
45
                return;
46
            }
47
        });
48
    }
49
50
    protected function findFilter(string $property): ?AllowedFilter
51
    {
52
        return $this->allowedFilters
53
            ->first(function (AllowedFilter $filter) use ($property) {
54
                return $filter->isForFilter($property);
55
            });
56
    }
57
58
    protected function isFilterRequested(AllowedFilter $allowedFilter): bool
59
    {
60
        return $this->request->filters()->has($allowedFilter->getName());
61
    }
62
63
    protected function ensureAllFiltersExist(): bool
64
    {
65
        $filterNames = $this->request->filters()->keys();
66
67
        $allowedFilterNames = $this->allowedFilters->map(function (AllowedFilter $allowedFilter) {
68
            return $allowedFilter->getName();
69
        });
70
71
        $diff = $filterNames->diff($allowedFilterNames);
72
73
        if ($diff->count()) {
74
            if ($this->throwInvalidQueryExceptions) {
0 ignored issues
show
Bug introduced by
The property throwInvalidQueryExceptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
                throw InvalidFilterQuery::filtersNotAllowed($diff, $allowedFilterNames);
76
            } else {
77
                return false;
78
            }
79
        }
80
81
        return true;
82
    }
83
}
84