From 0fe4e378e589f59f87d0f0f5ae8f225b031c3951 Mon Sep 17 00:00:00 2001 From: Edinei Valdameri Date: Tue, 4 Jun 2024 17:21:33 -0300 Subject: [PATCH] Add Builder in model Anything --- config/anything.php | 4 ++- phpunit.xml.dist | 3 ++ src/Http/Controllers/AnythingController.php | 9 ++--- .../Controllers/FetchAnythingController.php | 2 +- src/Models/Anything.php | 15 ++++++-- src/Models/Builders/AnythingBuilder.php | 36 +++++++++++++++++++ src/Models/Concerns/HasAnythingType.php | 6 ++-- 7 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src/Models/Builders/AnythingBuilder.php diff --git a/config/anything.php b/config/anything.php index b2bc0d5..b498ea0 100644 --- a/config/anything.php +++ b/config/anything.php @@ -3,5 +3,7 @@ declare(strict_types=1); return [ - // dex/anything + 'search' => [ + 'operator' => env('ANYTHING_SEARCH_OPERATOR', 'ilike'), + ], ]; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5a7c298..58de44c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,9 @@ + + + src diff --git a/src/Http/Controllers/AnythingController.php b/src/Http/Controllers/AnythingController.php index 194747d..8dbfbbe 100644 --- a/src/Http/Controllers/AnythingController.php +++ b/src/Http/Controllers/AnythingController.php @@ -15,10 +15,11 @@ class AnythingController */ public function __invoke(Request $request): Collection { - return Anything::query() - ->when($search = $request->query('search'), function ($query) use ($search) { - $query->where('slug', 'like', "%{$search}%"); - }) + /** @var Collection $result */ + $result = Anything::query() + ->whereSearch((string) $request->string('search')) ->get(); + + return $result; } } diff --git a/src/Http/Controllers/FetchAnythingController.php b/src/Http/Controllers/FetchAnythingController.php index fb4c453..04073db 100644 --- a/src/Http/Controllers/FetchAnythingController.php +++ b/src/Http/Controllers/FetchAnythingController.php @@ -16,7 +16,7 @@ public function __invoke(string $type): Collection { /** @var Collection $anything */ $anything = Anything::query() - ->where('type', $type) + ->whereType($type) ->get(); return $anything; diff --git a/src/Models/Anything.php b/src/Models/Anything.php index d937581..6b743ef 100644 --- a/src/Models/Anything.php +++ b/src/Models/Anything.php @@ -4,9 +4,11 @@ namespace Dex\Laravel\Anything\Models; +use Dex\Laravel\Anything\Models\Builders\AnythingBuilder; use Dex\Laravel\Anything\Models\Concerns\AnythingMorphed; use Dex\Laravel\Anything\Models\Concerns\HasAnythingType; use Dex\Laravel\Anything\Models\Concerns\HasSlug; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -15,6 +17,10 @@ * @property string $type * @property string $slug * @property string $label + * + * @method static Builder whereType(string $type) + * @method static Builder whereSlug(string $slug) + * @method static AnythingBuilder query() */ class Anything extends Model { @@ -33,10 +39,15 @@ public static function get(string $slug, ?string $type = null): static /** @var static $model */ $model = static::query() - ->where('type', $type) - ->where('slug', $slug) + ->whereType($type) + ->whereSlug($slug) ->first(); return $model; } + + public function newEloquentBuilder($query): AnythingBuilder + { + return new AnythingBuilder($query); + } } diff --git a/src/Models/Builders/AnythingBuilder.php b/src/Models/Builders/AnythingBuilder.php new file mode 100644 index 0000000..6c33f9c --- /dev/null +++ b/src/Models/Builders/AnythingBuilder.php @@ -0,0 +1,36 @@ + + */ +class AnythingBuilder extends Builder +{ + public function whereSearch(string $search): self + { + $likeOperator = ($temp = config('anything.search.operator')) && is_string($temp) ? $temp : null; + + return $this->when($search, function ($q) use ($search, $likeOperator) { + $q->whereAny([ + 'label', + 'slug', + ], $likeOperator, '%' . $search . '%'); + }); + } + + public function whereType(string $type): self + { + return $this->where('type', $type); + } + + public function whereSlug(string $slug): self + { + return $this->where('slug', $slug); + } +} diff --git a/src/Models/Concerns/HasAnythingType.php b/src/Models/Concerns/HasAnythingType.php index 999a5b1..365bc51 100644 --- a/src/Models/Concerns/HasAnythingType.php +++ b/src/Models/Concerns/HasAnythingType.php @@ -5,7 +5,7 @@ namespace Dex\Laravel\Anything\Models\Concerns; use Dex\Laravel\Anything\Models\Anything; -use Illuminate\Database\Eloquent\Builder; +use Dex\Laravel\Anything\Models\Builders\AnythingBuilder; /** * @mixin Anything @@ -20,7 +20,7 @@ public static function bootHasAnythingType(): void } }); - static::addGlobalScope(function (Builder $builder) { + static::addGlobalScope(function (AnythingBuilder $builder) { /** @var Anything $model */ $model = $builder->getModel(); @@ -31,7 +31,7 @@ public static function bootHasAnythingType(): void return; } - $builder->where('type', $type); + $builder->whereType($type); }); }