Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to exceptions and BE, LU formatters #13

Merged
merged 9 commits into from
Oct 8, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/composer.lock
/.phpunit.result.cache
/.idea
rubentebogt marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions src/InvalidPostcodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,39 @@
*/
class InvalidPostcodeException extends \Exception
{
protected string $postcode;
protected string $country;

/**
* Construct exception thrown when trying to format an invalid postcode.
*
* @param string $postcode
* @param string $country
*/
public function __construct(string $postcode, string $country)
{
parent::__construct('Invalid postcode: ' . $postcode);
$this->postcode = $postcode;
$this->country = $country;
}

/**
* Get the invalid postcode associated with this exception.
*
* @return string
*/
public function getPostcode(): string
{
return $this->postcode;
}

/**
* Get the country code associated with this exception.
*
* @return string
*/
public function getCountryCode(): string
rubentebogt marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->country;
}
}
6 changes: 3 additions & 3 deletions src/PostcodeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ public function format(string $country, string $postcode) : string
$formatter = $this->getFormatter($country);

if ($formatter === null) {
throw new UnknownCountryException('Unknown country: ' . $country);
throw new UnknownCountryException($country);
}

if (preg_match('/^[A-Z0-9]+$/', $postcode) !== 1) {
throw new InvalidPostcodeException('Invalid postcode: ' . $postcode);
throw new InvalidPostcodeException($postcode, $country);
}

$formatted = $formatter->format($postcode);

if ($formatted === null) {
throw new InvalidPostcodeException('Invalid postcode: ' . $postcode);
throw new InvalidPostcodeException($postcode, $country);
}

return $formatted;
Expand Down
22 changes: 22 additions & 0 deletions src/UnknownCountryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,26 @@
*/
class UnknownCountryException extends \Exception
{
protected string $country;

/**
* Construct exception thrown when an unknown country code is provided.
*
* @param string $country
*/
public function __construct(string $country)
{
parent::__construct('Unknown country: ' . $country);
$this->country = $country;
}

/**
* Get the unknown country code associated with this exception.
*
* @return string
*/
public function getCountryCode(): string
rubentebogt marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->country;
}
}