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

personId() public static method

Compliant with GB11643-1999 national standard.
public static personId ( string $check ) : boolean
$check string The value to check.
return boolean Success.
    public static function personId($check)
    {
        if (strlen($check) !== 18) {
            return false;
        }
        $sum = 0;
        $weight = 1;
        for ($i = 16; $i >= 0; $i--) {
            $weight = $weight * 2 % 11;
            $a = (int) $check[$i];
            if ($a < 0 || $a > 9) {
                return false;
            }
            $sum += $a * $weight;
        }
        if (strtolower($check[17]) === 'x') {
            $checksum = 10;
        } else {
            $checksum = (int) $check[17];
            if ($checksum < 0 || $checksum > 9) {
                return false;
            }
        }
        return $checksum === (12 - $sum % 11) % 11;
    }

Usage Example

Beispiel #1
0
 /**
  * test the personId method of CnValidation
  *
  * @return void
  */
 public function testPersonId()
 {
     $this->assertTrue(CnValidation::personId('361181198507131951'));
     $this->assertTrue(CnValidation::personId('361181197902271319'));
     $this->assertTrue(CnValidation::personId('36118119780214411X'));
     $this->assertTrue(CnValidation::personId('632321198701161557'));
     $this->assertFalse(CnValidation::personId('123'));
     $this->assertFalse(CnValidation::personId('1632321198701161557'));
     $this->assertFalse(CnValidation::personId('X12312412412431233'));
     $this->assertFalse(CnValidation::personId('361181198507131952'));
     $this->assertFalse(CnValidation::personId('36118119790227131X'));
     $this->assertFalse(CnValidation::personId('361181197802144119'));
 }