Class IdConverter
{
private $id;
public function __construct($id)
{
$this->id = $id;
if (!$this->checkControlDigit()) {
Throw new Exception('the id is invalid: ' . $id);
}
}
public function getSex()
{
return $this->id[0] == 1 ? 'M' : 'F';
}
public function getDate()
{
$date = substr($this->id, 1, 6);
$date = str_split($date, 2);
$currentCentury = floor(date('Y') / 100);
if ($date[0] > date('y')) {
// year is in last century
$date[0] = ($currentCentury - 1) . $date[0];
} else {
$date[0] = $currentCentury . $date[0];
}
return implode("-", $date);
}
public function getPlaceOfBirth()
{
return substr($this->id, 7, 2);
}
private function checkControlDigit()
{
// replace with actual check
return true;
}
}
$info= new IdConverter(2690627131268);
echo $info->getSex();
echo $info->getDate();
echo $info->getPlaceOfBirth();