PHPackages                             akukoder/mykad - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. akukoder/mykad

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

akukoder/mykad
==============

A package to deal with MyKad or MyKid format, including validation and Faker provider.

1.0.7(2mo ago)1413[1 issues](https://github.com/akukoder/mykad/issues)MITPHPPHP ^7.4|^8.0|^8.1

Since Mar 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/akukoder/mykad)[ Packagist](https://packagist.org/packages/akukoder/mykad)[ Docs](https://github.com/akukoder)[ RSS](/packages/akukoder-mykad/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (9)Used By (0)

MyKad
=====

[](#mykad)

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

About MyKad
-----------

[](#about-mykad)

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

Introduction
------------

[](#introduction)

This package provides:

1. Data extraction
2. Input validation
3. [Faker Provider](https://faker.readthedocs.io/en/master/providers.html) to generate fake MyKad number (for Laravel)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require akukoder/mykad
```

Data Extraction
---------------

[](#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

[](#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

[](#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

[](#state)

Get state name from the input.

```
use AkuKoder\MyKad\Extractor as MyKadExtractor;

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

// Result: Selangor
```

Validation
----------

[](#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

[](#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

[](#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
--------------

[](#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.

```
