Cake\Localized\Validation\FiValidation::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)
    {
        $pattern = '/^[0-9]{6}[-+A][0-9A-Z]{4}$/';
        if (!(bool) preg_match($pattern, $check)) {
            return false;
        }
        $date = \DateTime::createFromFormat('dmy', substr($check, 0, 6));
        if ($date === false) {
            return false;
        }
        $list = array_merge(range(0, 9), range('A', 'Y'));
        foreach ($list as $key => $item) {
            if (in_array($item, ['G', 'I', 'O', 'Q'], true)) {
                unset($list[$key]);
            }
            $list[$key] = (string) $item;
        }
        $list = array_values($list);
        return $check[strlen($check) - 1] === $list[intval(substr($check, 0, 6) . substr($check, 7, 3)) % 31];
    }

Usage Example

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