Skip to content
Merged
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
36 changes: 36 additions & 0 deletions src/FormatHelper/StripPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Brick\Postcode\FormatHelper;

/**
* Strip country code prefix from postalcode if it is valid.
*
* In some countries like Belgium or Luxembourg its common to provide a country code prefix in a postalcode,
* Although the format is incorrect, many government agencies, businesses and people still use this.
* Rather than failing this format into an exception, this helper class strips the prefix off before validating it,
* 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 StripPrefix
{
/**
* @param string $postcode
* @param string $prefix
* @return string
*/
public function stripPrefix(string $postcode, string $prefix): string
{
$prefixLength = strlen($prefix);

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

return $postcode;
}
}
2 changes: 1 addition & 1 deletion src/Formatter/ADFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Andorra
*/
class ADFormatter implements CountryPostcodeFormatter
final class ADFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AFFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Afghanistan
*/
class AFFormatter implements CountryPostcodeFormatter
final class AFFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AIFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class AIFormatter implements CountryPostcodeFormatter
final class AIFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/ALFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Albania
*/
class ALFormatter implements CountryPostcodeFormatter
final class ALFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AMFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Armenia
*/
class AMFormatter implements CountryPostcodeFormatter
final class AMFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AQFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class AQFormatter implements CountryPostcodeFormatter
final class AQFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/ARFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Argentina
*/
class ARFormatter implements CountryPostcodeFormatter
final class ARFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/ASFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_American_Samoa
*/
class ASFormatter implements CountryPostcodeFormatter
final class ASFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
7 changes: 6 additions & 1 deletion src/Formatter/ATFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\Postcode\Formatter;

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

/**
* Validates and formats postcodes in Austria.
Expand All @@ -14,10 +15,14 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Austria
*/
class ATFormatter implements CountryPostcodeFormatter
final class ATFormatter implements CountryPostcodeFormatter
{
use StripPrefix;

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

if (preg_match('/^[1-9][0-9]{3}$/', $postcode) !== 1) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AUFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postcodes_in_Australia
*/
class AUFormatter implements CountryPostcodeFormatter
final class AUFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AXFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class AXFormatter implements CountryPostcodeFormatter
final class AXFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/AZFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Azerbaijan
*/
class AZFormatter implements CountryPostcodeFormatter
final class AZFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BAFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class BAFormatter implements CountryPostcodeFormatter
final class BAFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BBFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Barbados
*/
class BBFormatter implements CountryPostcodeFormatter
final class BBFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BDFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/List_of_postal_codes_in_Bangladesh
*/
class BDFormatter implements CountryPostcodeFormatter
final class BDFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
7 changes: 6 additions & 1 deletion src/Formatter/BEFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\Postcode\Formatter;

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

/**
* Validates and formats postcodes in Belgium.
Expand All @@ -14,10 +15,14 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Belgium
*/
class BEFormatter implements CountryPostcodeFormatter
final class BEFormatter implements CountryPostcodeFormatter
{
use StripPrefix;

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

if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BGFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Bulgaria
*/
class BGFormatter implements CountryPostcodeFormatter
final class BGFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BHFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class BHFormatter implements CountryPostcodeFormatter
final class BHFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class BLFormatter implements CountryPostcodeFormatter
final class BLFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BMFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Bermuda
*/
class BMFormatter implements CountryPostcodeFormatter
final class BMFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BNFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Brunei
*/
class BNFormatter implements CountryPostcodeFormatter
final class BNFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/C%C3%B3digo_de_Endere%C3%A7amento_Postal
*/
class BRFormatter implements CountryPostcodeFormatter
final class BRFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BTFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class BTFormatter implements CountryPostcodeFormatter
final class BTFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/BYFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class BYFormatter implements CountryPostcodeFormatter
final class BYFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CAFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Canada
*/
class CAFormatter implements CountryPostcodeFormatter
final class CAFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CCFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class CCFormatter implements CountryPostcodeFormatter
final class CCFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CHFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Switzerland_and_Liechtenstein
*/
class CHFormatter implements CountryPostcodeFormatter
final class CHFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Chile
*/
class CLFormatter implements CountryPostcodeFormatter
final class CLFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CNFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/List_of_postal_codes_in_China
*/
class CNFormatter implements CountryPostcodeFormatter
final class CNFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/COFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://es.wikipedia.org/wiki/Anexo:C%C3%B3digos_postales_de_Colombia
*/
class COFormatter implements CountryPostcodeFormatter
final class COFormatter implements CountryPostcodeFormatter
{
private const DEPARTMENTS = [
'05', '08', '11', '13',
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/List_of_districts_of_Costa_Rica
*/
class CRFormatter implements CountryPostcodeFormatter
final class CRFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CUFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class CUFormatter implements CountryPostcodeFormatter
final class CUFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CVFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class CVFormatter implements CountryPostcodeFormatter
final class CVFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CXFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class CXFormatter implements CountryPostcodeFormatter
final class CXFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CYFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Cyprus
*/
class CYFormatter implements CountryPostcodeFormatter
final class CYFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/CZFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
*/
class CZFormatter implements CountryPostcodeFormatter
final class CZFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/DEFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Germany
*/
class DEFormatter implements CountryPostcodeFormatter
final class DEFormatter implements CountryPostcodeFormatter
{
public function format(string $postcode) : ?string
{
Expand Down
Loading