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

personId() public static method

Checks a social security number (NIP) for Poland.
public static personId ( string $check ) : boolean
$check string Value to check
return boolean Success.
    public static function personId($check)
    {
        $pattern = '/^([0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2})|([0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3})|([0-9]{10})$/';
        if (!preg_match($pattern, $check)) {
            return false;
        }
        $sum = 0;
        $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
        $check = str_replace('-', '', $check);
        for ($i = 0; $i < 9; $i++) {
            $sum += $check[$i] * $weights[$i];
        }
        $control = $sum % 11;
        if ($control == 10) {
            $control = 0;
        }
        if ($check[9] == $control) {
            return true;
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * test the ssn method of PlValidation
  *
  * @return void
  */
 public function testSsn()
 {
     $this->assertTrue(PlValidation::personId('768-000-24-66'));
     $this->assertTrue(PlValidation::personId('768-00-02-466'));
     $this->assertTrue(PlValidation::personId('7680002466'));
     $this->assertFalse(PlValidation::personId('768-000-24-65'));
     $this->assertFalse(PlValidation::personId('769-000-24-66'));
     $this->assertFalse(PlValidation::personId('7680002566'));
 }