jontsa / national-identification-number

解析国家身份证号码。

1.0.1 2022-03-05 05:16 UTC

This package is auto-updated.

Last update: 2024-09-05 11:08:51 UTC


README

Tests

此软件包可用于解析、验证和格式化国家个人身份证号码。在一些国家,这些可能被称为社会保险号、国家保险号码等。

其他类似软件包也可用,但其中一些已不再更新,一些不与较新的PHP版本兼容,一些只支持一个国家等。创建此软件包是为了更好地满足我们的需求。它只支持有限的几个国家,但如果有描述语法,添加新国家应该很容易。

特性

  • 解析和验证国家标识符字符串
  • 返回从标识符中提取的已知属性的对象
  • 支持的国家
    • 奥地利
    • 爱沙尼亚
    • 芬兰
    • 瑞典(个人和组织号码)
    • 英国NI号码

要求

  • PHP 7.4或更高版本
  • composer

安装

请确保已全局安装Composer,如Composer文档中的安装章节所述。

打开命令行,进入您的项目目录并执行

$ composer require jontsa/national-identification-number

用法

解析和验证身份号码

use Jontsa\NationalIdentificationNumber\Exception\InvalidIdentifierExceptionInterface;
use Jontsa\NationalIdentificationNumber\Exception\InvalidSyntaxExceptionInterface;
use Jontsa\NationalIdentificationNumber\Exception\UnsupportedCountryException;
use Jontsa\NationalIdentificationNumber\Factory;
use Jontsa\NationalIdentificationNumber\IdentificationNumber\BirthDateAwareInterface;
use Jontsa\NationalIdentificationNumber\IdentificationNumber\GenderAwareInterface;

$country = 'FI';
$string = '150921A123A';
try {
    $identificationNumber = Factory::create($country, $string);
    echo "Yay, this is a valid personal identification number.\n";
    if ($identificationNumber instanceof BirthDateAwareInterface) {
        if ($identificationNumber instanceof GenderAwareInterface) {
            $pronoun = $identificationNumber->getGender() === GenderAwareInterface::GENDER_MALE ? 'He' : 'She';
        } else {
            $pronoun = 'Person'
        }
        echo $pronoun . " was born on " . $identificationNumber->getBirthDate()->format('Y-m-d') . "\n";
    }
} catch (UnsupportedCountryException $e) {
    echo 'Sorry but ' . $country . ' is not supported. Maybe you can create a ticket or PR in Github?';
} catch (InvalidSyntaxExceptionInterface|InvalidIdentifierExceptionInterface $e) {
    echo 'The supplied string was not a valid personal identification number';
}

解析特定国家身份号码的替代方法

use Jontsa\NationalIdentificationNumber\Exception\InvalidIdentifierExceptionInterface;
use Jontsa\NationalIdentificationNumber\Exception\InvalidSyntaxExceptionInterface;
use Jontsa\NationalIdentificationNumber\Factory;

try {
    $identificationNumber = Factory::FI('150921A123A');
    echo "Yay, this is a valid personal identification number.\n";
    echo 'Gender: ' . $identificationNumber->getGender() . "\n";
    echo 'Born: ' . $identificationNumber->getBirthDate() . "\n";
} catch (InvalidSyntaxExceptionInterface|InvalidIdentifierExceptionInterface $e) {
    echo 'The supplied string was not valid Finnish personal identification number';
}

计算校验和并格式化芬兰个人身份证号码

use Jontsa\NationalIdentificationNumber\IdentificationNumber\FinnishIdentificationNumber;

$identificationNumber = new FinnishIdentificationNumber('19', '-', '79', '01', '31', '082');
echo 'Correct checksum: ' . $identificationNumber->getCheckSum() . "\n";
echo 'Formatted: ' . $identificationNumber->format() . "\n";

// output
// Correct checksum: U
// Formatted: 310179-082U