Skip to content

Commit

Permalink
Merge pull request #3 from nd91jqf/master
Browse files Browse the repository at this point in the history
Update some files and re-order the getBirth function
  • Loading branch information
bangadam authored Jul 29, 2024
2 parents 4b09e19 + 00d2a27 commit dd32b95
Show file tree
Hide file tree
Showing 2 changed files with 7,115 additions and 7,132 deletions.
187 changes: 85 additions & 102 deletions src/NikParser/NikParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,37 @@

namespace NikParser;

class NikParser
{
class NikParser {
private string $nik;
private array $location;
private array $location;

/**
* @throws \Exception
*/
public function __construct(string $nik)
public function __construct($nik)
{
$this->nik = $nik;
$this->nik = $nik;
$this->location = $this->getLocation();

$this->isValid();
}

/**
* @return array
*/
private function getLocation(): array

private function getLocation() : array
{
$path = __DIR__ . '/wilayah.json';
$json = file_get_contents($path);
return json_decode($json, true);
$wilayah = json_decode($json, true);
return $wilayah;
}

/**
* @throws \Exception
*/
private function isValid(): void
private function isValid() : void
{
// Validate Length
if (strlen($this->nik) < 16 || strlen($this->nik) > 16) {
throw new \Exception("NIK must be 16 characters");
}

// Validate Data Type
if (!ctype_digit($this->nik)) {
throw new \Exception("NIK must be a numeric string");
}
}

/**
* Province section
*/
public function isValidProvince(): bool
public function isValidProvince() :bool
{
$province = substr($this->nik, 0, 2);
if (array_key_exists($province, $this->location['provinsi'])) {
Expand All @@ -56,18 +41,13 @@ public function isValidProvince(): bool
return false;
}

/**
* @return string
*/
public function getProvinceID(): string
public function getProvinceID() :string
{
return substr($this->nik, 0, 2);
$province = substr($this->nik, 0, 2);
return $province;
}

/**
* @return string
*/
public function getProvince(): string
public function getProvince() :string
{
if ($this->isValidProvince()) {
return $this->location['provinsi'][$this->getProvinceID()];
Expand All @@ -78,80 +58,83 @@ public function getProvince(): string
/**
* City section
*/
public function isValidCity(): bool
public function isValidCity() :bool
{
$city = substr($this->nik, 0, 4);
return array_key_exists($city, $this->location['kabkot']);
if (array_key_exists($city, $this->location['kabkot'])) {
return true;
}
return false;
}

/**
* @return string
*/
public function getCityID(): string
public function getCityID() :string
{
return substr($this->nik, 0, 4);
$city = substr($this->nik, 0, 4);
return $city;
}

/**
* @return string
*/
public function getCity(): string
public function getCity() :string
{
return $this->isValidCity() ? $this->location['kabkot'][$this->getCityID()] : 'Kota/Kabupaten tidak valid';
if ($this->isValidCity()) {
return $this->location['kabkot'][$this->getCityID()];
}
return 'Kota/Kabupaten tidak valid';
}

/**
* District section
*/
public function isValidDistrict(): bool
public function isValidDistrict() :bool
{
$district = substr($this->nik, 0, 6);
return array_key_exists($district, $this->location['kecamatan']);
if (array_key_exists($district, $this->location['kecamatan'])) {
return true;
}
return false;
}

/**
* @return string
*/
public function getDistrictID(): string
public function getDistrictID() :string
{
return substr($this->nik, 0, 6);
$district = substr($this->nik, 0, 6);
return $district;
}

/**
* @return string
*/
public function getDistrict(): string
public function getDistrict() :string
{
return $this->isValidDistrict() ? $this->location['kecamatan'][$this->getDistrictID()] : 'Kecamatan tidak valid';
if ($this->isValidDistrict()) {
return $this->location['kecamatan'][$this->getDistrictID()];
}
return 'Kecamatan tidak valid';
}

/**
* @return string
*/
public function getZipCode(): string
public function getZipCode() :string
{
return $this->isValidDistrict() ? substr($this->location['kecamatan'][$this->getDistrictID()], -5) : 'Kode pos tidak valid';
if ($this->isValidDistrict()) {
return substr($this->location['kecamatan'][$this->getDistrictID()], -5);
}
return 'Kode pos tidak valid';
}

/**
* Birth section
*/
public function isValidBirth(): bool
public function isValidBirth() :bool
{
$birth = substr($this->nik, 6, 6);
return $birth > 0;
if ($birth > 0) {
return true;
}
return false;
}

/**
* @return string
*/
public function getBirth(): string
public function getBirth() :string
{
if ($this->isValidBirth()) {
$birth = substr($this->nik, 6, 6);
$year = substr($birth, 0, 2);
$day = substr($birth, 0, 2);
$day = $day > 40 ? $day - 40 : $day;
$month = substr($birth, 2, 2);
$day = substr($birth, 4, 2);
$year = substr($birth, 4, 2);
return $day . '-' . $month . '-19' . $year;
}
return 'Tanggal lahir tidak valid';
Expand All @@ -160,62 +143,62 @@ public function getBirth(): string
/**
* NIK section
*/
public function getNIK(): string
public function getNIK() :string
{
return $this->nik;
}

/**
* @return string
*/
public function getUniCode(): string
public function getUniCode() :string
{
return substr($this->nik, 12, 4);
}

/**
* Person section
*/
public function getGender(): string
public function getGender() : string
{
$gender = substr($this->nik, 6, 1);
return $gender % 2 == 0 ? 'Wanita' : 'Pria';
$gender = substr($this->nik, 6, 6);

if (strlen($gender) == 6) {
$dates = str_split($gender, 2);
$day = $dates[0];
$day = (int) $day;

return $day > 40 ? 'Wanita' : 'Pria';
}

return null;

}

/**
* @return int
*/
public function getAge(): int
{
$birth = substr($this->nik, 6, 6);
$year = substr($birth, 0, 2);
$year = substr($birth, 0, 2);
$month = substr($birth, 2, 2);
$day = substr($birth, 4, 2);
$date = $day . '-' . $month . '-19' . $year;
$date = date_create($date);
$day = substr($birth, 4, 2);
$date = $day . '-' . $month . '-19' . $year;
$date = date_create($date);
$today = date_create(date('d-m-Y'));
$diff = date_diff($date, $today);
$diff = date_diff($date, $today);
return $diff->y;
}

/**
* @return \NikParser\Person
*/
public function getAll(): Person
{
$person = new Person();
$person->nik = $this->getNIK();
$person->gender = $this->getGender();
$person->province = $this->getProvince();
$person->city = $this->getCity();
$person->district = $this->getDistrict();
$person->age = $this->getAge();
$person->zipCode = $this->getZipCode();
$person->birthDate = $this->getBirth();
public function getAll() :Person
{
$person = new Person();
$person->nik = $this->getNIK();
$person->gender = $this->getGender();
$person->province = $this->getProvince();
$person->city = $this->getCity();
$person->district = $this->getDistrict();
$person->age = $this->getAge();
$person->zipCode = $this->getZipCode();
$person->birthDate = $this->getBirth();
$person->uniqueCode = $this->getUniCode();
$person->provinceID = $this->getProvinceID();
$person->cityID = $this->getCityID();
$person->cityID = $this->getCityID();
$person->districtID = $this->getDistrictID();

return $person;
Expand Down
Loading

0 comments on commit dd32b95

Please sign in to comment.