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

personId() public static method

Checks a social security number for Iran.
public static personId ( string $check ) : boolean
$check string The value to check.
return boolean Success.
    public static function personId($check)
    {
        $pattern = '/^\\d{10}$/';
        if (!preg_match($pattern, $check)) {
            return false;
        }
        $sum = 0;
        $equivalent = 0;
        for ($i = 0; $i < 9; $i++) {
            $sum += $check[$i] * (10 - $i);
            if ($check[1] == $check[$i]) {
                $equivalent++;
            }
        }
        if ($equivalent == 10) {
            return false;
        }
        $remaining = $sum % 11;
        if ($remaining <= 1) {
            return (bool) ($remaining == $check[9]);
        }
        return (bool) (11 - $remaining == $check[9]);
    }

Usage Example

Beispiel #1
0
 /**
  * test the ssn method of IrValidation
  *
  * @return void
  */
 public function testSsn()
 {
     $this->assertTrue(IrValidation::personId('9876543210'));
     $this->assertTrue(IrValidation::personId('1234567891'));
     $this->assertTrue(IrValidation::personId('0324354657'));
     $this->assertFalse(IrValidation::personId('1234567890'));
     //$this->assertFalse(IrValidation::personId('3333333333'));
     $this->assertFalse(IrValidation::personId('0324354654'));
     $this->assertFalse(IrValidation::personId('12345'));
 }