Skip to content

Commit

Permalink
StripCountryCode to StripPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
rubentebogt committed Sep 23, 2024
1 parent 6a111e0 commit 0913002
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
* Thus providing you with the correct format without the country code prefix in it.
*
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Belgium
*
* @internal
*/
trait StripCountryCode
trait StripPrefix
{
/**
* @param string $postcode
* @param int $validCountryCodeLength
* @param string $prefix
* @return string
*/
public function stripCountryCode(string $postcode, int $validCountryCodeLength = 1): string
public function stripPrefix(string $postcode, string $prefix): string
{
$code = substr((new \ReflectionClass($this))->getShortName(), 0, $validCountryCodeLength);
$prefixLength = strlen($prefix);

if (substr($postcode, 0, $validCountryCodeLength) === $code) {
$postcode = substr($postcode, $validCountryCodeLength);
if (substr($postcode, 0, $prefixLength) === $prefix) {
$postcode = substr($postcode, $prefixLength);
}

return $postcode;
Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/ATFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Brick\Postcode\Formatter;

use Brick\Postcode\CountryPostcodeFormatter;
use Brick\Postcode\FormatHelper\StripCountryCode;
use Brick\Postcode\FormatHelper\StripPrefix;

/**
* Validates and formats postcodes in Austria.
Expand All @@ -17,11 +17,11 @@
*/
class ATFormatter implements CountryPostcodeFormatter
{
use StripCountryCode;
use StripPrefix;

public function format(string $postcode) : ?string
{
$postcode = $this->stripCountryCode($postcode);
$postcode = $this->stripPrefix($postcode, 'A');

if (preg_match('/^[1-9][0-9]{3}$/', $postcode) !== 1) {

Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/BEFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Brick\Postcode\Formatter;

use Brick\Postcode\CountryPostcodeFormatter;
use Brick\Postcode\FormatHelper\StripCountryCode;
use Brick\Postcode\FormatHelper\StripPrefix;

/**
* Validates and formats postcodes in Belgium.
Expand All @@ -17,11 +17,11 @@
*/
class BEFormatter implements CountryPostcodeFormatter
{
use StripCountryCode;
use StripPrefix;

public function format(string $postcode) : ?string
{
$postcode = $this->stripCountryCode($postcode);
$postcode = $this->stripPrefix($postcode, 'B');

if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/LUFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Brick\Postcode\Formatter;

use Brick\Postcode\CountryPostcodeFormatter;
use Brick\Postcode\FormatHelper\StripCountryCode;
use Brick\Postcode\FormatHelper\StripPrefix;

/**
* Validates and formats postcodes in Luxembourg.
Expand All @@ -17,11 +17,11 @@
*/
class LUFormatter implements CountryPostcodeFormatter
{
use StripCountryCode;
use StripPrefix;

public function format(string $postcode) : ?string
{
$postcode = $this->stripCountryCode($postcode);
$postcode = $this->stripPrefix($postcode, 'L');

if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/InvalidPostcodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public function getPostcode(): string
}

/**
* Get the country code associated with this exception.
* Get the country ISO2 code associated with this exception.
*
* @return string
*/
public function getCountryCode(): string
public function getCountry(): string
{
return $this->country;
}
Expand Down
4 changes: 2 additions & 2 deletions src/UnknownCountryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public function __construct(string $country)
}

/**
* Get the unknown country code associated with this exception.
* Get the unknown country ISO2 code associated with this exception.
*
* @return string
*/
public function getCountryCode(): string
public function getCountry(): string
{
return $this->country;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Formatter/ATFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function providerFormat() : array
['ABCD', null],
['ABCDE', null],
['ABCDEF', null],

['A-8084', '8084'],
['a-8084', '8084'],
['X-8084', null],
['x-8084', null],
];
}
}
5 changes: 5 additions & 0 deletions tests/Formatter/BEFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function providerFormat() : array
['ABCD', null],
['ABCDE', null],
['ABCDEF', null],

['B-8084', '8084'],
['b-8084', '8084'],
['X-8084', null],
['x-8084', null],
];
}
}
6 changes: 6 additions & 0 deletions tests/Formatter/LUFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function providerFormat() : array
['ABC', null],
['ABCD', null],
['ABCDE', null],


['L-8084', '8084'],
['l-8084', '8084'],
['X-8084', null],
['x-8084', null],
];
}
}
16 changes: 2 additions & 14 deletions tests/PostcodeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
class PostcodeFormatterTest extends TestCase
{
/**
* @throws InvalidPostcodeException
*/
public function testFormatUnknownCountry() : void
{
$formatter = new PostcodeFormatter();
Expand All @@ -33,7 +30,6 @@ public function testFormatUnknownCountry() : void
* @param string $postcode
*
* @return void
* @throws UnknownCountryException
*/
public function testFormatInvalidPostcode(string $country, string $postcode) : void
{
Expand All @@ -53,9 +49,6 @@ public function providerFormatInvalidPostcode() : array
['FR', '123456'],
['GB', 'ABCDEFG'],
['PL', '12*345'],
['AT', 'T-8084'],
['BE', 'E-1245'],
['LU', 'X2556'],
];
}

Expand All @@ -67,8 +60,6 @@ public function providerFormatInvalidPostcode() : array
* @param string $expectedOutput
*
* @return void
* @throws InvalidPostcodeException
* @throws UnknownCountryException
*/
public function testFormat(string $country, string $postcode, string $expectedOutput) : void
{
Expand All @@ -85,10 +76,7 @@ public function providerFormat() : array
return [
['GB', 'WC2E9RZ', 'WC2E 9RZ'],
['gb', 'wc-2E9RZ', 'WC2E 9RZ'],
['PL', '12345', '12-345'],
['AT', 'A-8084', '8084'],
['BE', 'B-1245', '1245'],
['LU', 'L2556', '2556'],
['PL', '12345', '12-345']
];
}

Expand Down Expand Up @@ -118,4 +106,4 @@ public function providerIsSupportedCountry() : array
['UnknownCountry', false],
];
}
}
}

0 comments on commit 0913002

Please sign in to comment.