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 */ 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, ]; } }