Last active
August 9, 2020 18:27
-
-
Save ernestosu16/a4fc5a22aec658f4f27a67b6798953bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DateTime; | |
use Exception; | |
/** | |
* Class IdentityNumber | |
*/ | |
class IdentityNumber | |
{ | |
/** @var string */ | |
private $yeah; | |
/** @var string */ | |
private $month; | |
/** @var string */ | |
private $day; | |
/** @var string */ | |
private $number; | |
/** | |
* Numero Identidad constructor | |
* @param string $identity_number | |
* @param DateTime|null $fecha | |
*/ | |
public function __construct(string $identity_number) | |
{ | |
$this->yeah = substr($identity_number, 0, 2); | |
$this->month = substr($identity_number, 2, 2); | |
$this->day = substr($identity_number, 4, 2); | |
$this->number = substr($identity_number, 6, 5); | |
} | |
/** | |
* @return string | |
*/ | |
public function getIdentityNumber(): string | |
{ | |
return $this->yeah . $this->month . $this->day . $this->number; | |
} | |
/** | |
* @return DateTime | |
* @throws Exception | |
*/ | |
public function getDateOfBirth(): DateTime | |
{ | |
$century_table = $this->getCenturyTable(); | |
if (!isset($century_table[$this->getCentury()])) | |
throw new Exception(sprintf('The number "%s" does not exist in the registry', $this->getCentury())); | |
$date = new DateTime(); | |
$year = substr($century_table[$this->getCentury()]['end'], 0, 2) . $this->yeah; | |
if ($year >= $date->format('Y')) { | |
$year = substr($century_table[$this->getCentury()]['start'], 0, 2) . $this->yeah; | |
} | |
return new DateTime(sprintf('%s-%s-%s', $year, $this->month, $this->day)); | |
} | |
/** | |
* @return int | |
* @throws Exception | |
*/ | |
public function getAge(): int | |
{ | |
$date_now = new DateTime(); | |
$date_birth = $this->getDateOfBirth(); | |
$diff = $date_now->diff($date_birth); | |
return $diff->y; | |
} | |
/** | |
* @url https://diie.minint.gob.cu/backend/img/numero%20de%20carne.jpg | |
* @return string | |
*/ | |
public function getSex(): string | |
{ | |
return (substr($this->number, 3, 1) % 2) ? 'F' : 'M'; | |
} | |
/** | |
* @url https://diie.minint.gob.cu/backend/img/numero%20de%20carne.jpg | |
* @return string | |
*/ | |
public function getCentury(): string | |
{ | |
return $this->getCenturyByIdentityNumber(substr($this->number, 0, 1)); | |
} | |
/** | |
* @url https://diie.minint.gob.cu/backend/img/numero%20de%20carne.jpg | |
* @param string $century | |
* @return string|null | |
*/ | |
private function getCenturyByIdentityNumber(string $century): string | |
{ | |
switch (true): | |
case 0 <= $century || $century >= 5: | |
$century = 'XX'; | |
break; | |
case 6 <= $century || $century >= 8: | |
$century = 'XXI'; | |
break; | |
case 9 == $century: | |
$century = 'XIX'; | |
break; | |
endswitch; | |
return $century; | |
} | |
/** | |
* @url https://diie.minint.gob.cu/backend/img/numero%20de%20carne.jpg | |
* @return string[][] | |
*/ | |
private function getCenturyTable(): array | |
{ | |
return [ | |
'XIX' => ['start' => 1800, 'end' => 1900], # Siglo 19 | |
'XX' => ['start' => 1900, 'end' => 2000], # Siglo 20 | |
'XXI' => ['start' => 2001, 'end' => 2100], # Siglo 21 | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment