Skip to content

Commit

Permalink
cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrouhal committed Sep 14, 2018
1 parent 666c325 commit f90ad47
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/ExtractableTraits/ExtractableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function fromOrNull(
public static function extractOrNull(
$data,
string $key,
bool $nullIfInvalid = false // default value breaks code sniffer
bool $nullIfInvalid = false
): ?self
{
if (!\is_array($data)) {
Expand Down
40 changes: 24 additions & 16 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ private function __construct(string $value)
}
}

public function getCountry(): CountryCode
{
return $this->country;
}

public function getValue(): string
{
return $this->value;
}

private function initilize(
string $value
): bool {
$value = (string) \preg_replace(
'/\s+/',
'',
$value
);
$value = (string) \preg_replace(
'~\x{00a0}~',
'',
$value = $this->preprocessValue(
$value
);

Expand Down Expand Up @@ -127,14 +130,19 @@ private function initilize(
return false;
}

public function getCountry(): CountryCode
{
return $this->country;
}

public function getValue(): string
{
return $this->value;
private function preprocessValue(
string $value
): string {
$value = (string) \preg_replace(
'/\s+/',
'',
$value
);
return (string) \preg_replace(
'~\x{00a0}~',
'',
$value
);
}

}
71 changes: 38 additions & 33 deletions src/VatId.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ final class VatId implements ToStringInterface
*/
private $vatNumber;

/**
* @var string[]
*/
private static $patternsByCountry = [
CountryCode::AT => 'ATU\d{8}',
CountryCode::BE => 'BE[0-1]\d{9}',
CountryCode::BG => 'BG\d{9,10}',
CountryCode::HR => 'HR\d{11}',
CountryCode::CY => 'CY\d{8}[A-Z]',
CountryCode::CZ => 'CZ\d{8,10}',
CountryCode::DK => 'DK(\d{2}){3}(\d{2})',
CountryCode::EE => 'EE\d{9}',
CountryCode::FI => 'FI\d{8}',
CountryCode::FR => 'FR[A-Z0-9]{2}\d{9}',
CountryCode::DE => 'DE\d{9}',
CountryCode::GR => '(GR|EL)\d{9}',
CountryCode::HU => 'HU\d{8}',
CountryCode::IE => 'IE\d{7}[A-Z]{1,2}',
CountryCode::IT => 'IT\d{11}',
CountryCode::LV => 'LV\d{11}',
CountryCode::LT => 'LT(\d{9}|\d{12})',
CountryCode::LU => 'LU\d{8}',
CountryCode::MT => 'MT\d{8}',
CountryCode::NL => 'NL\d{9}B\d{2}',
CountryCode::PL => 'PL\d{10}',
CountryCode::PT => 'PT\d{9}',
CountryCode::RO => 'RO\d{2,10}',
CountryCode::SK => 'SK\d{10}',
CountryCode::SI => 'SI\d{8}',
CountryCode::ES => 'ES(([A-Z]\d{8})|([A-Z]\d{7}[A-Z]))',
CountryCode::SE => 'SE\d{12}',
CountryCode::CH => 'CHE\d{9}((MWST)|(TVA)|(IVA))',
CountryCode::GB => 'GB((\d{9})|(\d{12}))',
CountryCode::GG => 'GY\d{6}',
];

private function __construct(string $vatId)
{
[$this->country, $this->prefix, $this->vatNumber] = self::extractCountryAndPrefixAndNumber($vatId);
Expand Down Expand Up @@ -100,7 +136,7 @@ private static function isValidForCountry(CountryCode $country, ?string $prefix,
}

$modulo = Arrays::get(self::getDivisible(), $country->getValue(), 1);
if (Validators::isNumericInt($vatNumber) && ($vatNumber % $modulo !== 0)) {
if (($vatNumber % $modulo !== 0) && Validators::isNumericInt($vatNumber)) {
return false;
}

Expand All @@ -112,38 +148,7 @@ private static function isValidForCountry(CountryCode $country, ?string $prefix,
*/
private static function getPatternsByCountry(): array
{
return [
CountryCode::AT => 'ATU\d{8}',
CountryCode::BE => 'BE[0-1]\d{9}',
CountryCode::BG => 'BG\d{9,10}',
CountryCode::HR => 'HR\d{11}',
CountryCode::CY => 'CY\d{8}[A-Z]',
CountryCode::CZ => 'CZ\d{8,10}',
CountryCode::DK => 'DK(\d{2}){3}(\d{2})',
CountryCode::EE => 'EE\d{9}',
CountryCode::FI => 'FI\d{8}',
CountryCode::FR => 'FR[A-Z0-9]{2}\d{9}',
CountryCode::DE => 'DE\d{9}',
CountryCode::GR => '(GR|EL)\d{9}',
CountryCode::HU => 'HU\d{8}',
CountryCode::IE => 'IE\d{7}[A-Z]{1,2}',
CountryCode::IT => 'IT\d{11}',
CountryCode::LV => 'LV\d{11}',
CountryCode::LT => 'LT(\d{9}|\d{12})',
CountryCode::LU => 'LU\d{8}',
CountryCode::MT => 'MT\d{8}',
CountryCode::NL => 'NL\d{9}B\d{2}',
CountryCode::PL => 'PL\d{10}',
CountryCode::PT => 'PT\d{9}',
CountryCode::RO => 'RO\d{2,10}',
CountryCode::SK => 'SK\d{10}',
CountryCode::SI => 'SI\d{8}',
CountryCode::ES => 'ES(([A-Z]\d{8})|([A-Z]\d{7}[A-Z]))',
CountryCode::SE => 'SE\d{12}',
CountryCode::CH => 'CHE\d{9}((MWST)|(TVA)|(IVA))',
CountryCode::GB => 'GB((\d{9})|(\d{12}))',
CountryCode::GG => 'GY\d{6}',
];
return self::$patternsByCountry;
}

/**
Expand Down

0 comments on commit f90ad47

Please sign in to comment.