'AT', 'BE' => 'BE', 'DK' => 'DK', 'FI' => 'FI', 'FR' => 'FR', 'FX' => 'FR', 'DE' => 'DE', 'GB' => 'XI', 'GR' => 'EL', 'IE' => 'IE', 'IT' => 'IT', 'LU' => 'LU', 'NL' => 'NL', 'PT' => 'PT', 'ES' => 'ES', 'SE' => 'SE', 'CY' => 'CY', 'EE' => 'EE', 'HU' => 'HU', 'LV' => 'LV', 'LT' => 'LT', 'MT' => 'MT', 'PL' => 'PL', 'SK' => 'SK', 'CZ' => 'CZ', 'SI' => 'SI', 'RO' => 'RO', 'BG' => 'BG', 'HR' => 'HR', ]; private $isoPattern = [ 'AT' => 'U[A-Z\d]{8}', 'BE' => '[0|1]{1}\d{9}', 'BG' => '\d{9,10}', 'CY' => '\d{8}[A-Z]', 'CZ' => '\d{8,10}', 'DE' => '\d{9}', 'DK' => '(\d{2} ?){3}\d{2}', 'EE' => '\d{9}', 'GB' => '\d{9}(\d{3})?|[A-Z]{2}\d{3}', 'GR' => '\d{9}', 'ES' => '[A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8}', 'FI' => '\d{8}', 'FR' => '([A-Z0-9]{2})\d{9}', 'FX' => '([A-Z0-9]{2})\d{9}', 'HR' => '\d{11}', 'HU' => '\d{8}', 'IE' => '[A-Z\d]{8}|[A-Z\d]{9}', 'IT' => '\d{11}', 'LT' => '(\d{9}|\d{12})', 'LU' => '\d{8}', 'LV' => '\d{11}', 'MT' => '\d{8}', 'NL' => '\d{9}B\d{2}', 'PL' => '\d{10}', 'PT' => '\d{9}', 'RO' => '\d{2,10}', 'SE' => '\d{12}', 'SI' => '\d{8}', 'SK' => '\d{10}', ]; public function __construct( string $rawVatNumber, string $rawIsoCode ) { $this->vatNumber = $this->commonFormatVatNumber($rawVatNumber); $this->isoCode = strtoupper($rawIsoCode); $this->validateVatNr(); } protected function validateVatNr() { $vatPrefix = self::$euIso[$this->isoCode] ?? $this->isoCode; if( substr($this->vatNumber, 0, 2) != $vatPrefix ){ $this->errorMessage = Context::getContext()->getTranslator()->trans('ISO in VAT number doesn\'t seem to match selected country', [], 'Modules.Ws_VatCheck.Front'); } $trimmedVat = str_replace($vatPrefix, '', $this->vatNumber); $isoPattern = $this->isoPattern[$this->isoCode]; if( !preg_match('/^(?:'.$isoPattern.')$/', $trimmedVat) ){ $this->errorMessage = Context::getContext()->getTranslator()->trans('VAT number doesn\'t seem to be valid', [], 'Modules.Ws_VatCheck.Front'); } } public function getFormattedVat() { return $this->vatNumber; } public function hasErrors() { return !empty($this->errorMessage); } public function isNotEuIso() { return !array_key_exists($this->isoCode, self::$euIso); } private function commonFormatVatNumber(string $rawVatNumber) { return strtoupper(preg_replace('/[ .]/', '', $rawVatNumber)); } public function getErrorMessage() { return $this->errorMessage; } }