Skip to content
/ NRIC Public

Provides a value object to store, generate and validate Singapore NRIC/FIN numbers

License

Notifications You must be signed in to change notification settings

IonBazan/NRIC

Repository files navigation

Singapore NRIC/FIN number generator and validator

Latest version GitHub Workflow Status PHP version Codecov Mutation testing badge Downloads License

It now supports new 2022 M-series FIN numbers!

This package provides a self-validating value object for storing, generating and validating Singapore NRIC and FIN numbers in PHP.

Usage

<?php

use IonBazan\NRIC\Exception\InvalidChecksumException;
use IonBazan\NRIC\Exception\InvalidFormatException;
use IonBazan\NRIC\NRIC;

$nric = NRIC::generateNric(new DateTime('1990-01-01')); // Generate a random NRIC number
$fin = NRIC::generateFin(new DateTime('1990-01-01')); // Generate a random FIN number

try {
    $invalid = NRIC::fromString('S0000001A'); // Create a self-validating (invalid) instance
} catch (InvalidChecksumException|InvalidFormatException $e) {
    var_dump('invalid');
}

$valid = NRIC::fromString('S0000001I'); // Create a self-validating instance from valid input
var_dump($valid->__toString()); // Thanks to Stringable interface