Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions dto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
50 changes: 27 additions & 23 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
<?php
require "dto.php";
require_once "dto.php";

if (empty($_GET['firstName'])) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>commit hygiene example</title>
</head>
<head>
<title>commit hygiene example</title>
</head>
<body>
<form>
<label for="firstName">first name</label>
<input id="firstName" name="firstName" /><br>
<label for="lastName">last name</label><input id="lastName" name="lastName" /><br>
<label for="street">street</label>
<input id="street" name="street" /><br>
<label for="zip">zip</label><input id="zip" name="zip" /><br>
<label for="city">city</label>
<input id="city" name="city" /><br>
<label>first name <input name="firstName"/></label><br>
<label>last name <input name="lastName"/></label><br>
<h2>invoice address</h2>
<label>street <input name="invoiceAddress[street]"/></label><br>
<label>zip <input name="invoiceAddress[zip]"/></label><br>
<label>city <input name="invoiceAddress[city]"/></label><br>
<h2>delivery address (optional)</h2>
<label>street <input name="deliveryAddress[street]"/></label><br>
<label>zip <input name="deliveryAddress[zip]"/></label><br>
<label>city <input name="deliveryAddress[city]"/></label><br>
<button type="submit">submit</button>
</form>
</body>
</html>
<?php
} else {
$user = new User($_GET);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
?>
<!DOCTYPE html>
<html lang="en">
<head> <title>commit hygiene example</title> </head>
<head><title>commit hygiene example</title></head>
<body>
<h1><?=$user->getFirstName() . ' ' . $user->getLastName()?></h1>
<p><?=$user->getStreet()?><br><span style="font-size: larger"><?=$user->getZip()?></span> <?=$user->getCity()?>
<h1><?= htmlspecialchars($user->getFirstName() . ' ' . $user->getLastName(), ENT_HTML5) ?></h1>
<h2>invoice address</h2>
<p>
<?= htmlspecialchars($user->getInvoiceAddress()->getStreet(), ENT_HTML5) ?><br>
<span style="font-size: larger"><?= htmlspecialchars($user->getInvoiceAddress()->getZip(), ENT_HTML5) ?></span> <?= htmlspecialchars($user->getInvoiceAddress()->getCity(), ENT_HTML5) ?>
</p>
<h2>delivery address</h2>
<p>
<?= htmlspecialchars($user->getDeliveryAddress()->getStreet(), ENT_HTML5) ?><br>
<span style="font-size: larger"><?= htmlspecialchars($user->getDeliveryAddress()->getZip(), ENT_HTML5) ?></span> <?= htmlspecialchars($user->getDeliveryAddress()->getCity(), ENT_HTML5) ?>
</p>
<form>
<button type="submit" formaction="index.php">new</button>
</form>
</body>
</html>
<?php
}
?>