FakerPHP / Faker

Faker is a PHP library that generates fake data for you
https://fakerphp.github.io
Other
3.49k stars 331 forks source link

Fix: lv_LV generating invalid personal identity numbers #663

Closed edgarsn closed 1 year ago

edgarsn commented 1 year ago

What is the reason for this PR?

Generated latvian personal identity number (PIN) part after dash is invalid. By implementing this, developers will have ability to work with randomly generated, valid PIN, especially if somewhere in tests they rely on a valid PIN.

Author's checklist

Summary of changes

https://en.wikipedia.org/wiki/National_identification_number#Latvia

Shortly:

In this PR all theese things are taken into account, making PIN totally valid.

About checksum digit formula

Format: ABCDEF-XGHIZ Formula: (1101-(1*A+6*B+3*C+7*D+9*E+10*F+5*X+8*G+4*H+2*I)) | Mod 11 | Mod 10

Example: Birth date: 1981-05-24 Century: 1 Serial number (random): 579 PIN without checksum (X) yet: 240581-1579X

Inserting real numbers into formula:

(1101 - (1 2 + 6 4 + 3 0 + 7 5 + 9 8 + 10 1 + 5 1 + 8 5 + 4 7 + 2 9)) | Mod 11 | Mod 10 = 9

Which makes PIN to be: 240581-15799

Review checklist

edgarsn commented 1 year ago

About failing psalm check - I don't have a experience using psalm, but seems that changing foreach loop to this fixes the check.

foreach ($partialNumberSplit as $key => $digit) {
  if (!isset($idDigitValidator[$key])) {
    continue;
  }

  $total += $idDigitValidator[$key] * (int) $digit;
}

... but is that the best way how to deal with it?

pimjansen commented 1 year ago

About failing psalm check - I don't have a experience using psalm, but seems that changing foreach loop to this fixes the check.


foreach ($partialNumberSplit as $key => $digit) {

  if (!isset($idDigitValidator[$key])) {

  continue;

  }

  $total += $idDigitValidator[$key] * (int) $digit;

}

... but is that the best way how to deal with it?

Skip the continue and make the if the other way around only execute if exists. Originally its a possible nullpointer exception so