ApplyFilter::isValidFiltersArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Queries\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sfneal\Filters\FilterInterface;
7
use Sfneal\Filters\FilterNullableInterface;
8
9
trait ApplyFilter
10
{
11
    /**
12
     * Apply a filter to a Query if the Filter class is valid.
13
     *
14
     * @param Builder $query
15
     * @param string $filterName
16
     * @param mixed $filterValue
17
     * @param FilterInterface|FilterNullableInterface $decorator
18
     * @return Builder
19
     */
20
    public function applyFilterToQuery(Builder $query, string $filterName, $filterValue = null, $decorator = null)
21
    {
22
        // Get the Filter class if none is provided
23
        $decorator = $decorator ?? self::getFilterClass($filterName);
0 ignored issues
show
Bug Best Practice introduced by
The method Sfneal\Queries\Traits\Ap...ilter::getFilterClass() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $decorator = $decorator ?? self::/** @scrutinizer ignore-call */ getFilterClass($filterName);
Loading history...
24
25
        // Apply Filter class if it exists and is a filterable attribute
26
        if (! is_null($decorator) && self::isValidFilterClass($decorator, $filterName)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Sfneal\Queries\Traits\Ap...r::isValidFilterClass() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        if (! is_null($decorator) && self::/** @scrutinizer ignore-call */ isValidFilterClass($decorator, $filterName)) {
Loading history...
27
            $query = $decorator::apply($query, $filterValue);
28
        }
29
30
        return $query;
31
    }
32
33
    /**
34
     * Determine if the Filter decorator class exists and is valid.
35
     *
36
     * Check that the Filter class exists or the $decorator is an object.
37
     * Then check that the attribute is declared as filterable.
38
     *
39
     * @param string|mixed $decorator
40
     * @param string $attribute
41
     * @return bool
42
     */
43
    private function isValidFilterClass($decorator, string $attribute): bool
44
    {
45
        return (class_exists($decorator) || is_object($decorator)) && $this->isFilterableAttribute($attribute);
46
    }
47
48
    /**
49
     * Create a filter decorator by manipulating filter name to find the corresponding filter class.
50
     *
51
     * @param $name
52
     * @return null|string|FilterInterface
53
     */
54
    private function getFilterClass($name)
55
    {
56
        // Check if an array of attribute keys and Filter class values is defined
57
        if (self::isValidFiltersArray($this->attribute_filters) && $this->isFilterableAttribute($name)) {
58
            return $this->attribute_filters[$name];
59
        }
60
    }
61
62
    /**
63
     * Check if the filters array is valid for querying.
64
     *
65
     * @param $filters
66
     * @return bool
67
     */
68
    private static function isValidFiltersArray($filters)
69
    {
70
        return ! empty($filters) && is_array($filters);
71
    }
72
73
    /**
74
     * Determine if a particular filter is in the array of filterable attributes.
75
     *
76
     * @param string $name
77
     * @return bool
78
     */
79
    private function isFilterableAttribute(string $name)
80
    {
81
        if (property_exists($this, 'attribute_filters')) {
82
            return in_array($name, array_keys($this->attribute_filters));
83
        } else {
84
            return true;
85
        }
86
    }
87
}
88