Overview

Classes

  • RTMykad
  • Overview
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * @link https://github.com/rogertiongdev/RTMykad RTMykad GitHub project
  5:  * @license https://rogertiongdev.github.io/MIT-License/
  6:  */
  7: 
  8: /**
  9:  * Simple helper to fetch information from MyKad Number
 10:  *
 11:  * @version 0.1
 12:  * @author Roger Tiong RTdev
 13:  */
 14: class RTMykad {
 15: 
 16:     /**
 17:      * Standard MyKad Number length
 18:      *
 19:      * @var int
 20:      */
 21:     CONST MyKadNoLen = 12;
 22: 
 23:     /**
 24:      * Standard MyKad minimum age
 25:      *
 26:      * @var int
 27:      */
 28:     CONST MyKadMinAge = 12;
 29: 
 30:     private $MyKadData = NULL;
 31: 
 32:     /**
 33:      * Set MyKad Number
 34:      *
 35:      * @param string $MyKadNo
 36:      */
 37:     public function setMyKad($MyKadNo) {
 38: 
 39:         $this->MyKadData = NULL;
 40: 
 41:         $MyKadData = NULL;
 42: 
 43:         if ($this->isMyKadValid($MyKadNo)) {
 44: 
 45:             $MyKadData['no'] = $this->formatMyKad($MyKadNo);
 46: 
 47:             $result1 = $this->splitMyKad($MyKadNo);
 48: 
 49:             $MyKadData['display'] = sprintf('%s-%s-%s', $result1['head'], $result1['body'], $result1['tail']);
 50:             $MyKadData['head'] = $result1['head'];
 51:             $MyKadData['body'] = $result1['body'];
 52:             $MyKadData['tail'] = $result1['tail'];
 53: 
 54:             $result2 = $this->getBirthData($MyKadNo);
 55: 
 56:             $MyKadData['bdate'] = $result2['bdate'];
 57:             $MyKadData['byear'] = $result2['byear'];
 58:             $MyKadData['bmth'] = $result2['bmth'];
 59:             $MyKadData['bday'] = $result2['bday'];
 60:             $MyKadData['age'] = $result2['age'];
 61: 
 62:             $MyKadData['state'] = $this->getMyKadState($MyKadNo);
 63:             $MyKadData['gender'] = $this->getMyKadGender($MyKadNo);
 64: 
 65:             $this->MyKadData = $MyKadData;
 66:         }
 67:     }
 68: 
 69:     /**
 70:      * Return MyKad data
 71:      *
 72:      * @return array
 73:      */
 74:     public function getData() {
 75: 
 76:         return $this->MyKadData;
 77:     }
 78: 
 79:     /**
 80:      * Return is MyKad valid (TRUE/FALSE)
 81:      *
 82:      * @return boolean
 83:      */
 84:     public function isValid() {
 85: 
 86:         return (!empty($this->MyKadData));
 87:     }
 88: 
 89:     /**
 90:      * Remove spaces and dash from MyKad Number
 91:      *
 92:      * @param string $data
 93:      * @return string
 94:      */
 95:     public function formatMyKad($data) {
 96: 
 97:         $data = preg_replace('/\s+/', '', (string) $data);
 98: 
 99:         return str_replace('-', '', $data);
100:     }
101: 
102:     /**
103:      * Validate MyKad Format
104:      *
105:      * @param string $data
106:      * @return boolean
107:      */
108:     public function isMyKadValid($data) {
109: 
110:         $data = $this->formatMyKad($data);
111: 
112:         return (!empty($data) && is_numeric($data) && strlen($data) == self::MyKadNoLen);
113:     }
114: 
115:     /**
116:      * Split MyKad Number to 3 section:<br>
117:      * - head: xxxxxx<br>
118:      * - body: xx<br>
119:      * - tail: xxxx<br>
120:      *
121:      * @param string $data
122:      * @return array
123:      */
124:     public function splitMyKad($data) {
125: 
126:         $data = $this->formatMyKad($data);
127: 
128:         $head = substr($data, 0, 6);
129:         $body = substr($data, 6, 2);
130:         $tail = substr($data, 8, 4);
131: 
132:         return array('head' => $head, 'body' => $body, 'tail' => $tail);
133:     }
134: 
135:     /**
136:      * Return birth data<br>
137:      * - bdate: Birth date Y-m-d<br>
138:      * - byear: Birth year Y<br>
139:      * - bmth: Birth month m<br>
140:      * - bday: Birth day d<br>
141:      * - age: Current age<br>
142:      *
143:      * @param string $data
144:      * @return array
145:      */
146:     public function getBirthData($data) {
147: 
148:         $data = $this->formatMyKad($data);
149:         $head = substr($data, 0, 6);
150: 
151:         $year = (int) substr($head, 0, 2);
152:         $currentYear = date('Y');
153: 
154:         $max = (int) substr($currentYear, 2, 4) - self::MyKadMinAge;
155: 
156:         $byear = $year + ($year > $max ? 1900 : 2000);
157:         $bmth = (int) substr($head, 2, 2);
158:         $bday = (int) substr($head, 4, 2);
159: 
160:         $bdate = date('Y-m-d', strtotime(sprintf('%s-%s-%s', $byear, $bmth, $bday)));
161: 
162:         $age = $currentYear - $byear;
163: 
164:         $today = strtotime(date('Y-m-d'));
165:         $btime = strtotime($bdate);
166: 
167:         if ($today > $btime) {
168: 
169:             $age -= 1;
170:         }
171: 
172:         return array(
173:             'bdate' => $bdate,
174:             'byear' => $byear,
175:             'bmth' => $bmth,
176:             'bday' => $bday,
177:             'age' => $age
178:         );
179:     }
180: 
181:     /**
182:      * Return State
183:      *
184:      * @param string $data
185:      * @return string
186:      */
187:     public function getMyKadState($data) {
188: 
189:         $data = $this->formatMyKad($data);
190:         $body = substr($data, 6, 2);
191: 
192:         switch ($body) {
193: 
194:             case '01':
195:             case '21':
196:             case '22':
197:             case '23':
198:             case '24':
199:                 return 'Johor';
200:             case '02':
201:             case '25':
202:             case '26':
203:             case '27':
204:                 return 'Kedah';
205:             case '03':
206:             case '28':
207:             case '29':
208:                 return 'Kelantan';
209:             case '04':
210:             case '30':
211:                 return 'Melaka';
212:             case '05':
213:             case '31':
214:             case '59':
215:                 return 'Negeri Sembilan';
216:             case '06':
217:             case '32':
218:             case '33':
219:                 return 'Pahang';
220:             case '07':
221:             case '34':
222:             case '35':
223:                 return 'Penang';
224:             case '08':
225:             case '36':
226:             case '37':
227:             case '38':
228:             case '39':
229:                 return 'Perak';
230:             case '09':
231:             case '40':
232:                 return 'Perlis';
233:             case '10':
234:             case '41':
235:             case '42':
236:             case '43':
237:             case '44':
238:                 return 'Selangor';
239:             case '11':
240:             case '45':
241:             case '46':
242:                 return 'Terengganu';
243:             case '12':
244:             case '47':
245:             case '48':
246:             case '49':
247:                 return 'Sabah';
248:             case '13':
249:             case '50':
250:             case '51':
251:             case '52':
252:             case '53':
253:                 return 'Sarawak';
254:             case '14':
255:             case '54':
256:             case '55':
257:             case '56':
258:             case '57':
259:                 return 'Wilayah Persekutuan Kuala Lumpur';
260:             case '15':
261:             case '58':
262:                 return 'Wilayah Persekutuan Labuan';
263:             case '16':
264:                 return 'Wilayah Persekutuan Putrajaya';
265:             case '82':
266:             default:
267:                 return 'Others';
268:         }
269:     }
270: 
271:     /**
272:      * Return Gender
273:      *
274:      * @param string $data
275:      * @return string
276:      */
277:     public function getMyKadGender($data) {
278: 
279:         $data = $this->formatMyKad($data);
280:         $tail = substr($data, 8, 4);
281:         $last = (int) substr($tail, -1);
282: 
283:         return ($last % 2) ? 'Male' : 'Female';
284:     }
285: 
286: }
287: 
RTMykad API documentation generated by ApiGen