Installation
composer require fikrimastor/mykad
No additional setup is required unless you need custom state mappings (publish config).
First Use Case: Validation
use Fikrimastor\Mykad\Facades\Mykad;
$isValid = Mykad::isValid('123456-01-5678'); // true/false
Where to Look First
Fikrimastor\Mykad\Facades\Mykad (primary entry point).isValid(), parse(), format(), getState().config('mykad.states-code') (published via vendor:publish).use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'mykad' => 'required|mykad', // Uses package's validation rule
]);
mykad as a Laravel validation rule.$parsed = Mykad::parse('123456-01-5678');
// Returns:
// [
// 'number' => '123456',
// 'state' => 'Johor',
// 'district' => '01',
// 'birth_year' => '56',
// 'check_digit' => '8',
// ]
$formatted = Mykad::format('123456015678'); // '123456-01-5678'
$state = Mykad::getState('01'); // 'Johor'
$district = Mykad::getDistrict('123456-01-5678'); // '01'
$age = Mykad::getAge('123456-01-5678'); // Age based on birth year (56)
mykad rule in Form Requests or manually.protected $casts = [
'mykad_number' => MykadCast::class, // Hypothetical; extend package if needed
];
return response()->json(['mykad' => Mykad::format($user->mykad)]);
False Positives in Validation
123456-01-5678) but not the check digit logic.State Code Ambiguity
01 for Johor and 01 for Kuala Lumpur in older MyKads).Mykad::parse() to resolve ambiguity via full number parsing.Birth Year Calculation
56 in 123456-01-5678).00 for 19XX).Hyphen Sensitivity
-) for parsing. Inputs like 123456015678 must be formatted first:
Mykad::parse(Mykad::format('123456015678')); // Force hyphenation
NNNNNN-DD-YYCC).
Mykad::parse('invalid'); // Throws \InvalidArgumentException
config('mykad.states-code') matches your use case (e.g., historical vs. current codes).Custom State Mappings Override the published config to add/remove states:
'states-code' => [
'99' => 'Custom State', // Add new entries
],
Check Digit Validation Extend the package to validate the check digit (modulo 11 algorithm):
// In a service or trait:
public function isValidWithCheckDigit(string $mykad): bool {
$parsed = Mykad::parse($mykad);
$checkDigit = $parsed['check_digit'];
$calculated = $this->calculateCheckDigit($parsed['number'] . $parsed['district'] . $parsed['birth_year']);
return $checkDigit == $calculated;
}
Age Calculation Enhancement
Improve getAge() to handle century ambiguity:
public function getAge(string $mykad): int {
$birthYear = Mykad::parse($mykad)['birth_year'];
$currentYear = now()->year;
return $birthYear >= 50 ? $currentYear - 1900 - $birthYear : $currentYear - 2000 - $birthYear;
}
Validation Rule Customization
Register a custom rule in AppServiceProvider:
use Fikrimastor\Mykad\Rules\Mykad as MykadRule;
Validator::extend('strict_mykad', function ($attribute, $value, $parameters) {
return (new MykadRule)->passes('strict', $value);
});
How can I help you explore Laravel packages today?