Skip to content

Commit ab9b438

Browse files
committed
Reorganize exceptions
1 parent e6f6132 commit ab9b438

7 files changed

Lines changed: 78 additions & 77 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\Postcode\Exception;
6+
7+
/**
8+
* Exception thrown when trying to format an invalid postcode.
9+
*/
10+
final class InvalidPostcodeException extends PostcodeException
11+
{
12+
public function __construct(
13+
private readonly string $postcode,
14+
private readonly string $country,
15+
) {
16+
parent::__construct('Invalid postcode: ' . $postcode);
17+
}
18+
19+
/**
20+
* Returns the invalid postcode that triggered this exception.
21+
*/
22+
public function getPostcode(): string
23+
{
24+
return $this->postcode;
25+
}
26+
27+
/**
28+
* Returns the ISO country code used to format the postcode.
29+
*/
30+
public function getCountry(): string
31+
{
32+
return $this->country;
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\Postcode\Exception;
6+
7+
use RuntimeException;
8+
9+
/**
10+
* Base class for postcode exceptions.
11+
*/
12+
abstract class PostcodeException extends RuntimeException
13+
{
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\Postcode\Exception;
6+
7+
/**
8+
* Exception thrown when trying to format a postcode with an unknown country code.
9+
*/
10+
final class UnknownCountryException extends PostcodeException
11+
{
12+
public function __construct(
13+
private readonly string $country,
14+
) {
15+
parent::__construct('Unknown country: ' . $country);
16+
}
17+
18+
/**
19+
* Returns the ISO country code used to format the postcode.
20+
*/
21+
public function getCountry(): string
22+
{
23+
return $this->country;
24+
}
25+
}

src/InvalidPostcodeException.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/PostcodeFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Brick\Postcode;
66

7+
use Brick\Postcode\Exception\InvalidPostcodeException;
8+
use Brick\Postcode\Exception\UnknownCountryException;
9+
710
use function array_key_exists;
811
use function class_exists;
912
use function preg_match;

src/UnknownCountryException.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/PostcodeFormatterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Brick\Postcode\Tests;
66

7-
use Brick\Postcode\InvalidPostcodeException;
7+
use Brick\Postcode\Exception\InvalidPostcodeException;
8+
use Brick\Postcode\Exception\UnknownCountryException;
89
use Brick\Postcode\PostcodeFormatter;
9-
use Brick\Postcode\UnknownCountryException;
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111
use PHPUnit\Framework\TestCase;
1212

0 commit comments

Comments
 (0)