2026-09-30 16:27:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Dto;
|
|
|
|
|
|
|
|
|
|
use App\Models\Product;
|
2026-11-14 16:25:59 +01:00
|
|
|
use App\Models\Tag;
|
2026-09-30 17:22:41 +02:00
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\SellUnit;
|
2026-11-14 16:25:59 +01:00
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
2026-11-14 16:06:21 +01:00
|
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
2026-09-30 16:27:40 +02:00
|
|
|
use Illuminate\Contracts\Routing\UrlRoutable;
|
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2026-11-14 16:25:59 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2026-09-30 17:22:41 +02:00
|
|
|
use Money\Money;
|
2026-09-30 16:27:40 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @implements Arrayable<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
final readonly class ProductDto implements Arrayable, UrlRoutable
|
|
|
|
|
{
|
2026-11-14 16:25:59 +01:00
|
|
|
/**
|
|
|
|
|
* @param list<TagDto> $tags
|
|
|
|
|
*/
|
2026-09-30 16:27:40 +02:00
|
|
|
public function __construct(
|
|
|
|
|
public string $id,
|
|
|
|
|
public string $name,
|
|
|
|
|
public string $slug,
|
2026-09-30 17:22:41 +02:00
|
|
|
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,
|
2026-11-14 16:25:59 +01:00
|
|
|
public ?TaxRate $tax_rate = null,
|
|
|
|
|
public ?InvoicingUnit $invoicing_unit = null,
|
2026-09-30 17:22:41 +02:00
|
|
|
public int $stock = 0,
|
|
|
|
|
public ?SellUnit $sell_unit = null,
|
2026-11-14 16:25:59 +01:00
|
|
|
public ?OrderUnit $order_unit = null,
|
2026-09-30 17:22:41 +02:00
|
|
|
public ?RangeDto $range = null,
|
2026-11-14 16:25:59 +01:00
|
|
|
public array $tags = [],
|
2026-09-30 17:22:41 +02:00
|
|
|
public int $order = 0,
|
|
|
|
|
public bool $status = false,
|
|
|
|
|
public bool $star = false,
|
2026-11-14 16:06:21 +01:00
|
|
|
public ?ProductWeight $product_weight = null,
|
|
|
|
|
public bool $weight_variable = false,
|
|
|
|
|
public bool $is_orderable = false,
|
2026-11-14 16:25:59 +01:00
|
|
|
public ?Carbon $orderable_start = null,
|
|
|
|
|
public ?Carbon $orderable_end = null,
|
2026-09-30 17:22:41 +02:00
|
|
|
public ?string $short_description = null,
|
|
|
|
|
public ?string $description = null,
|
2026-09-30 16:27:40 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 16:06:21 +01:00
|
|
|
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);
|
|
|
|
|
|
2026-11-14 16:25:59 +01:00
|
|
|
/** @var list<TagDto> $tags */
|
|
|
|
|
$tags = $product->relationLoaded('tags')
|
|
|
|
|
? array_values($product->tags->map(static fn (Tag $tag): TagDto => TagDto::fromModel($tag))->all())
|
|
|
|
|
: [];
|
|
|
|
|
|
2026-09-30 16:27:40 +02:00
|
|
|
return new self(
|
|
|
|
|
id: (string) $product->id,
|
|
|
|
|
name: $product->name,
|
|
|
|
|
slug: (string) $product->slug,
|
2026-09-30 17:22:41 +02:00
|
|
|
brand: $product->brand,
|
|
|
|
|
inco: $product->relationLoaded('inco') && $product->inco ? ProductIncoDto::fromModel($product->inco) : null,
|
2026-11-14 16:06:21 +01:00
|
|
|
picture: publicS3Asset($product->picture),
|
2026-09-30 17:22:41 +02:00
|
|
|
picture_alt: $product->picture_alt,
|
|
|
|
|
picture_title: $product->picture_title,
|
2026-11-14 16:06:21 +01:00
|
|
|
picture_large: publicS3Asset($product->picture_large),
|
2026-09-30 17:22:41 +02:00
|
|
|
picture_large_alt: $product->picture_large_alt,
|
|
|
|
|
picture_large_title: $product->picture_large_title,
|
|
|
|
|
price: $product->price instanceof Money ? $product->price : null,
|
2026-11-14 16:06:21 +01:00
|
|
|
discount: $discountModel ? DiscountDto::fromModel($discountModel) : null,
|
2026-11-14 16:25:59 +01:00
|
|
|
tax_rate: $product->tax_rate,
|
|
|
|
|
invoicing_unit: $product->invoicing_unit,
|
2026-09-30 17:22:41 +02:00
|
|
|
stock: (int) $product->stock,
|
|
|
|
|
sell_unit: $product->sell_unit,
|
2026-11-14 16:25:59 +01:00
|
|
|
order_unit: $product->order_unit,
|
2026-09-30 17:22:41 +02:00
|
|
|
range: $product->relationLoaded('range') && $product->range ? RangeDto::fromModel($product->range) : null,
|
2026-11-14 16:25:59 +01:00
|
|
|
tags: $tags,
|
2026-09-30 17:22:41 +02:00
|
|
|
order: (int) $product->order,
|
|
|
|
|
status: (bool) $product->status,
|
|
|
|
|
star: (bool) $product->star,
|
2026-11-14 16:06:21 +01:00
|
|
|
product_weight: $product->product_weight,
|
|
|
|
|
weight_variable: (bool) $product->weight_variable,
|
|
|
|
|
is_orderable: $isOrderable,
|
2026-11-14 16:25:59 +01:00
|
|
|
orderable_start: $product->orderable_start,
|
|
|
|
|
orderable_end: $product->orderable_end,
|
2026-09-30 17:22:41 +02:00
|
|
|
short_description: $product->short_description,
|
|
|
|
|
description: $product->description,
|
2026-09-30 16:27:40 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-30 17:22:41 +02:00
|
|
|
* @return array<string, mixed>
|
2026-09-30 16:27:40 +02:00
|
|
|
*/
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2026-09-30 17:22:41 +02:00
|
|
|
'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,
|
2026-11-14 16:25:59 +01:00
|
|
|
...$this->pricingAndUnitsToArray(),
|
2026-09-30 17:22:41 +02:00
|
|
|
'range' => $this->range?->toArray(),
|
2026-11-14 16:25:59 +01:00
|
|
|
'tags' => array_map(static fn (TagDto $tag): array => $tag->toArray(), $this->tags),
|
2026-09-30 17:22:41 +02:00
|
|
|
'order' => $this->order,
|
2026-11-14 16:06:21 +01:00
|
|
|
'product_weight' => $this->product_weight?->value,
|
|
|
|
|
'weight_variable' => $this->weight_variable,
|
2026-09-30 17:22:41 +02:00
|
|
|
'status' => $this->status,
|
|
|
|
|
'star' => $this->star,
|
2026-11-14 16:25:59 +01:00
|
|
|
'orderable_start' => $this->orderable_start,
|
|
|
|
|
'orderable_end' => $this->orderable_end,
|
2026-09-30 17:22:41 +02:00
|
|
|
'short_description' => $this->short_description,
|
|
|
|
|
'description' => $this->description,
|
2026-09-30 16:27:40 +02:00
|
|
|
];
|
|
|
|
|
}
|
2026-11-14 16:25:59 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array{
|
|
|
|
|
* price: string|null,
|
|
|
|
|
* discount: array<string, mixed>|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,
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-09-30 16:27:40 +02:00
|
|
|
}
|