-
Notifications
You must be signed in to change notification settings - Fork 1
/
NIK-Translator.php
187 lines (161 loc) · 6.66 KB
/
NIK-Translator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
class NIKTranslator
{
// Get current year and get the last 2 digit numbers
function getCurrentYear() {
return (int)date('y');
}
// Get year in NIK
function getNIKYear($nik) {
return (int)substr($nik, 10, 2);
}
// Get date in NIK
function getNIKDate($nik) {
return (int)substr($nik, 6, 2);
}
function getNIKDateFull($nik, $isFemale) {
$date = (int)substr($nik, 6, 2);
if($isFemale) $date -= 40;
return ($date > 10) ? $date : '0'.$date;
}
// Get subdistrict split postal code
function getSubdistrictPostalCode($nik, $location) {
return explode(' -- ', $location['kecamatan'][substr($nik, 0, 6)]);
}
// Get province in NIK
function getProvince($nik, $location) {
return $location['provinsi'][substr($nik, 0, 2)];
}
// Get city in NIK
function getCity($nik, $location) {
return $location['kabkot'][substr($nik, 0, 4)];
}
// Get NIK gender
function getGender($date) {
return ($date > 40) ? 'PEREMPUAN' : 'LAKI-LAKI';
}
// Get born month
function getBornMonth($nik) {
return (int)substr($nik, 8, 2);
}
function getBornMonthFull($nik) {
return substr($nik, 8, 2);
}
// Get born year
function getBornYear($nikYear, $currentYear) {
return ($nikYear < $currentYear)
? (($nikYear > 10) ? '20'.$nikYear : '200'.$nikYear)
: (($nikYear > 10) ? '19'.$nikYear : '190'.$nikYear);
}
// Get unique code in NIK
function getUniqueCode($nik) {
return substr($nik, 12, 4);
}
// Get age from NIK
function getAge($birthday) {
date_default_timezone_set('Asia/Jakarta');
$diff = date_diff(date_create($birthday), date_create(date('Y-m-d')));
return [
'years' => $diff->y,
'months' => $diff->m,
'days' => $diff->d,
];
}
// Get next birthday
function getNextBirthday($birthday) {
date_default_timezone_set('Asia/Jakarta');
$date = explode('-', date('Y-m-d'));
$birth = explode('-', $birthday);
if($date[1] == $birth[1] && $date[2] <= $birth[2]) $date[0] += 1;
$births = $date[0].substr($birthday, -6);
$diff = date_diff(date_create(date('Y-m-d')), date_create($births));
$y = ($diff->invert) ? -1*$diff->y : $diff->y;
$m = ($diff->invert) ? -1*$diff->m : $diff->m;
$d = ($diff->invert) ? -1*$diff->d : $diff->d;
$txt = '';
if($y != 0) $txt .= "$y tahun ";
if($m != 0) $txt .= "$m bulan ";
if($d != 0) $txt .= "$d hari ";
$txt .= 'lagi';
return [
'text' => $txt,
'year' => $y,
'month' => $m,
'day' => $d,
];
}
// Get zodiac from bornDate and bornMonth
function getZodiac($date, $month, $isFemale) {
if($isFemale) $date -= 40;
if(($month == 1 && $date >= 20) || ($month == 2 && $date < 19)) return 'Aquarius';
if(($month == 2 && $date >= 19) || ($month == 3 && $date < 21)) return 'Pisces';
if(($month == 3 && $date >= 21) || ($month == 4 && $date < 20)) return 'Aries';
if(($month == 4 && $date >= 20) || ($month == 5 && $date < 21)) return 'Taurus';
if(($month == 5 && $date >= 21) || ($month == 6 && $date < 22)) return 'Gemini';
if(($month == 6 && $date >= 21) || ($month == 7 && $date < 23)) return 'Cancer';
if(($month == 7 && $date >= 23) || ($month == 8 && $date < 23)) return 'Leo';
if(($month == 8 && $date >= 23) || ($month == 9 && $date < 23)) return 'Virgo';
if(($month == 9 && $date >= 23) || ($month == 10 && $date < 24)) return 'Libra';
if(($month == 10 && $date >= 24) || ($month == 11 && $date < 23)) return 'Scorpio';
if(($month == 11 && $date >= 23) || ($month == 12 && $date < 22)) return 'Sagitarius';
if(($month == 12 && $date >= 22) || ($month == 1 && $date < 19)) return 'Capricorn';
return 'Zodiak tidak ditemukan';
}
function parse($nik) {
$location = $this->getLocationAsset();
// Check NIK and make sure is correct
if($this->validate($nik)) {
$currentYear = $this->getCurrentYear();
$nikYear = $this->getNIKYear($nik);
$nikDate = $this->getNIKDate($nik);
$gender = $this->getGender($nikDate);
$nikDateFull = $this->getNIKDateFull($nik, $gender == 'PEREMPUAN');
$subdistrictPostalCode = $this->getSubdistrictPostalCode($nik, $location);
$province = $this->getProvince($nik, $location);
$city = $this->getCity($nik, $location);
$subdistrict = $subdistrictPostalCode[0];
$postalCode = $subdistrictPostalCode[1];
$bornMonth = $this->getBornMonth($nik);
$bornMonthFull = $this->getBornMonthFull($nik);
$bornYear = $this->getBornYear($nikYear, $currentYear);
$uniqueCode = $this->getUniqueCode($nik);
$zodiac = $this->getZodiac($nikDate, $bornMonth, $gender == 'PEREMPUAN');
$age = $this->getAge("$bornYear-$bornMonthFull-$nikDateFull");
$nextBirthday = $this->getNextBirthday("$bornYear-$bornMonthFull-$nikDateFull");
return [
'nik' => $nik ?? '',
'uniqueCode' => $uniqueCode ?? '',
'gender' => $gender ?? '',
'bornDate' => "$nikDateFull-$bornMonthFull-$bornYear" ?? '',
'age' => [
'text' => $age['years'].' tahun '.$age['months'].' bulan '.$age['days'].' hari',
'year' => $age['years'],
'month' => $age['months'],
'days' => $age['days']
],
'nextBirthday' => $nextBirthday,
'zodiac' => $zodiac ?? '',
'province' => $province ?? '',
'city' => $city ?? '',
'subdistrict' => $subdistrict ?? '',
'postalCode' => $postalCode ?? ''
];
} else {
return false;
}
}
// Validate NIK and make sure the number is correct
function validate($nik) {
$loc = $this->getLocationAsset();
return strlen($nik) == 16 &&
$loc['provinsi'][substr($nik, 0, 2)] != null &&
$loc['kabkot'][substr($nik, 0, 4)] != null &&
$loc['kecamatan'][substr($nik, 0, 6)] != null;
}
// Load location assets like province, city, and subdistricts
// from local json data
function getLocationAsset() {
$result = file_get_contents('wilayah.json');
return json_decode($result, true);
}
}