From 162002a86be3f5c650da44fb6be8823f6ec950bf Mon Sep 17 00:00:00 2001 From: bsh Date: Wed, 13 Nov 2024 13:52:22 +0100 Subject: [PATCH] Add Hungarian VAT number validation (#40) * Add Hungarian VAT number validation - Implement checksum validation for Hungarian VAT numbers. - Update `VatValidator.php` to include Hungarian VAT validation logic. - Add tests for Hungarian VAT number validation in `VatValidatorTest.php`. --- src/VatValidator.php | 25 +++++++++++++++++++++++++ tests/VatValidatorTest.php | 11 +++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/VatValidator.php b/src/VatValidator.php index fdc9b51..a0dbd55 100644 --- a/src/VatValidator.php +++ b/src/VatValidator.php @@ -87,6 +87,10 @@ public function validateFormat(string $vatNumber): bool return $result % 10 == 0; } + if ($validate_rule && $country === 'HU') { + return $this->validateHuVat($number); + } + return $validate_rule; } @@ -136,6 +140,27 @@ public static function luhnCheck(string $vat): int return $sum; } + /** + * Validates a Hungarian VAT number. + * + * @param string $vat + * @return bool + */ + private function validateHuVat(string $vat): bool + { + $checksum = (int) $vat[7]; + $weights = [9, 7, 3, 1, 9, 7, 3]; + $sum = 0; + + foreach ($weights as $i => $weight) { + $sum += (int) $vat[$i] * $weight; + } + + $calculatedChecksum = (10 - ($sum % 10)) % 10; + + return $calculatedChecksum === $checksum; + } + /** * Validates a VAT number . * diff --git a/tests/VatValidatorTest.php b/tests/VatValidatorTest.php index 476f209..77fe777 100644 --- a/tests/VatValidatorTest.php +++ b/tests/VatValidatorTest.php @@ -34,6 +34,7 @@ public function testVatWrongFormat(): void $vat_numbers = [ '', 'IT1234567890', + 'HU23395381', 'IT12345', 'foobar123', ]; @@ -57,4 +58,14 @@ public function testLuhnCheck(): void self::assertIsInt($this->validator->luhnCheck($this->fake_vat)); self::assertNotEquals(0, $this->validator->luhnCheck($this->fake_vat)); } + + public function testHuVatValidFormat(): void + { + self::assertTrue($this->validator->validateFormat('HU28395515')); + } + + public function testHuVatInvalidFormat(): void + { + self::assertFalse($this->validator->validateFormat('HU28395514')); + } }