diff --git a/dto.php b/dto.php index f2ad13f..a94ff80 100644 --- a/dto.php +++ b/dto.php @@ -3,16 +3,17 @@ class User { private string $firstName; private string $lastName; - private string $street; - private string $zip; - private string $city; + + private Address $invoiceAddress; + private Address|null $deliveryAddress = null; public function __construct(array $data) { $this->firstName = $data['firstName']; $this->lastName = $data['lastName']; - $this->street = $data['street']; - $this->zip = $data['zip']; - $this->city = $data['city']; + $this->invoiceAddress = new Address($data['invoiceAddress']); + if (empty($data['deliveryAddress']['street'])) { + $this->deliveryAddress = new Address($data['deliveryAddress']); + } } public function getFirstName(): string { @@ -23,6 +24,26 @@ public function getLastName(): string { return $this->lastName; } + public function getInvoiceAddress(): Address { + return $this->invoiceAddress; + } + + public function getDeliveryAddress(): Address { + return $this->deliveryAddress ?: $this->invoiceAddress; + } +} + +class Address { + private string $street; + private string $zip; + private string $city; + + public function __construct(array $data) { + $this->street = $data['street']; + $this->zip = $data['zip']; + $this->city = $data['city']; + } + public function getStreet(): string { return $this->street; } diff --git a/index.php b/index.php index b4ad82b..df51736 100644 --- a/index.php +++ b/index.php @@ -1,23 +1,25 @@ -
-=$user->getStreet()?>
=$user->getZip()?> =$user->getCity()?>
+
+ = htmlspecialchars($user->getInvoiceAddress()->getStreet(), ENT_HTML5) ?>
+ = htmlspecialchars($user->getInvoiceAddress()->getZip(), ENT_HTML5) ?> = htmlspecialchars($user->getInvoiceAddress()->getCity(), ENT_HTML5) ?>
+
+ = htmlspecialchars($user->getDeliveryAddress()->getStreet(), ENT_HTML5) ?>
+ = htmlspecialchars($user->getDeliveryAddress()->getZip(), ENT_HTML5) ?> = htmlspecialchars($user->getDeliveryAddress()->getCity(), ENT_HTML5) ?>