59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Features\Games\Data;
|
|
|
|
use App\Features\Consoles\Data\ConsoleData;
|
|
use App\Features\Games\Models\Game;
|
|
use Spatie\LaravelData\Attributes\MapInputName;
|
|
use Spatie\LaravelData\Data;
|
|
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
|
|
|
#[MapInputName(SnakeCaseMapper::class)]
|
|
class GameData extends Data
|
|
{
|
|
public function __construct(
|
|
public int $id,
|
|
public int $consoleId,
|
|
public string $title,
|
|
public string $slug,
|
|
public ?int $igdbId,
|
|
public ?int $tgdbId,
|
|
public ?string $description,
|
|
public ?int $releaseYear,
|
|
public ?string $developer,
|
|
public ?string $publisher,
|
|
public ?string $genre,
|
|
public ?float $rating,
|
|
public ?string $coverPath,
|
|
public ?string $thumbnailPath,
|
|
public ?ConsoleData $console,
|
|
public ?array $romFiles,
|
|
) {}
|
|
|
|
public static function fromModel(Game $game): self
|
|
{
|
|
return new self(
|
|
id: $game->id,
|
|
consoleId: $game->console_id,
|
|
title: $game->title,
|
|
slug: $game->slug,
|
|
igdbId: $game->igdb_id,
|
|
tgdbId: $game->tgdb_id,
|
|
description: $game->description,
|
|
releaseYear: $game->release_year,
|
|
developer: $game->developer,
|
|
publisher: $game->publisher,
|
|
genre: $game->genre,
|
|
rating: $game->rating ? (float) $game->rating : null,
|
|
coverPath: $game->cover_path,
|
|
thumbnailPath: $game->thumbnail_path,
|
|
console: $game->relationLoaded('console') && $game->console
|
|
? ConsoleData::fromModel($game->console)
|
|
: null,
|
|
romFiles: $game->relationLoaded('romFiles')
|
|
? $game->romFiles->map(fn ($rom) => RomFileData::fromModel($rom))->toArray()
|
|
: null,
|
|
);
|
|
}
|
|
}
|