Skip to content

Commit

Permalink
Add Hungarian VAT number validation (#40)
Browse files Browse the repository at this point in the history
* 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`.
  • Loading branch information
bsh authored Nov 13, 2024
1 parent 0d845f4 commit 162002a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/VatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 .
*
Expand Down
11 changes: 11 additions & 0 deletions tests/VatValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testVatWrongFormat(): void
$vat_numbers = [
'',
'IT1234567890',
'HU23395381',
'IT12345',
'foobar123',
];
Expand All @@ -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'));
}
}

0 comments on commit 162002a

Please sign in to comment.