Skip to content

Commit

Permalink
Refactor code to use intergers and strings
Browse files Browse the repository at this point in the history
  • Loading branch information
claudemyburgh committed Jun 21, 2024
1 parent 9a5e813 commit 613c945
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/SouthAfricanIdValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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));
}

Expand Down Expand Up @@ -50,20 +53,23 @@ 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;
}

// Method to determine if the ID number is for a female
public function isFemale(string $idNumber): bool
{
$genderDigits = $this->extractGenderDigits($idNumber);

return $genderDigits >= self::GENDER_FEMALE_MIN && $genderDigits <= self::GENDER_FEMALE_MAX;
}

// Method to determine if the person is a South African citizen
public function isSACitizen($idNumber): bool
{
$idNumber = $this->trimWhiteSpaces($idNumber);

// The 11th digit (index 10) indicates citizenship status
return $idNumber[10] == '0';
}
Expand All @@ -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';
}
Expand All @@ -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');
}

Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 4 additions & 9 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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');
});

0 comments on commit 613c945

Please sign in to comment.