From 3cad498363503bf463ff2e87d37858ac00614df2 Mon Sep 17 00:00:00 2001 From: niels Date: Tue, 18 Feb 2025 16:03:36 +0100 Subject: [PATCH] Add checksum to CY validator --- src/Vies/Validator/ValidatorCY.php | 29 ++++++++++++++++++++++-- tests/Vies/Validator/ValidatorCYTest.php | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Vies/Validator/ValidatorCY.php b/src/Vies/Validator/ValidatorCY.php index 3cdd866..d05658a 100644 --- a/src/Vies/Validator/ValidatorCY.php +++ b/src/Vies/Validator/ValidatorCY.php @@ -41,7 +41,32 @@ public function validate(string $vatNumber): bool return false; } - return in_array((int) $vatNumber[0], [0, 1, 3, 4, 5, 6, 9], true) - && ctype_alpha($vatNumber[8]); + $total = 0; + for ($i = 0; $i < 8; ++$i) { + $temp = (int) $vatNumber[$i]; + if (0 === $i % 2) { + if (0 === $temp) { + $temp = 1; + } elseif (1 === $temp) { + $temp = 0; + } elseif (2 === $temp) { + $temp = 5; + } elseif (3 === $temp) { + $temp = 7; + } elseif (4 === $temp) { + $temp = 9; + } else { + $temp = 2 * $temp + 3; + } + } + $total += $temp; + } + + // Establish check digit using modulus 26, and translate to char. equivalent. + $total = $total % 26; + $total = chr($total + 65); + + // Check to see if the check digit given is correct + return $vatNumber[8] === $total; } } diff --git a/tests/Vies/Validator/ValidatorCYTest.php b/tests/Vies/Validator/ValidatorCYTest.php index 0c536f9..47616f1 100644 --- a/tests/Vies/Validator/ValidatorCYTest.php +++ b/tests/Vies/Validator/ValidatorCYTest.php @@ -18,6 +18,8 @@ public function testValidator(string $vatNumber, bool $state) public function vatNumberProvider() { return [ + ['60040893B', true], + ['60052853Y', true], ['00532445O', true], ['005324451', false], ['0053244511', false],