Updated the OrderService to utilize the new fromBasket method for creating orders, enhancing code clarity and maintainability. Removed the ProductAvailabilityService as its functionality is now integrated into the ProductRepository.
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Domain\Models\Entities\Shop;
|
|
|
|
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
|
|
);
|
|
}
|
|
|
|
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,
|
|
];
|
|
}
|
|
}
|