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

pesel() public static method

Checks PESEL Universal Electronic System for Registration of the Population in Poland
public static pesel ( string $check ) : boolean
$check string Value to check
return boolean Success.
    public static function pesel($check)
    {
        $pattern = '/^[0-9]{11}$/';
        if (preg_match($pattern, $check)) {
            $sum = 0;
            $weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
            for ($i = 0; $i < 10; $i++) {
                $sum += $check[$i] * $weights[$i];
            }
            $control = 10 - $sum % 10;
            if ($control == 10) {
                $control = 0;
            }
            if ($check[10] == $control) {
                return true;
            }
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * Test the pesel method of PlValidation
  *
  * @return void
  */
 public function testPesel()
 {
     $this->assertTrue(PlValidation::pesel('49040501580'));
     $this->assertFalse(PlValidation::pesel('49040501680'));
     $this->assertFalse(PlValidation::pesel('49040501581'));
 }