-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Added validation rule for austrian insurance numbers (#82)
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Validation\Rules; | ||
|
||
use Intervention\Validation\AbstractRule; | ||
|
||
class AustrianInsuranceNumber extends AbstractRule | ||
{ | ||
/** | ||
* Austrian insurance number length | ||
* | ||
* @var int | ||
*/ | ||
private int $length = 10; | ||
|
||
/** | ||
* Multiplier series to calculate checksum | ||
* https://www.sozialversicherung.at/cdscontent/?contentid=10007.820902&viewmode=content | ||
* | ||
* @var array | ||
*/ | ||
private array $multiplierSeries = [ | ||
3, 7, 9, 5, 8, 4, 2, 1, 6 | ||
]; | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid(mixed $value): bool | ||
{ | ||
$value = str_replace(' ', '', $value); | ||
|
||
return is_numeric($value) | ||
&& $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Validation\Tests\Rules; | ||
|
||
use Intervention\Validation\Rules\AustrianInsuranceNumber; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AustrianInsuranceNumberTest extends TestCase | ||
{ | ||
#[DataProvider('dataProvider')] | ||
public function testValidation($result, $value): void | ||
{ | ||
$valid = (new AustrianInsuranceNumber())->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'], | ||
]; | ||
} | ||
} |