3
3
namespace ZerosDev \NikReader ;
4
4
5
5
use DateTime ;
6
+ use Exception ;
6
7
7
8
class Reader
8
9
{
9
10
private $ nik ;
11
+ private $ fetchNew = false ;
10
12
11
13
private static $ database ;
12
14
private static $ filemtime ;
@@ -34,7 +36,7 @@ public function __construct(string $nik = null, string $database = null)
34
36
$ this ->setNik ($ nik );
35
37
}
36
38
37
- $ database = $ database ?? dirname (__DIR__ ) . '/database/database.json ' ;
39
+ $ database = ! is_null ( $ database) ? $ database : dirname (__DIR__ ) . '/database/database.json ' ;
38
40
39
41
$ this ->setDatabase ($ database );
40
42
}
@@ -69,6 +71,28 @@ public function setNik(string $nik)
69
71
return $ this ;
70
72
}
71
73
74
+ /**
75
+ * Read data
76
+ */
77
+ public function read (string $ nik = null )
78
+ {
79
+ if (! is_null ($ nik )) {
80
+ $ this ->setNik ($ nik );
81
+ }
82
+
83
+ $ this ->fetchNew = true ;
84
+
85
+ $ this ->getProvince ();
86
+ $ this ->getCity ();
87
+ $ this ->getSubdistrict ();
88
+ $ this ->getPostalCode ();
89
+ $ this ->getBirthday ();
90
+ $ this ->getGender ();
91
+ $ this ->getUniqueId ();
92
+
93
+ return $ this ;
94
+ }
95
+
72
96
/**
73
97
* Set database dan baca isinya.
74
98
*
@@ -108,9 +132,17 @@ public function setDatabase(string $file)
108
132
*/
109
133
public function getProvince ()
110
134
{
135
+ if ($ this ->province && ! $ this ->fetchNew ) {
136
+ return $ this ->province ;
137
+ }
138
+
111
139
$ this ->province_id = substr ($ this ->nik , 0 , 2 );
112
140
113
- return $ this ->province = (static ::$ database ->provinsi ->{$ this ->province_id } ?? null );
141
+ return $ this ->province = (
142
+ isset (static ::$ database ->provinsi ->{$ this ->province_id })
143
+ ? static ::$ database ->provinsi ->{$ this ->province_id }
144
+ : null
145
+ );
114
146
}
115
147
116
148
/**
@@ -120,9 +152,17 @@ public function getProvince()
120
152
*/
121
153
public function getCity ()
122
154
{
155
+ if ($ this ->city && ! $ this ->fetchNew ) {
156
+ return $ this ->city ;
157
+ }
158
+
123
159
$ this ->city_id = substr ($ this ->nik , 0 , 4 );
124
160
125
- return $ this ->city = (static ::$ database ->kabkot ->{$ this ->city_id } ?? null );
161
+ return $ this ->city = (
162
+ isset (static ::$ database ->kabkot ->{$ this ->city_id })
163
+ ? static ::$ database ->kabkot ->{$ this ->city_id }
164
+ : null
165
+ );
126
166
}
127
167
128
168
/**
@@ -132,9 +172,17 @@ public function getCity()
132
172
*/
133
173
public function getSubdistrict ()
134
174
{
175
+ if ($ this ->subdistrict && ! $ this ->fetchNew ) {
176
+ return $ this ->subdistrict ;
177
+ }
178
+
135
179
$ this ->subdistrict_id = substr ($ this ->nik , 0 , 6 );
136
180
137
- $ this ->subdistrict = (static ::$ database ->kecamatan ->{$ this ->subdistrict_id } ?? null );
181
+ $ this ->subdistrict = (
182
+ isset (static ::$ database ->kecamatan ->{$ this ->subdistrict_id })
183
+ ? static ::$ database ->kecamatan ->{$ this ->subdistrict_id }
184
+ : null
185
+ );
138
186
139
187
if (! is_null ($ this ->subdistrict )) {
140
188
$ this ->subdistrict = explode (' -- ' , $ this ->subdistrict );
@@ -151,9 +199,17 @@ public function getSubdistrict()
151
199
*/
152
200
public function getPostalCode ()
153
201
{
202
+ if ($ this ->postal_code && ! $ this ->fetchNew ) {
203
+ return $ this ->postal_code ;
204
+ }
205
+
154
206
$ code = substr ($ this ->nik , 0 , 6 );
155
207
156
- $ subdistrict = (static ::$ database ->kecamatan ->{$ code } ?? null );
208
+ $ subdistrict = (
209
+ isset (static ::$ database ->kecamatan ->{$ code })
210
+ ? static ::$ database ->kecamatan ->{$ code }
211
+ : null
212
+ );
157
213
158
214
if (! is_null ($ subdistrict )) {
159
215
$ subdistrict = explode (' -- ' , $ subdistrict );
@@ -170,10 +226,14 @@ public function getPostalCode()
170
226
*/
171
227
public function getBirthday ()
172
228
{
229
+ if ($ this ->birthday && ! $ this ->fetchNew ) {
230
+ return $ this ->birthday ;
231
+ }
232
+
173
233
$ code = substr ($ this ->nik , 6 , 6 );
174
234
list ($ day , $ month , $ year ) = str_split ($ code , 2 );
175
235
176
- $ day = (( int ) $ day > 40 ) ? ($ day - 40 ) : $ day ;
236
+ $ day = (intval ( $ day) > 40 ) ? (intval ( $ day) - 40 ) : $ day ;
177
237
178
238
$ max = date ('Y ' ) - 17 ;
179
239
$ min = 1945 ;
@@ -185,7 +245,11 @@ public function getBirthday()
185
245
$ year = ($ temp > $ min ) ? (($ high > $ max ) ? $ low : $ high ) : $ low ;
186
246
187
247
if ($ year < $ min ) {
188
- throw new Exceptions \InvalidDateOfBirthException ('Error! year: ' . $ year );
248
+ throw new Exceptions \InvalidDateOfBirthException (sprintf (
249
+ 'Unable to parse date of birth (%s) from an invalid NIK number (%s) ' ,
250
+ $ code ,
251
+ $ this ->nik
252
+ ));
189
253
}
190
254
191
255
try {
@@ -194,15 +258,11 @@ public function getBirthday()
194
258
sprintf ('%s-%s-%d ' , $ day , $ month , $ year )
195
259
);
196
260
if ($ parse !== false ) {
197
- return $ parse ->format ('d-m-Y ' );
261
+ return $ this -> birthday = $ parse ->format ('d-m-Y ' );
198
262
} 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
- ));
263
+ throw new Exception ();
204
264
}
205
- } catch (\ Exception $ e ) {
265
+ } catch (Exception $ e ) {
206
266
throw new Exceptions \InvalidDateOfBirthException (sprintf (
207
267
'Unable to parse date of birth (%s) from an invalid NIK number (%s) ' ,
208
268
$ code ,
@@ -218,6 +278,10 @@ public function getBirthday()
218
278
*/
219
279
public function getGender ()
220
280
{
281
+ if ($ this ->gender && ! $ this ->fetchNew ) {
282
+ return $ this ->gender ;
283
+ }
284
+
221
285
$ day = substr ($ this ->nik , 6 , 2 );
222
286
223
287
if ($ day > 40 ) {
@@ -236,6 +300,10 @@ public function getGender()
236
300
*/
237
301
public function getUniqueId ()
238
302
{
303
+ if ($ this ->unique_id && ! $ this ->fetchNew ) {
304
+ return $ this ->unique_id ;
305
+ }
306
+
239
307
$ code = substr ($ this ->nik , 12 , 4 );
240
308
241
309
return $ this ->unique_id = (strlen ($ code ) === 4 ? $ code : null );
@@ -248,25 +316,17 @@ public function getUniqueId()
248
316
*/
249
317
public function toArray ()
250
318
{
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
319
return [
260
320
'province_id ' => $ this ->province_id ,
261
- 'province ' => $ province ,
321
+ 'province ' => $ this -> province ,
262
322
'city_id ' => $ this ->city_id ,
263
- 'city ' => $ city ,
323
+ 'city ' => $ this -> city ,
264
324
'subdistrict_id ' => $ this ->subdistrict_id ,
265
- 'subdistrict ' => $ subdistrict ,
266
- 'postal_code ' => $ postal_code ,
267
- 'birthday ' => $ birthday ,
268
- 'gender ' => $ gender ,
269
- 'unique_id ' => $ unique_id
325
+ 'subdistrict ' => $ this -> subdistrict ,
326
+ 'postal_code ' => $ this -> postal_code ,
327
+ 'birthday ' => $ this -> birthday ,
328
+ 'gender ' => $ this -> gender ,
329
+ 'unique_id ' => $ this -> unique_id
270
330
];
271
331
}
272
332
0 commit comments