ldc-cocotte/app/Dto/DiscountProductDto.php

49 lines
945 B
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 static function fromModel(Product $product): self
{
return new self(
id: (string) $product->id,
name: (string) $product->name,
);
}
/**
* @return array{id: string, name: string}
*/
#[\Override]
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
/**
* @return array{id: string, name: string}
*/
#[\Override]
public function jsonSerialize(): array
{
return $this->toArray();
}
}