About this exercise
I found out there is a form of strict types in PHP 7.4, so I freshened up my PHP coding with this simple Address
class. I did not use PHP since forever. So, any and all answers and comments highly appreciated. (This question is an update on its deleted version; only users with reputation above 10k threshold can see deleted questions)
I still do not know exactly how the exceptions are to be used. If I made some sort of error in this area, please excuse me, but this code is working on my webserver without much testing.
Additional Note (EDIT): This class is culturally specific to the Czech Republic, where, just to clarify:
Street and City have a minimum of 2 characters.
All of the following must be integers: House number / Orientation number, and the Zip code. There can be no letters to the House number for example, nor in the other two.
Code
<?php
// independent class
declare(strict_types=1);
class Address
{
private string $street = "";
private int $houseNumber = 0;
private int $orientationNumber = 0;
private string $city = "";
private int $zip = 0;
public function setStreet(string $newStreet): void
{
if (strlen($newStreet) >= 2)
{
$this->street = $newStreet;
}
else
{
throw new Exception("Street needs to be of length 2 or more.");
}
}
public function setHouseNumber(int $newHouseNumber): void
{
if (is_int($newHouseNumber) && $newHouseNumber > 0)
{
$this->houseNumber = $newHouseNumber;
}
else
{
throw new Exception('House number needs to have a value greater than 0.');
}
}
public function setOrientationNumber(int $newOrientationNumber): void
{
if (is_int($newOrientationNumber) && $newOrientationNumber >= 0)
{
$this->orientationNumber = $newOrientationNumber;
}
else
{
throw new Exception("Orientation number needs to have a value greater than 0; Can be equal to 0, in which case it won't be visible in full address.");
}
}
public function setCity(string $newCity): void
{
if (strlen($newCity) >= 2)
{
$this->city = $newCity;
}
else
{
throw new Exception("City needs to be of length 2 or more.");
}
}
public function setZip(int $newZip): void
{
if (is_integer($newZip) && $newZip >= 10000 && $newZip <= 99999 )
{
$this->zip = $newZip;
}
else
{
throw new Exception("Czech postal code must be integer in hypothetical range <10000;99999>.");
}
}
public function __construct(
string $initStreet,
int $initHouseNumber,
int $initOrientationNumber,
string $initCity,
int $initZip)
{
$this->setStreet($initStreet);
$this->setHouseNumber($initHouseNumber);
$this->setOrientationNumber($initOrientationNumber);
$this->setCity($initCity);
$this->setZip($initZip);
}
public function getStreet(): string
{
return $this->street;
}
public function getHouseNumber(): int
{
return $this->houseNumber;
}
public function getOrientationNumber(): int
{
return $this->orientationNumber;
}
public function getCity(): string
{
return $this->city;
}
public function getZip(): int
{
return $this->zip;
}
public function getFullAddress(): string
{
$fullAddress = $this->street . " " . $this->houseNumber;
if ($this->orientationNumber !== 0)
{
$fullAddress .= "/" . $this->orientationNumber;
}
$fullAddress .= ", " . $this->zip . " " . $this->city;
return $fullAddress;
}
}
int
withint $newHouseNumber
, then theis_int($newHouseNumber)
check is useless. \$\endgroup\$