Passed
Pull Request — master (#32)
by Zing
09:34
created

Filter::filter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Zing\QueryBuilder\Concerns\FilterCreator;
10
use Zing\QueryBuilder\Enums\CastType;
11
12
class Filter
13
{
14
    use FilterCreator;
15
16 26
    public function filter($query, $value)
17
    {
18 26
        $value = $this->resolveValueForFiltering($value);
19 26
        if ($value === null || $value === '') {
20 1
            return $query;
21
        }
22
23 25
        return $this->filter->apply($query, $value, $this->column);
24
    }
25
26 26
    public function getProperty(): string
27
    {
28 26
        return $this->property;
29
    }
30
31 1
    public function isForProperty(string $property): bool
32
    {
33 1
        return $this->property === $property;
34
    }
35
36 1
    public function getColumn(): string
37
    {
38 1
        return $this->column;
39
    }
40
41 6
    public function withCast($cast)
42
    {
43 6
        $this->cast = $cast;
44
45 6
        return $this;
46
    }
47
48 1
    public function hasCast(): bool
49
    {
50 1
        return isset($this->cast);
51
    }
52
53 18
    public function getCast()
54
    {
55 18
        return $this->cast;
56
    }
57
58 1
    public function ignore(...$values): self
59
    {
60 1
        $this->ignored = $this->ignored
61 1
            ->merge($values)
62 1
            ->flatten();
63
64 1
        return $this;
65
    }
66
67 27
    public function getIgnored(): Collection
68
    {
69 27
        return $this->ignored;
70
    }
71
72 1
    public function default($value): self
73
    {
74 1
        $this->default = $value;
75
76 1
        return $this;
77
    }
78
79 1
    public function getDefault()
80
    {
81 1
        return $this->default;
82
    }
83
84 1
    public function hasDefault(): bool
85
    {
86 1
        return isset($this->default);
87
    }
88
89 18
    protected function castValue($value)
90
    {
91 18
        switch ($this->getCast()) {
92
            case CastType::CAST_BOOLEAN:
93 2
                return filter_var($value, FILTER_VALIDATE_BOOLEAN);
94
            case CastType::CAST_INTEGER:
95 1
                return filter_var($value, FILTER_VALIDATE_INT);
96
            case CastType::CAST_ARRAY:
97 1
                return explode(',', $value);
98
            default:
99 15
                if (in_array(strtolower($value), ['true', 'false'], true)) {
100 1
                    $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
101
                }
102
103 15
                if (Str::contains($value, ',')) {
104 6
                    $value = explode(',', $value);
105
                }
106
107 15
                return $value;
108
        }//end switch
109
    }
110
111 27
    protected function resolveValueForFiltering($value)
112
    {
113 27
        if (is_string($value)) {
114 18
            $value = $this->castValue($value);
115
        }
116
117 27
        if (is_array($value)) {
118 12
            $remainingProperties = array_diff($value, $this->getIgnored()->toArray());
119
120 12
            return ! empty($remainingProperties) ? $remainingProperties : null;
121
        }
122
123 15
        return ! $this->getIgnored()->contains($value) ? $value : null;
124
    }
125
}
126