diff --git a/Validation/Finance/EUVat.php b/Validation/Finance/EUVat.php new file mode 100644 index 000000000..8b90a1647 --- /dev/null +++ b/Validation/Finance/EUVat.php @@ -0,0 +1,88 @@ + '/^(EU)([0-9][9])$/', // This is a special EU VAT. + 'AUT' => '/^(AT)U[0-9]{8}$/', + 'BEL' => '/^(BE)[0-9]{10}$/', + 'BGR' => '/^(BG)[0-9]{9,10}$/', + 'CYP' => '/^(CY)[0-9]{8}[A-Z]$/', + 'CZE' => '/^(CZ)([0-9]{8,10}|[0-9]{6}-[0-9]{3,4})$/', + 'DEU' => '/^(DE)[0-9]{9}$/', + 'DNK' => '/^(DK)[0-9]{8}$/', + 'EST' => '/^(EE)[0-9]{9}$/', + 'ESP' => '/^(ES)([A-Z][0-9]{8}|[A-Z][0-9]{7}[A-Z])$/', + 'FIN' => '/^(FI)([0-9]{8})$/', + 'FRA' => '/^(FR)[0-9]{2}[0-9]{9}$/', + 'GBR' => '/^(GB)([0-9]{9}|[0-9]{12}|(GD)[0-4][0-9]{2}|(HA)5[0-9]{2})$/', + 'GRC' => '/^(EL)([0-9]{7,9})$/', + 'HRV' => '/^(HR)([0-9]{11})$/', + 'HUN' => '/^(HU)([0-9]{8})$/', + 'IRL' => '/^(IE)([0-9]{7}[A-Z]{1,2}|[0-9][A-Z][0-9]{5}[A-Z])$/', + 'ITA' => '/^(IT)([0-9]{11})$/', + 'LTU' => '/^(LT)([0-9]{9}|[0-9]{12})$/', + 'LUX' => '/^(LU)([0-9]{8})$/', + 'LVA' => '/^(LV)([0-9]{11})$/', + 'MLT' => '/^(MT)([0-9]{8})$/', + 'NLD' => '/^(NL)([0-9]{9}B[0-9]{2})$/', + 'POL' => '/^(PL)([0-9]{10}|[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3})$/', + 'PRT' => '/^(PT)([0-9]{9})$/', + 'ROU' => '/^(RO)([0-9]{2,10})$/', + 'SWE' => '/^(SE)([0-9]{10}[0-9]{2})$/i', + 'SVN' => '/^(SI)([0-9]{8})$/', + 'SVK' => '/^(SK)([0-9][10])$/', + 'HRV' => '/^(HR)([0-9][11])$/', + ]; + + /** + * {@inheritdoc} + */ + public static function isValid(mixed $value, array $constraints = null) : bool + { + if (!\is_string($value)) { + return false; + } + + $value = \str_replace(' ', '', \strtoupper($value)); + + foreach (self::PATTERNS as $pattern) { + if ((bool) \preg_match($pattern, $value)) { + // VAT potentially valid + return true; + } elseif (\stripos($pattern, \substr($value, 0, 2)) !== false) { + // Regex found but didn't match, therefore VAT is considered potentially invalid + return false; + } + } + + // Couldn't find possible regex, therefore the VAT is considered potentially valid + return true; + } +}