Exemplo n.º 1
0
 /**
  * Set id
  *
  * Format is YYYYMMDD(+-)NNNC or YYMMDD(+-)NNNC where parenthesis represents
  * an optional one char delimiter, N represents the individual number and C
  * the check digit. If year is set using two digits century is calculated
  * based on delimiter (+ signals more than a hundred years old). If year is
  * set using four digits delimiter is calculated based on century.
  *
  * @param  string $number
  * @throws Exception\InvalidDateStructureException If date is not logically valid
  */
 public function __construct($number)
 {
     list(, $century, $this->serialPre, $delimiter, $this->serialPost, $this->checkDigit) = PersonalId::parseStructure($number);
     $this->delimiter = $delimiter ?: '-';
     if ($century) {
         // Set delimiter based on date (+ if date is more then a hundred years old)
         $this->date = DateTimeCreator::createFromFormat('Ymd', $century . $this->serialPre);
         $hundredYearsAgo = new \DateTime();
         $hundredYearsAgo->modify('-100 year');
         $this->delimiter = $this->getDate() < $hundredYearsAgo ? '+' : '-';
     } else {
         // No century defined
         $this->date = DateTimeCreator::createFromFormat('ymd', $this->serialPre);
         // If in the future century is wrong
         if ($this->date > new \DateTime()) {
             $this->date->modify('-100 year');
         }
         // Date is over a hundred years ago if delimiter is +
         if ($this->getDelimiter() == '+') {
             $this->date->modify('-100 year');
         }
     }
     // Validate that date is logically valid
     if ($this->date->format('ymd') != $this->serialPre) {
         throw new Exception\InvalidDateStructureException("Invalid date in <{$this->getId()}>");
     }
     $this->validateCheckDigit();
 }