Skip to content

Commit c4074d8

Browse files
committed
refactor: add support to PHP 5.6+, add example
1 parent a5a0afa commit c4074d8

File tree

2 files changed

+94
-32
lines changed

2 files changed

+94
-32
lines changed

examples/example.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use ZerosDev\NikReader\Reader;
66

7-
$nik = '3502200101910001';
7+
$nik = '3502200101200001';
88

9-
$reader = new Reader($nik);
9+
$reader = new Reader();
1010

11-
print_r($reader->isValid());
11+
$result = $reader->read($nik)->toArray();
12+
13+
print_r($result);

src/Reader.php

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace ZerosDev\NikReader;
44

55
use DateTime;
6+
use Exception;
67

78
class Reader
89
{
910
private $nik;
11+
private $fetchNew = false;
1012

1113
private static $database;
1214
private static $filemtime;
@@ -34,7 +36,7 @@ public function __construct(string $nik = null, string $database = null)
3436
$this->setNik($nik);
3537
}
3638

37-
$database = $database ?? dirname(__DIR__) . '/database/database.json';
39+
$database = ! is_null($database) ? $database : dirname(__DIR__) . '/database/database.json';
3840

3941
$this->setDatabase($database);
4042
}
@@ -69,6 +71,28 @@ public function setNik(string $nik)
6971
return $this;
7072
}
7173

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+
7296
/**
7397
* Set database dan baca isinya.
7498
*
@@ -108,9 +132,17 @@ public function setDatabase(string $file)
108132
*/
109133
public function getProvince()
110134
{
135+
if ($this->province && ! $this->fetchNew) {
136+
return $this->province;
137+
}
138+
111139
$this->province_id = substr($this->nik, 0, 2);
112140

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+
);
114146
}
115147

116148
/**
@@ -120,9 +152,17 @@ public function getProvince()
120152
*/
121153
public function getCity()
122154
{
155+
if ($this->city && ! $this->fetchNew) {
156+
return $this->city;
157+
}
158+
123159
$this->city_id = substr($this->nik, 0, 4);
124160

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+
);
126166
}
127167

128168
/**
@@ -132,9 +172,17 @@ public function getCity()
132172
*/
133173
public function getSubdistrict()
134174
{
175+
if ($this->subdistrict && ! $this->fetchNew) {
176+
return $this->subdistrict;
177+
}
178+
135179
$this->subdistrict_id = substr($this->nik, 0, 6);
136180

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+
);
138186

139187
if (! is_null($this->subdistrict)) {
140188
$this->subdistrict = explode(' -- ', $this->subdistrict);
@@ -151,9 +199,17 @@ public function getSubdistrict()
151199
*/
152200
public function getPostalCode()
153201
{
202+
if ($this->postal_code && ! $this->fetchNew) {
203+
return $this->postal_code;
204+
}
205+
154206
$code = substr($this->nik, 0, 6);
155207

156-
$subdistrict = (static::$database->kecamatan->{$code} ?? null);
208+
$subdistrict = (
209+
isset(static::$database->kecamatan->{$code})
210+
? static::$database->kecamatan->{$code}
211+
: null
212+
);
157213

158214
if (! is_null($subdistrict)) {
159215
$subdistrict = explode(' -- ', $subdistrict);
@@ -170,10 +226,14 @@ public function getPostalCode()
170226
*/
171227
public function getBirthday()
172228
{
229+
if ($this->birthday && ! $this->fetchNew) {
230+
return $this->birthday;
231+
}
232+
173233
$code = substr($this->nik, 6, 6);
174234
list($day, $month, $year) = str_split($code, 2);
175235

176-
$day = ((int) $day > 40) ? ($day - 40) : $day;
236+
$day = (intval($day) > 40) ? (intval($day) - 40) : $day;
177237

178238
$max = date('Y') - 17;
179239
$min = 1945;
@@ -185,7 +245,11 @@ public function getBirthday()
185245
$year = ($temp > $min) ? (($high > $max) ? $low : $high) : $low;
186246

187247
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+
));
189253
}
190254

191255
try {
@@ -194,15 +258,11 @@ public function getBirthday()
194258
sprintf('%s-%s-%d', $day, $month, $year)
195259
);
196260
if ($parse !== false) {
197-
return $parse->format('d-m-Y');
261+
return $this->birthday = $parse->format('d-m-Y');
198262
} 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();
204264
}
205-
} catch (\Exception $e) {
265+
} catch (Exception $e) {
206266
throw new Exceptions\InvalidDateOfBirthException(sprintf(
207267
'Unable to parse date of birth (%s) from an invalid NIK number (%s)',
208268
$code,
@@ -218,6 +278,10 @@ public function getBirthday()
218278
*/
219279
public function getGender()
220280
{
281+
if ($this->gender && ! $this->fetchNew) {
282+
return $this->gender;
283+
}
284+
221285
$day = substr($this->nik, 6, 2);
222286

223287
if ($day > 40) {
@@ -236,6 +300,10 @@ public function getGender()
236300
*/
237301
public function getUniqueId()
238302
{
303+
if ($this->unique_id && ! $this->fetchNew) {
304+
return $this->unique_id;
305+
}
306+
239307
$code = substr($this->nik, 12, 4);
240308

241309
return $this->unique_id = (strlen($code) === 4 ? $code : null);
@@ -248,25 +316,17 @@ public function getUniqueId()
248316
*/
249317
public function toArray()
250318
{
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-
259319
return [
260320
'province_id' => $this->province_id,
261-
'province' => $province,
321+
'province' => $this->province,
262322
'city_id' => $this->city_id,
263-
'city' => $city,
323+
'city' => $this->city,
264324
'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
270330
];
271331
}
272332

0 commit comments

Comments
 (0)