mirror of
https://github.com/koel/koel
synced 2024-11-24 13:13:05 +00:00
48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Repositories\AlbumRepository;
|
|
use App\Repositories\ArtistRepository;
|
|
use App\Repositories\SongRepository;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Laravel\Scout\Builder;
|
|
|
|
class SearchService
|
|
{
|
|
private $songRepository;
|
|
private $albumRepository;
|
|
private $artistRepository;
|
|
|
|
public function __construct(
|
|
SongRepository $songRepository,
|
|
AlbumRepository $albumRepository,
|
|
ArtistRepository $artistRepository
|
|
) {
|
|
$this->songRepository = $songRepository;
|
|
$this->albumRepository = $albumRepository;
|
|
$this->artistRepository = $artistRepository;
|
|
}
|
|
|
|
/** @return array<mixed> */
|
|
public function excerptSearch(string $keywords): array
|
|
{
|
|
return [
|
|
'songs' => self::getTopResults($this->songRepository->search($keywords)),
|
|
'artists' => self::getTopResults($this->artistRepository->search($keywords)),
|
|
'albums' => self::getTopResults($this->albumRepository->search($keywords)),
|
|
];
|
|
}
|
|
|
|
/** @return Collection|array<Model> */
|
|
private static function getTopResults(Builder $query, int $count = 6): Collection
|
|
{
|
|
return $query->take($count)->get();
|
|
}
|
|
|
|
public function searchSongs(string $keywords): Builder
|
|
{
|
|
return $this->songRepository->search($keywords);
|
|
}
|
|
}
|