46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Vision;
|
|
|
|
use App\Models\Artwork;
|
|
use App\Models\Category;
|
|
|
|
final class ArtworkVectorMetadataService
|
|
{
|
|
/**
|
|
* @return array{content_type: string, category: string, user_id: string, tags: list<string>}
|
|
*/
|
|
public function forArtwork(Artwork $artwork): array
|
|
{
|
|
$artwork->loadMissing([
|
|
'categories' => fn ($categories) => $categories->with('contentType')->orderBy('sort_order')->orderBy('name'),
|
|
'tags:id,slug',
|
|
]);
|
|
|
|
$category = $this->primaryCategory($artwork);
|
|
|
|
return [
|
|
'content_type' => (string) ($category?->contentType?->name ?? ''),
|
|
'category' => (string) ($category?->name ?? ''),
|
|
'user_id' => (string) ($artwork->user_id ?? ''),
|
|
'tags' => $artwork->tags
|
|
->pluck('slug')
|
|
->map(static fn (mixed $slug): string => trim((string) $slug))
|
|
->filter(static fn (string $slug): bool => $slug !== '')
|
|
->unique()
|
|
->values()
|
|
->all(),
|
|
];
|
|
}
|
|
|
|
private function primaryCategory(Artwork $artwork): ?Category
|
|
{
|
|
/** @var Category|null $category */
|
|
$category = $artwork->categories->sortBy('sort_order')->first();
|
|
|
|
return $category;
|
|
}
|
|
}
|