From 48e0ad81ddf0a0cb7afaf03527c722c3c700ef5f Mon Sep 17 00:00:00 2001 From: Lukas Krempler Date: Sat, 25 May 2024 15:44:16 +0200 Subject: [PATCH] [feat] Added validation rule for austrian insurance numbers (#82) --- src/Rules/AustrianInsuranceNumber.php | 75 +++++++++++++++++++++ src/lang/de/validation.php | 1 + src/lang/en/validation.php | 1 + tests/Rules/AustrianInsuranceNumberTest.php | 34 ++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/Rules/AustrianInsuranceNumber.php create mode 100644 tests/Rules/AustrianInsuranceNumberTest.php diff --git a/src/Rules/AustrianInsuranceNumber.php b/src/Rules/AustrianInsuranceNumber.php new file mode 100644 index 0000000..d5a131d --- /dev/null +++ b/src/Rules/AustrianInsuranceNumber.php @@ -0,0 +1,75 @@ +startsNotWithZero($value) + && $this->hasValidLength($value) + && $this->checkChecksum($value); + } + + private function hasValidLength($svnumber): bool + { + return $this->length === strlen($svnumber); + } + + private function startsNotWithZero($svnumber): bool + { + return (int) $svnumber[0] !== 0; + } + + private function checkChecksum($svnumber): bool + { + if (strlen($svnumber) !== $this->length) { + return false; + } + + $checksumSVNumber = (int) $svnumber[3]; + $svnumberWithoutChecksum = substr($svnumber, 0, 3) . substr($svnumber, 4); + + $sum = 0; + for ($c = 0, $cMax = strlen($svnumberWithoutChecksum); $c < $cMax; $c++) { + $result = $svnumberWithoutChecksum[$c] * $this->multiplierSeries[$c]; + $sum += $result; + } + $checksum = $sum % 11; + + if ($checksum === 10) { + return false; + } + return $checksum === $checksumSVNumber; + } +} diff --git a/src/lang/de/validation.php b/src/lang/de/validation.php index f53aa15..1a3c495 100644 --- a/src/lang/de/validation.php +++ b/src/lang/de/validation.php @@ -34,4 +34,5 @@ 'gtin' => 'Der Wert :attribute ist keine gültige Global Trade Item Number (GTIN).', 'postalcode' => 'Der Wert :attribute muss eine gültige Postleitzahl sein.', 'mimetype' => 'Der Wert :attribute einhält keinen gültigen Internet Media Type (MIME-Type).', + 'austrianinsurancenumber' => 'Der Wert :attribute enthält keine gültige österreichische Versicherungsnummer', ]; diff --git a/src/lang/en/validation.php b/src/lang/en/validation.php index e61e5fa..23d5cd5 100644 --- a/src/lang/en/validation.php +++ b/src/lang/en/validation.php @@ -33,4 +33,5 @@ 'gtin' => 'The :attribute is not a valid Global Trade Item Number (GTIN).', 'postalcode' => 'The value :attribute must be a valid postal code.', 'mimetype' => 'The value :attribute does not contain a valid Internet Media Type (MIME-Type).', + 'austrianinsurancenumber' => 'The value :attribute does not contain a valid austrian insurance number.', ]; diff --git a/tests/Rules/AustrianInsuranceNumberTest.php b/tests/Rules/AustrianInsuranceNumberTest.php new file mode 100644 index 0000000..e1abcb5 --- /dev/null +++ b/tests/Rules/AustrianInsuranceNumberTest.php @@ -0,0 +1,34 @@ +isValid($value); + $this->assertEquals($result, $valid); + } + + public static function dataProvider(): array + { + return [ + [true, '8757080475'], + [true, '1230011471'], + [false, '1234567890'], + [false, '0123456789'], + [false, '12345'], + [false, '25bc78'], + [false, '8753080475'], + [false, 'foo'], + [true, '1230 011471'], + ]; + } +}