Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit b9c4a53

Browse files
Merge pull request #7 from DarkGhostHunter/master
Enhanced `whereConfig()`
2 parents 4596ec1 + 7774ed7 commit b9c4a53

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

src/Eloquent/Scopes/WhereConfig.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,72 @@ public function apply(Builder $builder, Model $model): void
3131
public function extend(Builder $builder): void
3232
{
3333
$builder->macro('whereConfig', [static::class, 'whereConfig']);
34+
$builder->macro('orWhereConfig', [static::class, 'orWhereConfig']);
3435
}
3536

3637
/**
3738
* Filters the user by the config value.
3839
*
3940
* @param \Illuminate\Database\Eloquent\Builder $builder
4041
* @param string|array $name
41-
* @param mixed|null $value
42+
* @param string|null $operator
43+
* @param null $value
44+
* @param string $boolean
4245
*
4346
* @return \Illuminate\Database\Eloquent\Builder
4447
*/
45-
public static function whereConfig(Builder $builder, string|array $name, mixed $value = null): Builder
46-
{
48+
public static function whereConfig(
49+
Builder $builder,
50+
string|array $name,
51+
string $operator = null,
52+
$value = null,
53+
string $boolean = 'and'
54+
): Builder {
4755
if (is_array($name)) {
4856
foreach ($name as $key => $item) {
49-
static::whereConfig($builder, $key, $item);
57+
if (is_array($item)) {
58+
static::whereConfig($builder, ...$item);
59+
} else {
60+
static::whereConfig($builder, $key, $item);
61+
}
5062
}
5163

5264
return $builder;
5365
}
5466

55-
return $builder->whereHas('settings', static function (Builder $builder) use ($name, $value): void {
56-
$builder
57-
->withoutGlobalScope(AddMetadata::class)
58-
->where(
59-
static function (Builder $builder) use ($name, $value): void {
60-
$builder
61-
->where('value', $value)
62-
->whereHas('metadata', static function (Builder $builder) use ($name): void {
63-
$builder->where('name', $name);
64-
});
67+
return $builder->has(
68+
relation: 'settings',
69+
boolean: $boolean,
70+
callback: static function (Builder $builder) use ($name, $operator, $value): void {
71+
$builder
72+
->withoutGlobalScope(AddMetadata::class)
73+
->where(
74+
static function (Builder $builder) use ($name, $operator, $value): void {
75+
$builder
76+
->where('value', $operator, $value)
77+
->whereHas('metadata', static function (Builder $builder) use ($name): void {
78+
$builder->where('name', $name);
79+
});
80+
});
6581
});
66-
});
82+
}
83+
84+
/**
85+
* Filters the user by the config value.
86+
*
87+
* @param \Illuminate\Database\Eloquent\Builder $builder
88+
* @param string|array $name
89+
* @param string|null $operator
90+
* @param null $value
91+
*
92+
* @return \Illuminate\Database\Eloquent\Builder
93+
*/
94+
public static function orWhereConfig(
95+
Builder $builder,
96+
string|array $name,
97+
string $operator = null,
98+
$value = null,
99+
): Builder {
100+
return static::whereConfig($builder, $name, $operator, $value, 'or');
67101
}
68102
}

src/HasConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace DarkGhostHunter\Laraconfig;
44

5-
use Illuminate\Database\Eloquent\Builder;
65
use Illuminate\Database\Eloquent\Model;
76

87
use function method_exists;
98

109
/**
1110
* @property-read \DarkGhostHunter\Laraconfig\SettingsCollection<\DarkGhostHunter\Laraconfig\Eloquent\Setting>|\DarkGhostHunter\Laraconfig\Eloquent\Setting[] $settings
1211
*
13-
* @method Builder|static whereConfig(string|array $name, mixed $value = null)
12+
* @method \Illuminate\Database\Eloquent\Builder|static whereConfig(string|array $name, string $operator = null, $value = null, string $boolean = 'and')
13+
* @method \Illuminate\Database\Eloquent\Builder|static orWhereConfig(string|array $name, string $operator = null, $value = null)
1414
*/
1515
trait HasConfig
1616
{

tests/Eloquent/Scopes/FilterBySettingTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,31 @@ public function filterBags()
161161

162162
static::assertCount(1, $users);
163163
}
164+
165+
public function test_accepts_where_with_operator_and_or(): void
166+
{
167+
$user = DummyModel::make()->forceFill([
168+
'name' => 'john',
169+
'email' => 'john@email.com',
170+
'password' => '123456',
171+
]);
172+
173+
$user->saveQuietly();
174+
175+
$this->createSettingsForUser($user);
176+
177+
Metadata::query()->whereKey(1)->update([
178+
'type' => Metadata::TYPE_INTEGER,
179+
]);
180+
181+
Setting::query()->whereKey(1)->update([
182+
'value' => 2
183+
]);
184+
185+
static::assertCount(1, DummyModel::whereConfig('foo', '>', 1)->get());
186+
static::assertCount(0, DummyModel::whereConfig('foo', '<', 1)->get());
187+
188+
static::assertCount(1, DummyModel::whereConfig('foo', '>', 1)->orWhereConfig('foo', '<', 0)->get());
189+
static::assertCount(0, DummyModel::whereConfig('foo', '>', 2)->orWhereConfig('foo', '<', 0)->get());
190+
}
164191
}

0 commit comments

Comments
 (0)