diff --git a/src/SouthAfricanIdValidator.php b/src/SouthAfricanIdValidator.php index 72a1688..82d9627 100644 --- a/src/SouthAfricanIdValidator.php +++ b/src/SouthAfricanIdValidator.php @@ -7,10 +7,12 @@ class SouthAfricanIdValidator { - private const GENDER_MALE_MIN = 5000; + private const GENDER_MALE_MAX = 9999; + private const GENDER_FEMALE_MIN = 0; + private const GENDER_FEMALE_MAX = 4999; /** @@ -21,6 +23,7 @@ class SouthAfricanIdValidator public function isValid(mixed $idNumber): bool { $idNumber = $this->trimWhiteSpaces($idNumber); + return ! (! $this->isLength13($idNumber) || ! $this->isNumber($idNumber) || ! $this->passesLuhnCheck($idNumber)); } @@ -50,6 +53,7 @@ private function extractGenderDigits(string $idNumber): int public function isMale(string $idNumber): bool { $genderDigits = $this->extractGenderDigits($idNumber); + return $genderDigits >= self::GENDER_MALE_MIN && $genderDigits <= self::GENDER_MALE_MAX; } @@ -57,6 +61,7 @@ public function isMale(string $idNumber): bool public function isFemale(string $idNumber): bool { $genderDigits = $this->extractGenderDigits($idNumber); + return $genderDigits >= self::GENDER_FEMALE_MIN && $genderDigits <= self::GENDER_FEMALE_MAX; } @@ -64,6 +69,7 @@ public function isFemale(string $idNumber): bool public function isSACitizen($idNumber): bool { $idNumber = $this->trimWhiteSpaces($idNumber); + // The 11th digit (index 10) indicates citizenship status return $idNumber[10] == '0'; } @@ -72,6 +78,7 @@ public function isSACitizen($idNumber): bool public function isPermanentResident($idNumber): bool { $idNumber = $this->trimWhiteSpaces($idNumber); + // The 11th digit (index 10) indicates citizenship status return $idNumber[10] == '1'; } @@ -80,7 +87,7 @@ public function isPermanentResident($idNumber): bool public function parse($idNumber): array { $idNumber = $this->trimWhiteSpaces($idNumber); - if (!$this->isValid($idNumber)) { + if (! $this->isValid($idNumber)) { throw new \InvalidArgumentException('Invalid ID number'); } @@ -113,6 +120,7 @@ public function parse($idNumber): array private function trimWhiteSpaces(mixed $idNumber): string { $idNumber = trim((string) $idNumber); // Remove spaces around the string + return str_replace(' ', '', $idNumber); // Remove spaces within the string } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 77560a8..ff118d2 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -46,13 +46,10 @@ expect($this->validator->isValid($this->id))->toBeTrue(); }); - it('can validate list of ID', function ($value) { expect($this->validator->isValid($this->id))->toBeTrue(); })->with(['690124 0689 08 6', '8907290565082', '7809090453082', 9108200519082, '9006010051082']); - - it('parses a valid ID number', function () { $result = $this->validator->parse($this->id); expect($result['valid'])->toBeTrue() @@ -62,24 +59,22 @@ ->and($result['citizenship'])->toBeString(); }); - -it('should equal date of ISO from', function() { +it('should equal date of ISO from', function () { $result = $this->validator->parse($this->id); expect($result['birthday']['iso'])->toEqual('1992-12-23'); }); -it('should equal date of american from', function() { +it('should equal date of american from', function () { $result = $this->validator->parse($this->id); expect($result['birthday']['american'])->toEqual('12/23/1992'); }); -it('should equal date of european from', function() { +it('should equal date of european from', function () { $result = $this->validator->parse($this->id); expect($result['birthday']['european'])->toEqual('23/12/1992'); }); -it('should equal date of long format from', function() { +it('should equal date of long format from', function () { $result = $this->validator->parse($this->id); expect($result['birthday']['long'])->toEqual('December 23, 1992'); }); -