ilexn / hkid-check-digit
香港身份证号码校验
5.2
2023-12-07 06:34 UTC
Requires
- php: >=8.3
- ilexn/result-option: ^0.2.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.32.0
- ilexn/keep-a-change-log: ^2.0
- infection/infection: ^0.27.8
- phpbench/phpbench: 1.2.15
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: 10.5.1
- rector/rector: 0.18.12
- spatie/phpunit-watcher: ^1.23
- symplify/easy-coding-standard: ^12.0.11
- vimeo/psalm: ^5.17
This package is auto-updated.
Last update: 2024-09-19 07:58:08 UTC
README
用于验证香港身份证号码格式和校验位计算。
__ __ __ ___ __ _______ ______ __ __ _______ ______ __ ___
| | | | | |/ / | | | \ / || | | | | ____| / || |/ /
| |__| | | ' / | | | .--. | | ,----'| |__| | | |__ | ,----'| ' /
| __ | | < | | | | | | | | | __ | | __| | | | <
| | | | | . \ | | | '--' | | `----.| | | | | |____ | `----.| . \
|__| |__| |__|\__\ |__| |_______/ \______||__| |__| |_______| \______||__|\__\
安装
composer require ilexn/hkid-check-digit
从 4.x 升级到 5.x
\Ilex\Validation\HkidValidation\Reason\ReasonInterface
已更改为枚举 \Ilex\Validation\HkidValidation\Enum\Reason
//4.x switch ($hkid->getReason()){ case \Ilex\Validation\HkidValidation\Reason\ReasonInterface::OK: echo('correct'); echo($hkid->format()); break; case \Ilex\Validation\HkidValidation\Reason\ReasonInterface::PATTEN_ERROR: echo('Patten not match'); break; case \Ilex\Validation\HkidValidation\Reason\ReasonInterface::DIGIT_ERROR: echo('Digit not match'); break; } //5.x switch ($hkid->getReason()){ case \Ilex\Validation\HkidValidation\Enum\Reason::Ok: echo('correct'); echo($hkid->format()); break; case \Ilex\Validation\HkidValidation\Enum\Reason::PattenError: echo('Patten not match'); break; case \Ilex\Validation\HkidValidation\Enum\Reason::DigitError: echo('Digit not match'); break; }
从 2.x 升级到 3.x
现在所有检查都返回 \Ilex\Validation\HkidValidation\HkId
对象,而不是 bool
<?php use Ilex\Validation\HkidValidation\Helper; require_once 'vendor/autoload.php'; $a = Helper::checkByString($s); //2.x: $a is bool //>=3.x $a->isValid(); //bool $a->isPattenError(); //bool $a->isDigitError(); //bool echo($a->format()); // print the formated HKID. echo($a->getReason()); //also can get back each parts echo($a->getPart1()); echo($a->getPart2()); echo($a->getPart3());
使用示例
快速助手 - 按部分检查
<?php use Ilex\Validation\HkidValidation\Helper; require_once 'vendor/autoload.php'; //CA182361(1) $p1 = 'CA'; $p2 = '182361'; $p3 = '1'; $a = Helper::checkByParts($p1, $p2, $p3); if ($a->isValid()) { echo ('correct'); echo $a->format(); //CA182361(1) echo (string) $a; //CA182361(1) } else { echo ('wrong'); }
快速助手 - 按字符串检查
<?php use Ilex\Validation\HkidValidation\Enum\Reason;use Ilex\Validation\HkidValidation\Helper; require_once 'vendor/autoload.php'; $s = 'CA182361(1)'; $hkid = Helper::checkByString($s); switch ($hkid->getReason()){ case \Ilex\Validation\HkidValidation\Enum\Reason::Ok: echo('correct'); echo($hkid->format()); break; case \Ilex\Validation\HkidValidation\Enum\Reason::PattenError: echo('Patten not match'); break; case \Ilex\Validation\HkidValidation\Enum\Reason::DigitError: echo('Digit not match'); break;
常规
<?php use Ilex\Validation\HkidValidation\HkidDigitCheck; require_once 'vendor/autoload.php'; $p1 = 'CA'; $p2 = '182361'; $p3 = '1'; $s = 'CA182361(1)'; $c = new HkidDigitCheck(); $hkid = $c->checkParts($p1,$p2,$p3); if ($hkid->isValid()) { echo ('correct'); echo $hkid->format(); } else { echo ('wrong'); if ($hkid->isPattenError()) { echo('Patten not match'); } if ($hkid->isDigitError()) { echo('Digit not match'); } } $hkid = $c->checkString($s); if ($hkid->isValid()) { echo ('correct'); echo $hkid->format(); } else { echo ('wrong'); }