-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,937 additions
and
475 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Str; | ||
use Laravel\Scout\Searchable as ScoutSearchable; | ||
|
||
trait Searchable | ||
{ | ||
use ScoutSearchable { | ||
search as scoutSearch; | ||
} | ||
|
||
/** | ||
* Get the index name for the model. | ||
* | ||
* @return string | ||
*/ | ||
public function searchableAs(): string | ||
{ | ||
return config('scout.prefix') . app()->getLocale() . '_' . $this->getTable(); | ||
} | ||
|
||
/** | ||
* Perform a search against the model's indexed data. | ||
* | ||
* @param string $query | ||
* @param \Closure $callback | ||
* @return \Laravel\Scout\Builder | ||
*/ | ||
public static function search($query = '', $callback = null) | ||
{ | ||
$query = Str::of($query) | ||
->lower() | ||
->ascii() | ||
->value(); | ||
|
||
return self::scoutSearch($query, $callback); | ||
} | ||
|
||
public static function searchAndFilter(?string $terms, ?array $filters) | ||
{ | ||
if (filled($terms)) { | ||
return self::search($terms) | ||
->query(fn ($query) => $query->filter($filters)); | ||
} | ||
|
||
return self::query() | ||
->filter($filters); | ||
} | ||
|
||
public function scopeFilter(Builder $query, ?array $filters): Builder | ||
{ | ||
return $query->filterQuery( | ||
collect($filters) | ||
->filter() | ||
->all() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Str; | ||
use Spatie\Translatable\HasTranslations; | ||
|
||
trait Translatable | ||
{ | ||
use HasTranslations; | ||
|
||
public function scopeWhereTranslatable(Builder $query, string $column, string $value, string $boolean = 'and'): Builder | ||
{ | ||
$locale = app()->getLocale(); | ||
$value = Str::lower($value); | ||
|
||
if (Str::contains($column, '.')) { | ||
$clause = $boolean === 'and' ? 'whereHas' : 'orWhereHas'; | ||
|
||
return $query->{$clause}($column, function ($query) use ($column, $value, $locale, $boolean) { | ||
$query->whereRaw("LOWER({$column}->\"$.{$locale}\") LIKE ?", ["%{$value}%"], $boolean); | ||
}); | ||
} | ||
|
||
return $query->whereRaw("LOWER({$column}->\"$.{$locale}\") LIKE ?", ["%{$value}%"], $boolean); | ||
} | ||
|
||
public function scopeOrWhereTranslatable(Builder $query, string $column, string $value): Builder | ||
{ | ||
return $query->whereTranslatable($column, $value, 'or'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Ngo; | ||
use App\Models\Service; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Traits\Localizable; | ||
|
||
class RebuildSearchIndex extends Command | ||
{ | ||
use Localizable; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'search:rebuild'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Rebuilds the search index for all models'; | ||
|
||
protected $models = [ | ||
Ngo::class, | ||
Service::class, | ||
]; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
app('languages') | ||
->keys() | ||
->crossJoin($this->models) | ||
->each(function ($args) { | ||
[$locale, $model] = $args; | ||
|
||
$this->withLocale($locale, function () use ($model) { | ||
$this->call('scout:flush', ['model' => $model]); | ||
$this->call('scout:import', ['model' => $model]); | ||
}); | ||
}); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Helpers; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
|
||
class Normalize | ||
{ | ||
/** | ||
* Strip tags and normalize the string to lowercase ascii. | ||
* | ||
* @param null|string $string | ||
* @return string | ||
*/ | ||
public static function string(?string $string): string | ||
{ | ||
$string ??= ''; | ||
|
||
return Str::of(html_entity_decode($string)) | ||
->stripTags() | ||
->lower() | ||
->ascii() | ||
->value(); | ||
} | ||
|
||
public static function collection(Collection $input): Collection | ||
{ | ||
return $input->map(fn (?string $value) => self::string($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.