Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Builder #3

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/anything.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
declare(strict_types=1);

return [
// dex/anything
'search' => [
'operator' => env('ANYTHING_SEARCH_OPERATOR', 'ilike'),
],
];
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
</testsuite>
</testsuites>
<coverage/>
<php>
<server name="ANYTHING_SEARCH_OPERATOR" value="LIKE"/>
</php>
<source>
<include>
<directory suffix=".php">src</directory>
Expand Down
9 changes: 5 additions & 4 deletions src/Http/Controllers/AnythingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Anything> $result */
$result = Anything::query()
->whereSearch((string) $request->string('search'))
->get();

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/FetchAnythingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __invoke(string $type): Collection
{
/** @var Collection<int, Anything> $anything */
$anything = Anything::query()
->where('type', $type)
->whereType($type)
->get();

return $anything;
Expand Down
15 changes: 13 additions & 2 deletions src/Models/Anything.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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);
}
}
36 changes: 36 additions & 0 deletions src/Models/Builders/AnythingBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Dex\Laravel\Anything\Models\Builders;

use Dex\Laravel\Anything\Models\Anything;
use Illuminate\Database\Eloquent\Builder;

/**
* @extends Builder<Anything>
*/
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);
}
}
6 changes: 3 additions & 3 deletions src/Models/Concerns/HasAnythingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -31,7 +31,7 @@ public static function bootHasAnythingType(): void
return;
}

$builder->where('type', $type);
$builder->whereType($type);
});
}

Expand Down