*/ final readonly class ProductDto implements Arrayable, UrlRoutable { /** * @param list $tags */ public function __construct( public string $id, public string $name, public string $slug, public ?ProductBrand $brand = null, public ?ProductIncoDto $inco = null, public ?string $picture = null, public ?string $picture_alt = null, public ?string $picture_title = null, public ?string $picture_large = null, public ?string $picture_large_alt = null, public ?string $picture_large_title = null, public ?Money $price = null, public ?DiscountDto $discount = null, public ?TaxRate $tax_rate = null, public ?InvoicingUnit $invoicing_unit = null, public int $stock = 0, public ?SellUnit $sell_unit = null, public ?OrderUnit $order_unit = null, public ?RangeDto $range = null, public array $tags = [], public int $order = 0, public bool $status = false, public bool $star = false, public ?ProductWeight $product_weight = null, public bool $weight_variable = false, public bool $is_orderable = false, public ?Carbon $orderable_start = null, public ?Carbon $orderable_end = null, public ?string $short_description = null, public ?string $description = null, ) { } public static function fromModel( Product $product, bool $isOrderable, bool $validDiscountOnly = false, ): self { $discountModel = $validDiscountOnly ? ($product->relationLoaded('validDiscount') ? $product->validDiscount : null) : ($product->relationLoaded('discount') ? $product->discount : null); /** @var list $tags */ $tags = $product->relationLoaded('tags') ? array_values($product->tags->map(static fn (Tag $tag): TagDto => TagDto::fromModel($tag))->all()) : []; return new self( id: (string) $product->id, name: $product->name, slug: (string) $product->slug, brand: $product->brand, inco: $product->relationLoaded('inco') && $product->inco ? ProductIncoDto::fromModel($product->inco) : null, picture: publicS3Asset($product->picture), picture_alt: $product->picture_alt, picture_title: $product->picture_title, picture_large: publicS3Asset($product->picture_large), picture_large_alt: $product->picture_large_alt, picture_large_title: $product->picture_large_title, price: $product->price instanceof Money ? $product->price : null, discount: $discountModel ? DiscountDto::fromModel($discountModel) : null, tax_rate: $product->tax_rate, invoicing_unit: $product->invoicing_unit, stock: (int) $product->stock, sell_unit: $product->sell_unit, order_unit: $product->order_unit, range: $product->relationLoaded('range') && $product->range ? RangeDto::fromModel($product->range) : null, tags: $tags, order: (int) $product->order, status: (bool) $product->status, star: (bool) $product->star, product_weight: $product->product_weight, weight_variable: (bool) $product->weight_variable, is_orderable: $isOrderable, orderable_start: $product->orderable_start, orderable_end: $product->orderable_end, short_description: $product->short_description, description: $product->description, ); } #[\Override] public function getRouteKey(): mixed { return $this->slug; } #[\Override] public function getRouteKeyName(): string { return 'slug'; } #[\Override] public function resolveRouteBinding($value, $field = null): mixed { return null; } #[\Override] public function resolveChildRouteBinding($childType, $value, $field): mixed { return null; } /** * @return array */ #[\Override] public function toArray(): array { return [ 'id' => $this->id, 'name' => $this->name, 'slug' => $this->slug, 'brand' => $this->brand?->value, 'inco' => $this->inco?->toArray(), 'picture' => $this->picture, 'picture_alt' => $this->picture_alt, 'picture_title' => $this->picture_title, 'picture_large' => $this->picture_large, 'picture_large_alt' => $this->picture_large_alt, 'picture_large_title' => $this->picture_large_title, ...$this->pricingAndUnitsToArray(), 'range' => $this->range?->toArray(), 'tags' => array_map(static fn (TagDto $tag): array => $tag->toArray(), $this->tags), 'order' => $this->order, 'product_weight' => $this->product_weight?->value, 'weight_variable' => $this->weight_variable, 'status' => $this->status, 'star' => $this->star, 'orderable_start' => $this->orderable_start, 'orderable_end' => $this->orderable_end, 'short_description' => $this->short_description, 'description' => $this->description, ]; } /** * @return array{ * price: string|null, * discount: array|null, * tax_rate: int|null, * invoicing_unit: string|null, * stock: int, * sell_unit: string|null, * order_unit: string|null * } */ private function pricingAndUnitsToArray(): array { return [ 'price' => $this->price?->getAmount(), 'discount' => $this->discount?->toArray(), 'tax_rate' => $this->tax_rate?->value, 'invoicing_unit' => $this->invoicing_unit?->value, 'stock' => $this->stock, 'sell_unit' => $this->sell_unit?->value, 'order_unit' => $this->order_unit?->value, ]; } }