|
6 | 6 | use Illuminate\Database\Eloquent\Casts\Attribute as AttributeCast; |
7 | 7 | use Illuminate\Database\Eloquent\Relations\HasMany; |
8 | 8 | use Illuminate\Support\Arr; |
| 9 | +use InvalidArgumentException; |
9 | 10 | use Rapidez\Core\Models\Attribute; |
10 | 11 | use Rapidez\Core\Models\AttributeDatetime; |
11 | 12 | use Rapidez\Core\Models\AttributeDecimal; |
@@ -76,27 +77,35 @@ public function scopeAttributeHas(Builder $builder, string $attributeCode, calla |
76 | 77 | ); |
77 | 78 | } |
78 | 79 |
|
79 | | - public function scopeWhereAttribute(Builder $builder, string $attributeCode, $operator = null, $value = null) |
| 80 | + public function scopeWhereMethodAttribute(Builder $builder, string $method, string $attributeCode, ...$args) |
80 | 81 | { |
81 | | - return $builder->attributeHas( |
| 82 | + if (! method_exists(\Illuminate\Database\Query\Builder::class, $method)) { |
| 83 | + throw new InvalidArgumentException('Method is not a valid method'); |
| 84 | + } |
| 85 | + $isOrMethod = str_starts_with($method, 'or'); |
| 86 | + |
| 87 | + if ($isOrMethod) { |
| 88 | + $method = lcfirst(substr($method, 2)); |
| 89 | + } |
| 90 | + |
| 91 | + $q = fn ($builder) => $builder->attributeHas( |
82 | 92 | $attributeCode, |
83 | 93 | fn ($query) => $query |
84 | | - ->where('value', $operator, $value) |
| 94 | + ->$method('value', ...$args) |
85 | 95 | ->where($this->getCustomAttributeCode(), $attributeCode) |
86 | 96 | ); |
| 97 | + |
| 98 | + return $isOrMethod ? $builder->orWhere($q) : $q($builder); |
| 99 | + } |
| 100 | + |
| 101 | + public function scopeWhereAttribute(Builder $builder, string $attributeCode, $operator = null, $value = null) |
| 102 | + { |
| 103 | + return $this->whereMethodAttribute('where', $attributeCode, $operator, $value); |
87 | 104 | } |
88 | 105 |
|
89 | | - // TODO: This one is a bit a duplicate of the one above, |
90 | | - // can we re-use some code? What if another method |
91 | | - // is needed like whereBetween, whereNull, etc |
92 | 106 | public function scopeWhereInAttribute(Builder $builder, string $attributeCode, $values = null, $boolean = 'and', $not = false) |
93 | 107 | { |
94 | | - return $builder->attributeHas( |
95 | | - $attributeCode, |
96 | | - fn ($query) => $query |
97 | | - ->whereIn('value', $values, $boolean, $not) |
98 | | - ->where($this->getCustomAttributeCode(), $attributeCode) |
99 | | - ); |
| 108 | + return $this->whereMethodAttribute('whereIn', $attributeCode, $values, $boolean, $not); |
100 | 109 | } |
101 | 110 |
|
102 | 111 | public function attributeDatetime(): HasMany |
@@ -179,10 +188,6 @@ public function customAttributes(): AttributeCast |
179 | 188 | if ($values = $this->$relation) { |
180 | 189 | $data->push(...$values); |
181 | 190 | } |
182 | | - |
183 | | - // TODO: Double check if this is actually useful |
184 | | - // it could improve performance / reduce memory |
185 | | - // $this->unsetRelation($relation); |
186 | 191 | } |
187 | 192 |
|
188 | 193 | return $data->keyBy($this->getCustomAttributeCode()); |
|
0 commit comments