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