public function setValue(Content $content) { $binaryData = $content->binaryData; $offsetIndex = 0; $format = 'ymdGi'; $dateTimeString = substr($binaryData, $offsetIndex, 10); $offsetIndex += 10; // extract optional seconds part if ($binaryData[$offsetIndex] != 'Z' && $binaryData[$offsetIndex] != '+' && $binaryData[$offsetIndex] != '-') { $dateTimeString .= substr($binaryData, $offsetIndex, 2); $offsetIndex += 2; $format .= 's'; } $dateTime = \DateTime::createFromFormat($format, $dateTimeString, new \DateTimeZone('UTC')); // extract time zone settings if ($binaryData[$offsetIndex] == '+' || $binaryData[$offsetIndex] == '-') { $dateTime = static::extractTimeZoneData($binaryData, $offsetIndex, $dateTime); } elseif ($binaryData[$offsetIndex++] != 'Z') { throw new ParserException('Invalid UTC String', $offsetIndex); } $dateTimeZone = 'UTC'; if ($dateTime == null || is_string($dateTime)) { $timeZone = new DateTimeZone($dateTimeZone); $dateTimeObject = new DateTime($dateTime, $timeZone); if ($dateTimeObject == false) { $errorMessage = $this->getLastDateTimeErrors(); $className = IdentifierManager::getName($this->getType()); throw new Exception(sprintf("Could not create %s from date time string '%s': %s", $className, $dateTime, $errorMessage)); } $dateTime = $dateTimeObject; } elseif (!$dateTime instanceof DateTime) { throw new Exception('Invalid first argument for some instance of ASN_AbstractTime constructor'); } $this->value = $dateTime; }
protected function checkString() { $stringLength = $this->getContentLength(); for ($i = 0; $i < $stringLength; $i++) { if (in_array($this->value[$i], $this->allowedCharacters) == false) { $typeName = IdentifierManager::getName($this->identifier->getTagNumber()); throw new Exception("Could not create a {$typeName} from the character sequence '{$this->value}'."); } } }
protected static function createIdentifier($tagClass, $isConstructed, $tagNumber) { $identifierOctets = IdentifierManager::create($tagClass, $tagNumber, $isConstructed); $identifier = new Identifier($identifierOctets); return $identifier; }
protected static function parseBinaryIdentifier($binaryData, &$offsetIndex) { if (strlen($binaryData) <= $offsetIndex) { throw new ParserException('Can not parse identifier from data: Offset index larger than input size', $offsetIndex); } $identifier = $binaryData[$offsetIndex++]; if (IdentifierManager::isLongForm(ord($identifier)) == false) { return $identifier; } while (true) { if (strlen($binaryData) <= $offsetIndex) { throw new ParserException('Can not parse identifier (long form) from data: Offset index larger than input size', $offsetIndex); } $nextOctet = $binaryData[$offsetIndex++]; $identifier .= $nextOctet; if ((ord($nextOctet) & 0x80) === 0) { // the most significant bit is 0 to we have reached the end of the identifier break; } } return $identifier; }
/** * @param int|string $identifier * * @return int */ public static function getTagNumber($identifier) { $firstOctet = substr($identifier, 0, 1); if (!IdentifierManager::isLongForm($firstOctet)) { return ord($firstOctet) & self::LONG_FORM; } $firstOctet = self::makeNumeric($identifier); if (is_numeric($identifier)) { $identifier = chr($identifier); } return Base128::decode(substr($identifier, 1)); }
public function setValue(Content $content) { $binaryData = $content->binaryData; $offsetIndex = 0; $lengthOfMinimumTimeString = 14; // YYYYMMDDHHmmSS $contentLength = $this->contentLength->length; $maximumBytesToRead = $contentLength; $format = 'YmdGis'; $content = substr($binaryData, $offsetIndex, $contentLength); $dateTimeString = substr($content, 0, $lengthOfMinimumTimeString); $offsetIndex += $lengthOfMinimumTimeString; $maximumBytesToRead -= $lengthOfMinimumTimeString; if ($contentLength == $lengthOfMinimumTimeString) { $localTimeZone = new \DateTimeZone(date_default_timezone_get()); $dateTime = \DateTime::createFromFormat($format, $dateTimeString, $localTimeZone); } else { if ($binaryData[$offsetIndex] == '.') { $maximumBytesToRead--; // account for the '.' $nrOfFractionalSecondElements = 1; // account for the '.' while ($maximumBytesToRead > 0 && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != '+' && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != '-' && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != 'Z') { $nrOfFractionalSecondElements++; $maximumBytesToRead--; } $dateTimeString .= substr($binaryData, $offsetIndex, $nrOfFractionalSecondElements); $offsetIndex += $nrOfFractionalSecondElements; $format .= '.u'; } $dateTime = \DateTime::createFromFormat($format, $dateTimeString, new \DateTimeZone('UTC')); if ($maximumBytesToRead > 0) { if ($binaryData[$offsetIndex] == '+' || $binaryData[$offsetIndex] == '-') { $dateTime = static::extractTimeZoneData($binaryData, $offsetIndex, $dateTime); } elseif ($binaryData[$offsetIndex++] != 'Z') { throw new ParserException('Invalid ISO 8601 Time String', $offsetIndex); } } $dateTimeZone = 'UTC'; if ($dateTime == null || is_string($dateTime)) { $timeZone = new DateTimeZone($dateTimeZone); $dateTimeObject = new DateTime($dateTime, $timeZone); if ($dateTimeObject == false) { $errorMessage = $this->getLastDateTimeErrors(); $className = IdentifierManager::getName($this->getType()); throw new \Exception(sprintf("Could not create %s from date time string '%s': %s", $className, $dateTime, $errorMessage)); } $dateTime = $dateTimeObject; } elseif (!$dateTime instanceof DateTime) { throw new \Exception('Invalid first argument for some instance of ASN_AbstractTime constructor'); } $this->value = $dateTime; $this->microseconds = $this->value->format('u'); if ($this->containsFractionalSecondsElement()) { // DER requires us to remove trailing zeros $this->microseconds = preg_replace('/([1-9]+)0+$/', '$1', $this->microseconds); } } }