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

116 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\PickupException;
use App\Models\Shop;
use Domain\Models\Entities\Shop as DomainShop;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class ShopDto implements Arrayable, UrlRoutable
{
/**
* @param \Illuminate\Support\Collection<int, \App\Dto\PickupExceptionDto>|null $pickupExceptions
*/
public function __construct(
public string $id,
public string $name,
public string $address,
public string $postcode,
public string $city,
public ?string $phone,
public ?string $latitude,
public ?string $longitude,
public bool $default,
public ?Collection $pickupExceptions = null,
) {
}
public static function fromModel(Shop $shop): self
{
$pickupExceptions = null;
if ($shop->relationLoaded('pickupExceptions')) {
$pickupExceptions = $shop->pickupExceptions->map(
fn (PickupException $pe): PickupExceptionDto => PickupExceptionDto::fromModel($pe)
);
}
return new self(
id: $shop->id,
name: $shop->name,
address: $shop->address,
postcode: $shop->postcode,
city: $shop->city,
phone: $shop->phone,
latitude: $shop->latitude,
longitude: $shop->longitude,
default: (bool) $shop->default,
pickupExceptions: $pickupExceptions,
);
}
public static function fromDomain(DomainShop $shop): self
{
return new self(
id: $shop->id->value->toString(),
name: $shop->name,
address: $shop->address,
postcode: $shop->postcode,
city: $shop->city,
phone: $shop->phone,
latitude: $shop->latitude,
longitude: $shop->longitude,
default: $shop->isDefault,
);
}
#[\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,
'name' => $this->name,
'address' => $this->address,
'postcode' => $this->postcode,
'city' => $this->city,
'phone' => $this->phone,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'default' => $this->default,
];
}
}