Cake\Localized\Validation\LvValidation::personId PHP Method

personId() public static method

Checks a country specific identification number.
public static personId ( string $check ) : boolean
$check string The value to check.
return boolean Success.
    public static function personId($check)
    {
        $check = trim(str_replace('-', '', $check));
        $pattern = '/^[0-9]{6}[0-9A-Z]{5}$/';
        if (!(bool) preg_match($pattern, $check)) {
            return false;
        }
        $date = \DateTime::createFromFormat('dmy', substr($check, 0, 6));
        if ($date === false) {
            return false;
        }
        $counter = 0;
        $map = [0 => 1, 1 => 6, 2 => 3, 3 => 7, 4 => 9, 5 => 10, 6 => 5, 7 => 8, 8 => 4, 9 => 2];
        foreach ($map as $index => $multiplier) {
            $counter += (int) $check[$index] * $multiplier;
        }
        if ($counter === false) {
            return false;
        }
        return (1101 - $counter) % 11 === (int) $check[10];
    }

Usage Example

Example #1
0
 /**
  * Test Latvian person id
  *
  * @return void
  * @dataProvider personIdProvider
  */
 public function testPersonId($item, $assert)
 {
     $this->assertEquals($assert, LvValidation::personId($item));
 }