Completed
Push — master ( fc49af...187043 )
by
unknown
02:23
created

src/Concerns/FiltersQuery.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        $this->ensureAllFiltersExist();
26
27
        $this->addFiltersToQuery();
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());
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()
64
    {
65
        if (! $this->throwInvalidQueryExceptions) {
0 ignored issues
show
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...
66
            return;
67
        }
68
69
        $filterNames = $this->request->filters()->keys();
70
71
        $allowedFilterNames = $this->allowedFilters->map(function (AllowedFilter $allowedFilter) {
72
            return $allowedFilter->getName();
73
        });
74
75
        $diff = $filterNames->diff($allowedFilterNames);
76
77
        if ($diff->count()) {
78
            throw InvalidFilterQuery::filtersNotAllowed($diff, $allowedFilterNames);
79
        }
80
    }
81
}
82