ldc-cocotte/app/Dto/ProductDto.php
Jérémy Munsch 93e4b202da
feat(products): enhance product form data with additional fields
Added support for new product attributes including tax rate, invoicing unit, order unit, and orderable dates. Updated related queries and views to accommodate these changes.
2026-11-15 16:45:00 +01:00

190 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\Product;
use App\Models\Tag;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\ProductWeight;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Carbon;
use Money\Money;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class ProductDto implements Arrayable, UrlRoutable
{
/**
* @param list<TagDto> $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<TagDto> $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<string, mixed>
*/
#[\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<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,
];
}
}