ldc-boutique-cnc-sable/app/Dto/OrderDto.php

118 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\Order;
use App\Models\OrderLine;
use Carbon\CarbonInterface;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderReference;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Money\Money;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class OrderDto implements Arrayable, UrlRoutable
{
/**
* @param Collection<int, OrderLineDto> $lines
*/
public function __construct(
public string $id,
public OrderReference $reference,
public Money $total,
public OrderStatus $status,
public ?CarbonInterface $created_at = null,
public ?CarbonInterface $pickup_day = null,
public ?OrderEmployee $employee = null,
public ?ShopDto $shop = null,
public ?string $shop_id = null,
public ?DeliveryAddress $delivery_address = null,
public ?string $employee_matricule = null,
public Collection $lines = new Collection(),
) {
}
public static function fromModel(Order $order): self
{
/** @var Collection<int, OrderLineDto> $lines */
$lines = $order->relationLoaded('lines')
? $order->lines->map(fn (OrderLine $line): OrderLineDto => OrderLineDto::fromModel($line))
: new Collection();
return new self(
id: (string) $order->id,
reference: $order->reference,
total: $order->total,
status: $order->status,
created_at: $order->created_at,
pickup_day: $order->pickup_day,
employee: $order->employee,
shop: $order->relationLoaded('shop') && $order->shop ? ShopDto::fromModel($order->shop) : null,
shop_id: $order->shop_id !== null ? (string) $order->shop_id : null,
delivery_address: $order->delivery_address,
employee_matricule: $order->employee_matricule,
lines: $lines,
);
}
#[\Override]
public function getRouteKey(): mixed
{
return $this->id;
}
#[\Override]
public function getRouteKeyName(): string
{
return 'id';
}
#[\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,
'reference' => (string) $this->reference,
'total' => $this->total->getAmount(),
'status' => $this->status->value,
'created_at' => $this->created_at?->toISOString(),
'pickup_day' => $this->pickup_day?->toISOString(),
'employee' => $this->employee !== null ? [
'matricule' => $this->employee->matricule->value,
'firstname' => $this->employee->firstname,
'lastname' => $this->employee->lastname,
'facility' => $this->employee->facility,
'site' => $this->employee->site,
'service' => $this->employee->service,
] : null,
'shop' => $this->shop?->toArray(),
'shop_id' => $this->shop_id,
'delivery_address' => $this->delivery_address?->toArray(),
'employee_matricule' => $this->employee_matricule,
'lines' => $this->lines->map(fn (OrderLineDto $line): array => $line->toArray())->all(),
];
}
}