akukoder / mykad
A package to deal with MyKad or MyKid format, including validation and Faker provider.
Requires
- php: ^7.4|^8.0|^8.1
- nesbot/carbon: ^2.53.1
Requires (Dev)
- orchestra/testbench: ^5.2
- phpunit/phpunit: ^9.1
README
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:
- Data extraction
- Input validation
- 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:
- Date of Birth
- State name
- 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
- https://www.jpn.gov.my/en/faq/faq-identity-card
- https://hofmannsven.com/2021/faker-provider-in-laravel
License
The MIT License (MIT).