ldc-boutique-cnc-sable/domain/Models/ValueObjects/DeliveryAddress.php

72 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Domain\Models\Entities\Shop;
2026-06-14 21:32:07 +02:00
final readonly class DeliveryAddress
{
public function __construct(
public string $name,
public string $address,
public string $postcode,
public string $city,
public ?string $phone = null,
public ?string $latitude = null,
public ?string $longitude = null,
) {
$this->validate();
}
public static function fromShop(Shop $shop): self
{
return new self(
name: $shop->name,
address: $shop->address,
postcode: $shop->postcode,
city: $shop->city,
phone: $shop->phone,
latitude: $shop->latitude,
longitude: $shop->longitude
);
}
2026-06-14 21:32:07 +02:00
private function validate(): void
{
if (trim($this->name) === '') {
throw new \DomainException('Store name cannot be empty.');
}
if (trim($this->address) === '') {
throw new \DomainException('Address cannot be empty.');
}
if (trim($this->postcode) === '') {
throw new \DomainException('Postcode cannot be empty.');
}
if (trim($this->city) === '') {
throw new \DomainException('City cannot be empty.');
}
if ($this->latitude !== null && $this->longitude === null) {
throw new \DomainException('Longitude must be provided if latitude is.');
}
}
/**
* @return array<string,string|null>
*/
public function toArray(): array
{
return [
'name' => $this->name,
'address' => $this->address,
'postcode' => $this->postcode,
'city' => $this->city,
'phone' => $this->phone,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
];
}
}