ldc-cocotte/app/Dto/DiscountProductDto.php

52 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\Product;
use Illuminate\Contracts\Support\Arrayable;
/**
* @implements Arrayable<string, string>
*/
final readonly class DiscountProductDto implements \JsonSerializable, Arrayable
{
public function __construct(
public string $id,
public string $name,
public ?string $picture,
) {
}
public static function fromModel(Product $product): self
{
return new self(
id: (string) $product->id,
name: (string) $product->name,
picture: publicS3Asset($product->picture),
);
}
/**
* @return array{id: string, name: string, picture: ?string}
*/
#[\Override]
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'picture' => $this->picture,
];
}
/**
* @return array{id: string, name: string, picture: ?string}
*/
#[\Override]
public function jsonSerialize(): array
{
return $this->toArray();
}
}