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

Commit

Permalink
Merge pull request #7 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Enhanced `whereConfig()`
  • Loading branch information
DarkGhostHunter authored Jul 20, 2021
2 parents 4596ec1 + 7774ed7 commit b9c4a53
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
64 changes: 49 additions & 15 deletions src/Eloquent/Scopes/WhereConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,72 @@ public function apply(Builder $builder, Model $model): void
public function extend(Builder $builder): void
{
$builder->macro('whereConfig', [static::class, 'whereConfig']);
$builder->macro('orWhereConfig', [static::class, 'orWhereConfig']);
}

/**
* Filters the user by the config value.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param string|array $name
* @param mixed|null $value
* @param string|null $operator
* @param null $value
* @param string $boolean
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function whereConfig(Builder $builder, string|array $name, mixed $value = null): Builder
{
public static function whereConfig(
Builder $builder,
string|array $name,
string $operator = null,
$value = null,
string $boolean = 'and'
): Builder {
if (is_array($name)) {
foreach ($name as $key => $item) {
static::whereConfig($builder, $key, $item);
if (is_array($item)) {
static::whereConfig($builder, ...$item);
} else {
static::whereConfig($builder, $key, $item);
}
}

return $builder;
}

return $builder->whereHas('settings', static function (Builder $builder) use ($name, $value): void {
$builder
->withoutGlobalScope(AddMetadata::class)
->where(
static function (Builder $builder) use ($name, $value): void {
$builder
->where('value', $value)
->whereHas('metadata', static function (Builder $builder) use ($name): void {
$builder->where('name', $name);
});
return $builder->has(
relation: 'settings',
boolean: $boolean,
callback: static function (Builder $builder) use ($name, $operator, $value): void {
$builder
->withoutGlobalScope(AddMetadata::class)
->where(
static function (Builder $builder) use ($name, $operator, $value): void {
$builder
->where('value', $operator, $value)
->whereHas('metadata', static function (Builder $builder) use ($name): void {
$builder->where('name', $name);
});
});
});
});
}

/**
* Filters the user by the config value.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param string|array $name
* @param string|null $operator
* @param null $value
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function orWhereConfig(
Builder $builder,
string|array $name,
string $operator = null,
$value = null,
): Builder {
return static::whereConfig($builder, $name, $operator, $value, 'or');
}
}
4 changes: 2 additions & 2 deletions src/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace DarkGhostHunter\Laraconfig;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

use function method_exists;

/**
* @property-read \DarkGhostHunter\Laraconfig\SettingsCollection<\DarkGhostHunter\Laraconfig\Eloquent\Setting>|\DarkGhostHunter\Laraconfig\Eloquent\Setting[] $settings
*
* @method Builder|static whereConfig(string|array $name, mixed $value = null)
* @method \Illuminate\Database\Eloquent\Builder|static whereConfig(string|array $name, string $operator = null, $value = null, string $boolean = 'and')
* @method \Illuminate\Database\Eloquent\Builder|static orWhereConfig(string|array $name, string $operator = null, $value = null)
*/
trait HasConfig
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Eloquent/Scopes/FilterBySettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,31 @@ public function filterBags()

static::assertCount(1, $users);
}

public function test_accepts_where_with_operator_and_or(): void
{
$user = DummyModel::make()->forceFill([
'name' => 'john',
'email' => '[email protected]',
'password' => '123456',
]);

$user->saveQuietly();

$this->createSettingsForUser($user);

Metadata::query()->whereKey(1)->update([
'type' => Metadata::TYPE_INTEGER,
]);

Setting::query()->whereKey(1)->update([
'value' => 2
]);

static::assertCount(1, DummyModel::whereConfig('foo', '>', 1)->get());
static::assertCount(0, DummyModel::whereConfig('foo', '<', 1)->get());

static::assertCount(1, DummyModel::whereConfig('foo', '>', 1)->orWhereConfig('foo', '<', 0)->get());
static::assertCount(0, DummyModel::whereConfig('foo', '>', 2)->orWhereConfig('foo', '<', 0)->get());
}
}

0 comments on commit b9c4a53

Please sign in to comment.