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 @@ - - commit hygiene example - + + commit hygiene example +
- -
-
- -
-
- -
+
+
+

invoice address

+
+
+
+

delivery address (optional)

+
+
+
@@ -25,21 +27,23 @@ - +?> + - commit hygiene example + commit hygiene example -

getFirstName() . ' ' . $user->getLastName()?>

-

getStreet()?>
getZip()?> getCity()?> +

getFirstName() . ' ' . $user->getLastName(), ENT_HTML5) ?>

+

invoice address

+

+ getInvoiceAddress()->getStreet(), ENT_HTML5) ?>
+ getInvoiceAddress()->getZip(), ENT_HTML5) ?> getInvoiceAddress()->getCity(), ENT_HTML5) ?> +

+

delivery address

+

+ getDeliveryAddress()->getStreet(), ENT_HTML5) ?>
+ getDeliveryAddress()->getZip(), ENT_HTML5) ?> getDeliveryAddress()->getCity(), ENT_HTML5) ?>

-
- -
- -