Skip to content

Commit

Permalink
CNP
Browse files Browse the repository at this point in the history
  • Loading branch information
NICOLAE ALCEA committed Aug 19, 2019
1 parent 29a3609 commit dbb2803
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use alcea\cnp\Cnp;

$cnpToBeValidated = '5110102441483';

echo "CNP {$cnpToValidate} is " . Cnp::validate($cnpToValidate) ? 'valid' : 'invalid';
echo "CNP {$cnpToBeValidated} is " . Cnp::validate($cnpToBeValidated) ? 'valid' : 'invalid';

// OR

Expand All @@ -36,7 +36,9 @@ if ($cnp->isValid()) {
echo "Birth Date: {$cnp->getBirthDateFromCNP('Y/m/d')}" . PHP_EOL;
echo "Birth Place: {$cnp->getBirthCountyFromCNP()}" . PHP_EOL;
echo "Gender: {$cnp->getGenderFromCNP('male', 'female')}" . PHP_EOL;
echo "Serial: {$cnp->getSerialNumberFromCNP()}" . PHP_EOL;
echo "Serial: {$cnp->getSerialNumberFromCNP()}" . PHP_EOL;
echo "Person is " . ($cnp->isPersonMajor() ? '' : 'not' ) . ' major' . PHP_EOL;
echo "Person have an Identity Card " . ($cnp->hasIdentityCard() ? 'YES' : 'NO' );
} else {
echo "CNP {$cnpToBeValidated} is invalid" . PHP_EOL;
}
Expand Down
38 changes: 37 additions & 1 deletion src/Cnp.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
* echo "CNP {$cnpToValidate} - is valid" . PHP_EOL;
* echo "Birth Date: {$cnp->getBirthDateFromCNP('Y/m/d')}" . PHP_EOL;
* echo "Birth Place: {$cnp->getBirthCountyFromCNP()}" . PHP_EOL;
* echo "Gender: {$cnp->getGenderFromCNP('male', 'female')}" . PHP_EOL;
* echo "Gender: {$cnp->getGenderFromCNP('male', 'female')}" . PHP_EOL;
* echo "Person is " . ($cnp->isPersonMajor() ? '' : 'not' ) . ' major' . PHP_EOL;
* echo "Person have an Identity Card " . ($cnp->hasIdentityCard() ? 'YES' : 'NO' );
* } else {
* echo "CNP {$cnpToValidate} is invalid" . PHP_EOL;
* }
Expand Down Expand Up @@ -342,4 +344,38 @@ public function getSerialNumberFromCNP()
return ($this->_isValid) ? $this->_cnp[9] . $this->_cnp[10] . $this->_cnp[11] : '';
}

/**
* Verifica daca titularul CNP este major (>=18 years)
* @return boolean
*/
public function isPersonMajor()
{
return ($this->_isValid) ? ($this->getAgeInYears() >= 18) : false;
}

/**
* Are carte de identitate emisa de politie (emiterea se face la implinirea varstei de 14 ani)
* @return boolean
*/
public function hasIdentityCard()
{
return ($this->_isValid) ? ($this->getAgeInYears() >= 14) : false;
}

/**
* @return int
*/
private function getAgeInYears()
{
try {
$time = "{$this->_year}-{$this->_month}-{$this->_day}";
$birthDate = \DateTime::createFromFormat('Y-m-d', $time);
$now = (new \DateTime())->setTime(0, 0, 0);
return (int)$birthDate->diff($now)->format('%y');
} catch (\Throwable $e) {
return 0;
}

}

}

0 comments on commit dbb2803

Please sign in to comment.