3
3
namespace ZerosDev \NikReader ;
4
4
5
5
use DateTime ;
6
- use Exception ;
7
6
8
7
class Reader
9
8
{
@@ -12,15 +11,28 @@ class Reader
12
11
private static $ database ;
13
12
private static $ filemtime ;
14
13
14
+ public $ province_id ;
15
+ public $ province ;
16
+ public $ city_id ;
17
+ public $ city ;
18
+ public $ subdistrict_id ;
19
+ public $ subdistrict ;
20
+ public $ postal_code ;
21
+ public $ birthday ;
22
+ public $ gender ;
23
+ public $ unique_id ;
24
+
15
25
/**
16
26
* Constructor.
17
27
*
18
- * @param string $nik
28
+ * @param string|null $nik
19
29
* @param string|null $database
20
30
*/
21
- public function __construct (string $ nik , string $ database = null )
31
+ public function __construct (string $ nik = null , string $ database = null )
22
32
{
23
- $ this ->setNik ($ nik );
33
+ if (! is_null ($ nik )) {
34
+ $ this ->setNik ($ nik );
35
+ }
24
36
25
37
$ database = $ database ?? dirname (__DIR__ ) . '/database/database.json ' ;
26
38
@@ -30,13 +42,11 @@ public function __construct(string $nik, string $database = null)
30
42
/**
31
43
* Cek validitas nomor NIK.
32
44
*
33
- * @param string $nik
34
- *
35
45
* @return bool
36
46
*/
37
- public function isValid (string $ nik )
47
+ public function isValid ()
38
48
{
39
- return is_numeric ( $ nik ) && strlen ($ nik ) === 16 ;
49
+ return is_string ( $ this -> nik ) && strlen ($ this -> nik ) === 16 ;
40
50
}
41
51
42
52
/**
@@ -48,10 +58,11 @@ public function setNik(string $nik)
48
58
{
49
59
$ this ->nik = $ nik ;
50
60
51
- if (! $ this ->isValid ($ nik )) {
61
+ if (! $ this ->isValid ()) {
52
62
throw new Exceptions \InvalidNikNumberException (sprintf (
53
- 'NIK number should be a 16-digit numeric string. Got: %s ' ,
54
- gettype ($ nik )
63
+ 'NIK number should be a 16-digit numeric string. Got: %s (%d) ' ,
64
+ gettype ($ nik ),
65
+ strlen ($ nik )
55
66
));
56
67
}
57
68
@@ -95,51 +106,78 @@ public function setDatabase(string $file)
95
106
*
96
107
* @return string|null
97
108
*/
98
- public function getProvinsi ()
109
+ public function getProvince ()
99
110
{
100
- $ code = substr ($ this ->nik , 0 , 2 );
111
+ $ this -> province_id = substr ($ this ->nik , 0 , 2 );
101
112
102
- return static ::$ database ->provinsi ->{$ code } ?? null ;
113
+ return $ this -> province = ( static ::$ database ->provinsi ->{$ this -> province_id } ?? null ) ;
103
114
}
104
115
105
116
/**
106
117
* Get data kabupaten/kota dari NIK.
107
118
*
108
119
* @return string|null
109
120
*/
110
- public function getKabupatenKota ()
121
+ public function getCity ()
111
122
{
112
- $ code = substr ($ this ->nik , 0 , 4 );
123
+ $ this -> city_id = substr ($ this ->nik , 0 , 4 );
113
124
114
- return static ::$ database ->kabkot ->{$ code } ?? null ;
125
+ return $ this -> city = ( static ::$ database ->kabkot ->{$ this -> city_id } ?? null ) ;
115
126
}
116
127
117
128
/**
118
129
* Get data kecamatan dari NIK.
119
130
*
120
131
* @return string|null
121
132
*/
122
- public function getKecamatan ()
133
+ public function getSubdistrict ()
134
+ {
135
+ $ this ->subdistrict_id = substr ($ this ->nik , 0 , 6 );
136
+
137
+ $ this ->subdistrict = (static ::$ database ->kecamatan ->{$ this ->subdistrict_id } ?? null );
138
+
139
+ if (! is_null ($ this ->subdistrict )) {
140
+ $ this ->subdistrict = explode (' -- ' , $ this ->subdistrict );
141
+ $ this ->subdistrict = isset ($ this ->subdistrict [0 ]) ? $ this ->subdistrict [0 ] : null ;
142
+ }
143
+
144
+ return $ this ->subdistrict ;
145
+ }
146
+
147
+ /**
148
+ * Get kode pos
149
+ *
150
+ * @return string|null
151
+ */
152
+ public function getPostalCode ()
123
153
{
124
154
$ code = substr ($ this ->nik , 0 , 6 );
125
155
126
- return static ::$ database ->kecamatan ->{$ code } ?? null ;
156
+ $ subdistrict = (static ::$ database ->kecamatan ->{$ code } ?? null );
157
+
158
+ if (! is_null ($ subdistrict )) {
159
+ $ subdistrict = explode (' -- ' , $ subdistrict );
160
+ $ this ->postal_code = isset ($ subdistrict [1 ]) ? $ subdistrict [1 ] : null ;
161
+ }
162
+
163
+ return $ this ->postal_code ;
127
164
}
128
165
129
166
/**
130
167
* Get data tanggal lahir dari NIK.
131
168
*
132
169
* @return string|null
133
170
*/
134
- public function getTanggalLahir ()
171
+ public function getBirthday ()
135
172
{
136
173
$ code = substr ($ this ->nik , 6 , 6 );
137
174
list ($ day , $ month , $ year ) = str_split ($ code , 2 );
138
175
139
- $ day = ((( int ) $ day - 40 ) > 0 ) ? ($ day - 40 ) : $ day ;
176
+ $ day = ((int ) $ day > 40 ) ? ($ day - 40 ) : $ day ;
140
177
141
178
$ max = date ('Y ' ) - 17 ;
142
179
$ min = 1945 ;
180
+
143
181
$ temp = '20 ' . $ year ;
144
182
$ low = '19 ' . $ year ;
145
183
$ high = '20 ' . $ year ;
@@ -151,16 +189,94 @@ public function getTanggalLahir()
151
189
}
152
190
153
191
try {
154
- return DateTime::createFromFormat (
192
+ $ parse = DateTime::createFromFormat (
155
193
'd-m-Y ' ,
156
- sprintf ('%s-%s-%s ' , $ day , $ month , $ year )
157
- )->format ('d-m-Y ' );
158
- } catch (Exception $ e ) {
194
+ sprintf ('%s-%s-%d ' , $ day , $ month , $ year )
195
+ );
196
+ if ($ parse !== false ) {
197
+ return $ parse ->format ('d-m-Y ' );
198
+ } else {
199
+ throw new Exceptions \InvalidDateOfBirthException (sprintf (
200
+ 'Unable to parse date of birth (%s) from an invalid NIK number (%s) ' ,
201
+ $ code ,
202
+ $ this ->nik
203
+ ));
204
+ }
205
+ } catch (\Exception $ e ) {
159
206
throw new Exceptions \InvalidDateOfBirthException (sprintf (
160
207
'Unable to parse date of birth (%s) from an invalid NIK number (%s) ' ,
161
208
$ code ,
162
209
$ this ->nik
163
210
));
164
211
}
165
212
}
213
+
214
+ /**
215
+ * Get gender type.
216
+ *
217
+ * @return string|null
218
+ */
219
+ public function getGender ()
220
+ {
221
+ $ day = substr ($ this ->nik , 6 , 2 );
222
+
223
+ if ($ day > 40 ) {
224
+ $ this ->gender = 'female ' ;
225
+ } else {
226
+ $ this ->gender = 'male ' ;
227
+ }
228
+
229
+ return $ this ->gender ;
230
+ }
231
+
232
+ /**
233
+ * Get unique id.
234
+ *
235
+ * @return string|null
236
+ */
237
+ public function getUniqueId ()
238
+ {
239
+ $ code = substr ($ this ->nik , 12 , 4 );
240
+
241
+ return $ this ->unique_id = (strlen ($ code ) === 4 ? $ code : null );
242
+ }
243
+
244
+ /**
245
+ * Convert to Array
246
+ *
247
+ * @return array
248
+ */
249
+ public function toArray ()
250
+ {
251
+ $ province = $ this ->getProvince ();
252
+ $ city = $ this ->getCity ();
253
+ $ subdistrict = $ this ->getSubdistrict ();
254
+ $ postal_code = $ this ->getPostalCode ();
255
+ $ birthday = $ this ->getBirthday ();
256
+ $ gender = $ this ->getGender ();
257
+ $ unique_id = $ this ->getUniqueId ();
258
+
259
+ return [
260
+ 'province_id ' => $ this ->province_id ,
261
+ 'province ' => $ province ,
262
+ 'city_id ' => $ this ->city_id ,
263
+ 'city ' => $ city ,
264
+ 'subdistrict_id ' => $ this ->subdistrict_id ,
265
+ 'subdistrict ' => $ subdistrict ,
266
+ 'postal_code ' => $ postal_code ,
267
+ 'birthday ' => $ birthday ,
268
+ 'gender ' => $ gender ,
269
+ 'unique_id ' => $ unique_id
270
+ ];
271
+ }
272
+
273
+ /**
274
+ * Convert to JSON
275
+ *
276
+ * @return string
277
+ */
278
+ public function toJSON ()
279
+ {
280
+ return json_encode ($ this ->toArray ());
281
+ }
166
282
}
0 commit comments