PHP Classes

File: core/RTMykad.php

Recommend this page to a friend!
  Classes of No name   RT PHP Mykad Reader   core/RTMykad.php   Download  
File: core/RTMykad.php
Role: Class source
Content type: text/plain
Description: Class source
Class: RT PHP Mykad Reader
Validate and get a Malasyan MyKad identity number
Author: By
Last change:
Date: 6 years ago
Size: 6,629 bytes
 

Contents

Class file image Download
<?php

/**
 * @link https://github.com/rogertiongdev/RTMykad RTMykad GitHub project
 * @license https://rogertiongdev.github.io/MIT-License/
 */

/**
 * Simple helper to fetch information from MyKad Number
 *
 * @version 0.1
 * @author Roger Tiong RTdev
 */
class RTMykad {

   
/**
     * Standard MyKad Number length
     *
     * @var int
     */
   
CONST MyKadNoLen = 12;

   
/**
     * Standard MyKad minimum age
     *
     * @var int
     */
   
CONST MyKadMinAge = 12;

    private
$MyKadData = NULL;

   
/**
     * Set MyKad Number
     *
     * @param string $MyKadNo
     */
   
public function setMyKad($MyKadNo) {

       
$this->MyKadData = NULL;

       
$MyKadData = NULL;

        if (
$this->isMyKadValid($MyKadNo)) {

           
$MyKadData['no'] = $this->formatMyKad($MyKadNo);

           
$result1 = $this->splitMyKad($MyKadNo);

           
$MyKadData['display'] = sprintf('%s-%s-%s', $result1['head'], $result1['body'], $result1['tail']);
           
$MyKadData['head'] = $result1['head'];
           
$MyKadData['body'] = $result1['body'];
           
$MyKadData['tail'] = $result1['tail'];

           
$result2 = $this->getBirthData($MyKadNo);

           
$MyKadData['bdate'] = $result2['bdate'];
           
$MyKadData['byear'] = $result2['byear'];
           
$MyKadData['bmth'] = $result2['bmth'];
           
$MyKadData['bday'] = $result2['bday'];
           
$MyKadData['age'] = $result2['age'];

           
$MyKadData['state'] = $this->getMyKadState($MyKadNo);
           
$MyKadData['gender'] = $this->getMyKadGender($MyKadNo);

           
$this->MyKadData = $MyKadData;
        }
    }

   
/**
     * Return MyKad data
     *
     * @return array
     */
   
public function getData() {

        return
$this->MyKadData;
    }

   
/**
     * Return is MyKad valid (TRUE/FALSE)
     *
     * @return boolean
     */
   
public function isValid() {

        return (!empty(
$this->MyKadData));
    }

   
/**
     * Remove spaces and dash from MyKad Number
     *
     * @param string $data
     * @return string
     */
   
public function formatMyKad($data) {

       
$data = preg_replace('/\s+/', '', (string) $data);

        return
str_replace('-', '', $data);
    }

   
/**
     * Validate MyKad Format
     *
     * @param string $data
     * @return boolean
     */
   
public function isMyKadValid($data) {

       
$data = $this->formatMyKad($data);

        return (!empty(
$data) && is_numeric($data) && strlen($data) == self::MyKadNoLen);
    }

   
/**
     * Split MyKad Number to 3 section:<br>
     * - head: xxxxxx<br>
     * - body: xx<br>
     * - tail: xxxx<br>
     *
     * @param string $data
     * @return array
     */
   
public function splitMyKad($data) {

       
$data = $this->formatMyKad($data);

       
$head = substr($data, 0, 6);
       
$body = substr($data, 6, 2);
       
$tail = substr($data, 8, 4);

        return array(
'head' => $head, 'body' => $body, 'tail' => $tail);
    }

   
/**
     * Return birth data<br>
     * - bdate: Birth date Y-m-d<br>
     * - byear: Birth year Y<br>
     * - bmth: Birth month m<br>
     * - bday: Birth day d<br>
     * - age: Current age<br>
     *
     * @param string $data
     * @return array
     */
   
public function getBirthData($data) {

       
$data = $this->formatMyKad($data);
       
$head = substr($data, 0, 6);

       
$year = (int) substr($head, 0, 2);
       
$currentYear = date('Y');

       
$max = (int) substr($currentYear, 2, 4) - self::MyKadMinAge;

       
$byear = $year + ($year > $max ? 1900 : 2000);
       
$bmth = (int) substr($head, 2, 2);
       
$bday = (int) substr($head, 4, 2);

       
$bdate = date('Y-m-d', strtotime(sprintf('%s-%s-%s', $byear, $bmth, $bday)));

       
$age = $currentYear - $byear;

       
$today = strtotime(date('Y-m-d'));
       
$btime = strtotime($bdate);

        if (
$today > $btime) {

           
$age -= 1;
        }

        return array(
           
'bdate' => $bdate,
           
'byear' => $byear,
           
'bmth' => $bmth,
           
'bday' => $bday,
           
'age' => $age
       
);
    }

   
/**
     * Return State
     *
     * @param string $data
     * @return string
     */
   
public function getMyKadState($data) {

       
$data = $this->formatMyKad($data);
       
$body = substr($data, 6, 2);

        switch (
$body) {

            case
'01':
            case
'21':
            case
'22':
            case
'23':
            case
'24':
                return
'Johor';
            case
'02':
            case
'25':
            case
'26':
            case
'27':
                return
'Kedah';
            case
'03':
            case
'28':
            case
'29':
                return
'Kelantan';
            case
'04':
            case
'30':
                return
'Melaka';
            case
'05':
            case
'31':
            case
'59':
                return
'Negeri Sembilan';
            case
'06':
            case
'32':
            case
'33':
                return
'Pahang';
            case
'07':
            case
'34':
            case
'35':
                return
'Penang';
            case
'08':
            case
'36':
            case
'37':
            case
'38':
            case
'39':
                return
'Perak';
            case
'09':
            case
'40':
                return
'Perlis';
            case
'10':
            case
'41':
            case
'42':
            case
'43':
            case
'44':
                return
'Selangor';
            case
'11':
            case
'45':
            case
'46':
                return
'Terengganu';
            case
'12':
            case
'47':
            case
'48':
            case
'49':
                return
'Sabah';
            case
'13':
            case
'50':
            case
'51':
            case
'52':
            case
'53':
                return
'Sarawak';
            case
'14':
            case
'54':
            case
'55':
            case
'56':
            case
'57':
                return
'Wilayah Persekutuan Kuala Lumpur';
            case
'15':
            case
'58':
                return
'Wilayah Persekutuan Labuan';
            case
'16':
                return
'Wilayah Persekutuan Putrajaya';
            case
'82':
            default:
                return
'Others';
        }
    }

   
/**
     * Return Gender
     *
     * @param string $data
     * @return string
     */
   
public function getMyKadGender($data) {

       
$data = $this->formatMyKad($data);
       
$tail = substr($data, 8, 4);
       
$last = (int) substr($tail, -1);

        return (
$last % 2) ? 'Male' : 'Female';
    }

}