Skip to content

Commit

Permalink
Merge pull request #249 from imahmood/fix-iranian-person-id
Browse files Browse the repository at this point in the history
fixed iranian personal ID validation logic
  • Loading branch information
dereuromark committed Dec 14, 2023
2 parents 04247fd + bc389d6 commit 053f92c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/Validation/IrValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,24 @@ public static function personId(string $check): bool
}
$sum = 0;
$equivalent = 0;
$controlDigit = (int)substr($check, 9, 1);

for ($i = 0; $i < 9; $i++) {
$sum += (int)substr($check, $i, 1) * (10 - $i);
if (substr($check, 1, 1) === substr($check, $i, 1)) {
$currentDigit = (int)substr($check, $i, 1);
$sum += $currentDigit * (10 - $i);

if ($currentDigit === $controlDigit) {
$equivalent++;
}
}
if ($equivalent === 10) {
if ($equivalent === 9) {
return false;
}
$remaining = $sum % 11;
if ($remaining <= 1) {
return (bool)($remaining == substr($check, 9, 1));
return $controlDigit === $remaining;
}

return (bool)(substr($check, 9, 1) == 11 - $remaining);
return $controlDigit === 11 - $remaining;
}
}
3 changes: 2 additions & 1 deletion tests/TestCase/Validation/IrValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ public function testSsn()
$this->assertTrue(IrValidation::personId('9876543210'));
$this->assertTrue(IrValidation::personId('1234567891'));
$this->assertTrue(IrValidation::personId('0324354657'));
$this->assertTrue(IrValidation::personId('1111111121'));

$this->assertFalse(IrValidation::personId('1234567890'));
//$this->assertFalse(IrValidation::personId('3333333333'));
$this->assertFalse(IrValidation::personId('3333333333'));
$this->assertFalse(IrValidation::personId('0324354654'));
$this->assertFalse(IrValidation::personId('12345'));
}
Expand Down

0 comments on commit 053f92c

Please sign in to comment.