Skip to content

Commit a5a0afa

Browse files
committed
refactor: rename method, add toArray(), add toJSON()
1 parent 0ff80b5 commit a5a0afa

File tree

1 file changed

+141
-25
lines changed

1 file changed

+141
-25
lines changed

src/Reader.php

Lines changed: 141 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace ZerosDev\NikReader;
44

55
use DateTime;
6-
use Exception;
76

87
class Reader
98
{
@@ -12,15 +11,28 @@ class Reader
1211
private static $database;
1312
private static $filemtime;
1413

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+
1525
/**
1626
* Constructor.
1727
*
18-
* @param string $nik
28+
* @param string|null $nik
1929
* @param string|null $database
2030
*/
21-
public function __construct(string $nik, string $database = null)
31+
public function __construct(string $nik = null, string $database = null)
2232
{
23-
$this->setNik($nik);
33+
if (! is_null($nik)) {
34+
$this->setNik($nik);
35+
}
2436

2537
$database = $database ?? dirname(__DIR__) . '/database/database.json';
2638

@@ -30,13 +42,11 @@ public function __construct(string $nik, string $database = null)
3042
/**
3143
* Cek validitas nomor NIK.
3244
*
33-
* @param string $nik
34-
*
3545
* @return bool
3646
*/
37-
public function isValid(string $nik)
47+
public function isValid()
3848
{
39-
return is_numeric($nik) && strlen($nik) === 16;
49+
return is_string($this->nik) && strlen($this->nik) === 16;
4050
}
4151

4252
/**
@@ -48,10 +58,11 @@ public function setNik(string $nik)
4858
{
4959
$this->nik = $nik;
5060

51-
if (! $this->isValid($nik)) {
61+
if (! $this->isValid()) {
5262
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)
5566
));
5667
}
5768

@@ -95,51 +106,78 @@ public function setDatabase(string $file)
95106
*
96107
* @return string|null
97108
*/
98-
public function getProvinsi()
109+
public function getProvince()
99110
{
100-
$code = substr($this->nik, 0, 2);
111+
$this->province_id = substr($this->nik, 0, 2);
101112

102-
return static::$database->provinsi->{$code} ?? null;
113+
return $this->province = (static::$database->provinsi->{$this->province_id} ?? null);
103114
}
104115

105116
/**
106117
* Get data kabupaten/kota dari NIK.
107118
*
108119
* @return string|null
109120
*/
110-
public function getKabupatenKota()
121+
public function getCity()
111122
{
112-
$code = substr($this->nik, 0, 4);
123+
$this->city_id = substr($this->nik, 0, 4);
113124

114-
return static::$database->kabkot->{$code} ?? null;
125+
return $this->city = (static::$database->kabkot->{$this->city_id} ?? null);
115126
}
116127

117128
/**
118129
* Get data kecamatan dari NIK.
119130
*
120131
* @return string|null
121132
*/
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()
123153
{
124154
$code = substr($this->nik, 0, 6);
125155

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;
127164
}
128165

129166
/**
130167
* Get data tanggal lahir dari NIK.
131168
*
132169
* @return string|null
133170
*/
134-
public function getTanggalLahir()
171+
public function getBirthday()
135172
{
136173
$code = substr($this->nik, 6, 6);
137174
list($day, $month, $year) = str_split($code, 2);
138175

139-
$day = (((int) $day - 40) > 0) ? ($day - 40) : $day;
176+
$day = ((int) $day > 40) ? ($day - 40) : $day;
140177

141178
$max = date('Y') - 17;
142179
$min = 1945;
180+
143181
$temp = '20' . $year;
144182
$low = '19' . $year;
145183
$high = '20' . $year;
@@ -151,16 +189,94 @@ public function getTanggalLahir()
151189
}
152190

153191
try {
154-
return DateTime::createFromFormat(
192+
$parse = DateTime::createFromFormat(
155193
'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) {
159206
throw new Exceptions\InvalidDateOfBirthException(sprintf(
160207
'Unable to parse date of birth (%s) from an invalid NIK number (%s)',
161208
$code,
162209
$this->nik
163210
));
164211
}
165212
}
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+
}
166282
}

0 commit comments

Comments
 (0)