Passed
Push — master ( b9f979...4a2c07 )
by Zing
12:11 queued 06:54
created

Filter::delimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Str;
10
use Zing\QueryBuilder\Concerns\FilterCreator;
11
use Zing\QueryBuilder\Enums\CastType;
12
13
class Filter
14
{
15
    use FilterCreator;
16
17
    /**
18
     * @param mixed $value
19
     */
20 31
    public function filter(Builder $query, $value): Builder
21
    {
22 31
        $value = $this->resolveValueForFiltering($value);
23 31
        if ($value === null) {
24 1
            return $query;
25
        }
26
27 30
        if ($value === '') {
28 1
            return $query;
29
        }
30
31 29
        return $this->filter->apply($query, $value, $this->column);
32
    }
33
34 31
    public function getProperty(): string
35
    {
36 31
        return $this->property;
37
    }
38
39 1
    public function isForProperty(string $property): bool
40
    {
41 1
        return $this->property === $property;
42
    }
43
44
    /**
45
     * @return \Illuminate\Database\Query\Expression|string
46
     */
47 1
    public function getColumn()
48
    {
49 1
        return $this->column;
50
    }
51
52 7
    public function withCast(string $cast): self
53
    {
54 7
        $this->cast = $cast;
55
56 7
        return $this;
57
    }
58
59 1
    public function hasCast(): bool
60
    {
61 1
        return $this->cast !== null;
62
    }
63
64 22
    public function getCast(): ?string
65
    {
66 22
        return $this->cast;
67
    }
68
69
    /**
70
     * @param mixed[] $values
71
     */
72 1
    public function ignore(array $values): self
73
    {
74 1
        $this->ignored = $this->ignored
75 1
            ->merge($values)
76 1
            ->flatten();
77
78 1
        return $this;
79
    }
80
81 32
    public function getIgnored(): Collection
82
    {
83 32
        return $this->ignored;
84
    }
85
86
    /**
87
     * @param mixed $value
88
     */
89 1
    public function default($value): self
90
    {
91 1
        $this->default = $value;
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * @return mixed|null
98
     */
99 1
    public function getDefault()
100
    {
101 1
        return $this->default;
102
    }
103
104 1
    public function hasDefault(): bool
105
    {
106 1
        return $this->default !== null;
107
    }
108
109
    /**
110
     * @param mixed $value
111
     *
112
     * @return false|mixed|string[]
113
     */
114 22
    protected function castValue($value)
115
    {
116 22
        $cast = $this->getCast();
117 22
        if ($cast === CastType::STRING) {
118 1
            return $value;
119
        }
120
121 21
        if ($cast === CastType::BOOLEAN) {
122 2
            return filter_var($value, FILTER_VALIDATE_BOOLEAN);
123
        }
124
125 20
        if ($cast === CastType::INTEGER) {
126 1
            return filter_var($value, FILTER_VALIDATE_INT);
127
        }
128
129 19
        if ($cast === CastType::ARRAY) {
130 1
            return explode($this->delimiter, $value);
131
        }
132
133 18
        if (in_array(strtolower($value), ['true', 'false'], true)) {
134 1
            $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
135
        }
136
137 18
        if (Str::contains($value, $this->delimiter)) {
138 7
            $value = explode($this->delimiter, $value);
139
        }
140
141 18
        return $value;
142
    }
143
144
    /**
145
     * @param mixed $value
146
     *
147
     * @return mixed[]|mixed|null
148
     */
149 32
    protected function resolveValueForFiltering($value)
150
    {
151 32
        if (is_string($value)) {
152 22
            $value = $this->castValue($value);
153
        }
154
155 32
        if (is_array($value)) {
156 14
            $remainingProperties = array_diff($value, $this->getIgnored()->toArray());
157
158 14
            return empty($remainingProperties) ? null : $remainingProperties;
159
        }
160
161 19
        return $this->getIgnored()
162 19
            ->contains($value) ? null : $value;
163
    }
164
165
    /**
166
     * @phpstan-var non-empty-string
167
     *
168
     * @var string
169
     */
170
    private $delimiter = ',';
171
172
    /**
173
     * @param non-empty-string $delimiter
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
174
     */
175 1
    public function delimiter(string $delimiter): self
176
    {
177 1
        $this->delimiter = $delimiter;
178
179 1
        return $this;
180
    }
181
}
182