Giter VIP home page Giter VIP logo

mykad's Introduction

MyKad

Extract information from MyKad/MyKid number, validate the input from user and generate Faker data.

About MyKad

The Government Multi-Purpose Smart Card Project (MPSC) or MyKad is part of the Multimedia Super Corridor (MSC Malaysia) initiative.

Introduction

This package provides:

  1. Data extraction
  2. Input validation
  3. Faker Provider to generate fake MyKad number (for Laravel)

Installation

You can install the package via composer:

composer require akukoder/mykad

Data Extraction

With this package, your can extract some information from the MyKad/MyKid:

  1. Date of Birth
  2. State name
  3. Gender

Date of birth

Get date of birth from the input.

use AkuKoder\MyKad\Extractor as MyKadExtractor;

echo (new MyKadExtractor('871003417888'))->dateOfBirth();
// Result: 1987-10-03

echo (new MyKadExtractor('871003417888'))->dateOfBirth('d/m/Y');

// Result: 03/10/1987

Gender

Get gender from the input. Basically, 1 for male and 0 for female.

use AkuKoder\MyKad\Extractor as MyKadExtractor;

echo (new MyKadExtractor('871003417888'))->gender();

// Result: 1

State

Get state name from the input.

use AkuKoder\MyKad\Extractor as MyKadExtractor;

echo (new MyKadExtractor('871003417888'))->stateName();

// Result: Selangor

Validation

One of the most annoying thing when dealing with user records is when they entered wrong MyKad/MyKid number. This package helps reduce the burden to deal with invalid input by users.

This package will validate MyKad/MyKid number to make sure:

  • Contains numbers only
  • Valid length
  • Valid date of birth
  • Valid state/country code

Note:

Any other unnecessary characters from the input will be removed, including dashes.

Usage

use AkuKoder\MyKadValidator\Validator;

// Check for invalid date
if ((new Validator)->validate('982404-06-5883')) {
    // Result: false
}

// Check for invalid length
if ((new Validator)->validate('982404-06-83')) {
    // Result: false    
}

// Check for invalid state code
if ((new Validator)->validate('980404-00-5335')) {
    // Result: false
}

// Check for invalid characters
if ((new Validator)->validate('9804AA-00-5335')) {
    // Result: false
}

// All passes
if ((new Validator)->validate('980404-06-5335')) {
    // Result: true
}

Get exception on errors

use AkuKoder\MyKadValidator\Validator;

$validator = new Validator;

if ($validator->validate('982404-06-5883', true)) {
    // This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidDateException    
}

if ($validator->validate('982404-06-83', true)) {
    // This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidLengthException    
}

if ($validator->validate('980404-00-5335', true)) {
    // This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidCodeException    
}

if ($validator->validate('9804AA-00-5335', true)) {
    // This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidCharacterException    
}

Faker Provider

To generate dummy data for your test or model factory, add these to on of your service provider or create new one. Let's create a new service provider:

php artisan make:provider FakerServiceProvider

Register MyKadProvider in register method.

<?php

namespace App\Providers;

use AkuKoder\MyKad\Faker\MyKadProvider;
use Faker\{Factory, Generator};
use Illuminate\Support\ServiceProvider;

class FakerServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register(): void
    {
        $this->app->singleton(Generator::class, function () {
            $faker = Factory::create();
            $faker->addProvider(new MyKadProvider($faker));

            return $faker;
        });
    }
}

Make sure to include the additional Laravel service provider in the /config/app.php file.

'providers' => [
    App\Providers\FakerServiceProvider::class,
],

Now you can use the new formatter like the other Faker formatters. In a Laravel factory, the syntax for the custom formatter looks like this:

public function definition(): array
{
    return [
        'ic_number' => $this->faker->mykad,
    ];
}

Testing

composer test

Credits

  1. https://www.jpn.gov.my/en/faq/faq-identity-card
  2. https://hofmannsven.com/2021/faker-provider-in-laravel

License

The MIT License (MIT).

mykad's People

Contributors

syahzul avatar

Stargazers

 avatar

Watchers

 avatar

mykad's Issues

Fingerprint matching

How to parse/read fingerprint data from MyKad and match it with live data from scanner?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.