pnm1231/nic-parser

斯里兰卡国民身份证号码验证器

v1.2.2 2023-06-10 07:24 UTC

This package is auto-updated.

Last update: 2024-09-10 10:54:02 UTC


README

一个PHP库,用于解析、验证和生成有效的斯里兰卡国民身份证号码。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

安装

您可以使用composer轻松安装此库。需要PHP 7.4或更高版本。

composer require pnm1231/nic-parser

或者,您可以从GitHub下载库,并手动包含类或将其集成到自己的自动加载器中。请参阅包含的composer.json文件以了解PSR-4命名空间映射。

使用方法

有关使用格式的信息,请参阅维基百科文章

解析ID号码

<?php

use pnm1231\NICParser\Parser;

require_once __DIR__ . '/../vendor/autoload.php';

/**
 * Example 1
 */
$idNumber = '862348594v';

$parser = new Parser($idNumber);
$parser->getBirthday();// Returns a \DateTime object with the date parsed.

echo $parser->getBirthday()->format('Y-m-d'); // prints "1986-08-22"
echo $parser->getGender(); // Prints "M". M for male, F for female.
echo $parser->getSerialNumber(); // Prints "8594"

/**
 * Example 2
 */
$idNumber = '19935158154';

$parser = new Parser($idNumber);
$parser->getBirthday();// Returns a \DateTime object with the date parsed.

echo $parser->getBirthday()->format('Y-m-d'); // prints "1993-01-15"
echo $parser->getGender(); // Prints "F". M for male, F for female.
echo $parser->getSerialNumber(); // Prints "8154"

验证ID号码

当您使用无效的ID号码实例化Parser类时,该类会抛出异常。请确保您始终在验证时捕获异常。

<?php
use pnm1231\NICParser\Parser;
use pnm1231\NICParser\Exception\InvalidArgumentException;

require_once __DIR__ . '/../vendor/autoload.php';

/* This is an invalid ID number because 499 here is not indicating a valid
birth date */
$idNumber = '924998593v';

try {
  $parser = new Parser($idNumber);
}
catch (\pnm1231\NICParser\Exception\InvalidArgumentException $exception) {
  echo $exception->getMessage(); // "Birthday indicator is invalid."
}

根据验证错误,您将获得不同的消息来解释情况。所有异常都将为pnm1231\NICParser\Exception\InvalidArgumentException的实例。

构建NIC号码

<?php

use pnm1231\NICParser\Builder;

require_once __DIR__ . '/../vendor/autoload.php';

$birthday = new \DateTime();
$birthday->setDate(1992, 9, 16);
$birthday->setTime(0, 0);

$builder = new Builder();
$builder->setBirthday($birthday);
$builder->setGender('M'); // M for male, F for female.
$builder->setSerialNumber(25738);

echo $builder->getNumber(); // "199226025738". This is the new format.

贡献

欢迎所有贡献。如果您有任何问题,请在GitHub上发布一个问题。对于任何PR,我们非常感谢您添加适当的测试覆盖率。

其他实现