From 09c9ba2eb3b7ecc4a1fa2b03c29bc56efa037f0b Mon Sep 17 00:00:00 2001 From: Lukas Krempler Date: Wed, 5 Jun 2024 17:37:30 +0200 Subject: [PATCH] Validate birthday in austrian insurance number (#85) --- src/Rules/AustrianInsuranceNumber.php | 20 ++++++++++++++++++++ tests/Rules/AustrianInsuranceNumberTest.php | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/Rules/AustrianInsuranceNumber.php b/src/Rules/AustrianInsuranceNumber.php index 33bb181..0275ef0 100644 --- a/src/Rules/AustrianInsuranceNumber.php +++ b/src/Rules/AustrianInsuranceNumber.php @@ -37,6 +37,7 @@ public function isValid(mixed $value): bool return is_numeric($value) && $this->startsNotWithZero($value) && $this->hasValidLength($value) + && $this->hasValidBirthday($value) && $this->checkChecksum($value); } @@ -50,6 +51,25 @@ private function startsNotWithZero(string $svnumber): bool return (int) $svnumber[0] !== 0; } + private function hasValidBirthday(string $svnumber): bool + { + $splittedBirthday = str_split(substr($svnumber, 4), 2); + + if (!in_array((int) $splittedBirthday[0], range(1, 31), true)) { + return false; + } + + if (!in_array((int) $splittedBirthday[1], range(1, 20), true)) { + return false; + } + + if (!in_array((int) $splittedBirthday[2], range(0, 99), true)) { + return false; + } + + return true; + } + private function checkChecksum(string $svnumber): bool { if (strlen($svnumber) !== $this->length) { diff --git a/tests/Rules/AustrianInsuranceNumberTest.php b/tests/Rules/AustrianInsuranceNumberTest.php index e1abcb5..5a28aca 100644 --- a/tests/Rules/AustrianInsuranceNumberTest.php +++ b/tests/Rules/AustrianInsuranceNumberTest.php @@ -29,6 +29,10 @@ public static function dataProvider(): array [false, '8753080475'], [false, 'foo'], [true, '1230 011471'], + [false, '9999999999'], + [false, '9999202501'], + [false, '9999009901'], + [true, '1680250250'], ]; } }