farzai/thai-citizen-id-validation

Thai citizen id validation

1.3.0 2024-10-17 08:23 UTC

This package is auto-updated.

Last update: 2024-10-17 08:24:13 UTC


README

Latest Version on Packagist Tests codecov Total Downloads

Thai Citizen ID validator is a easy-to-use PHP library designed to help validate Thai National Identification Card numbers

Installation

You can install the package via composer:

composer require farzai/thai-citizen-id-validation

Usage for validation

Example usage for validating Thai Citizen ID

use Farzai\ThaiIdValidation\Validator;
use Farzai\ThaiIdValidation\Exceptions\InvalidThaiCitizenIdException;

$validator = new Validator();

try {
    $validator->validate('1410100100000');
} catch (InvalidThaiCitizenIdException $e) {
    // Handle invalid citizen id
}

Usage for generating

Sometimes you need to generate a random Thai Citizen ID for testing purpose. This library can help you with that.

use Farzai\ThaiIdValidation\Generator;

$generator = new Generator();

// Optional
$generator
    ->personType(1) // ประเภทบุคคล
    ->provinceOfBirth('10') // เลขจังหวัดที่เกิด
    ->districtOfBirth('10'); // เลขอำเภอที่เกิด

// Generate
$idCard = $generator->generate();

echo (string)$idCard; // 1410100100000

echo $idCard->getId(); // 1410100100000
echo $idCard->getType(); // 1 digit
echo $idCard->getProvinceOfBirth(); // 2 digit
echo $idCard->getDistrictOfBirth(); // 2 digit
echo $idCard->getVolume(); // 5 digit
echo $idCard->getNumber(); // 2 digit
echo $idCard->getCheckDigit(); // 1 digit

Usage with Laravel

You can use the IdCard rule to validate Thai Citizen ID in Laravel.

use Farzai\ThaiIdValidation\Laravel\Rules\IdCard;

$request->validate([
    'id_card' => ['required', new IdCard],
]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.