@@ -16,113 +16,113 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GeneralizedTime extends TimeType |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Regular expression to parse date. |
|
| 24 | - * |
|
| 25 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | - '(\d\d\d\d)' . /* YYYY */ |
|
| 31 | - '(\d\d)' . /* MM */ |
|
| 32 | - '(\d\d)' . /* DD */ |
|
| 33 | - '(\d\d)' . /* hh */ |
|
| 34 | - '(\d\d)' . /* mm */ |
|
| 35 | - '(\d\d)' . /* ss */ |
|
| 36 | - '(?:\.(\d+))?' . /* frac */ |
|
| 37 | - 'Z' . /* TZ */ |
|
| 38 | - '$#' /* @formatter:on */; |
|
| 22 | + /** |
|
| 23 | + * Regular expression to parse date. |
|
| 24 | + * |
|
| 25 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | + '(\d\d\d\d)' . /* YYYY */ |
|
| 31 | + '(\d\d)' . /* MM */ |
|
| 32 | + '(\d\d)' . /* DD */ |
|
| 33 | + '(\d\d)' . /* hh */ |
|
| 34 | + '(\d\d)' . /* mm */ |
|
| 35 | + '(\d\d)' . /* ss */ |
|
| 36 | + '(?:\.(\d+))?' . /* frac */ |
|
| 37 | + 'Z' . /* TZ */ |
|
| 38 | + '$#' /* @formatter:on */; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Cached formatted date. |
|
| 42 | - * |
|
| 43 | - * @var string|null |
|
| 44 | - */ |
|
| 45 | - private $_formatted; |
|
| 40 | + /** |
|
| 41 | + * Cached formatted date. |
|
| 42 | + * |
|
| 43 | + * @var string|null |
|
| 44 | + */ |
|
| 45 | + private $_formatted; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor. |
|
| 49 | - * |
|
| 50 | - * @param \DateTimeImmutable $dt |
|
| 51 | - */ |
|
| 52 | - public function __construct(\DateTimeImmutable $dt) |
|
| 53 | - { |
|
| 54 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 55 | - parent::__construct($dt); |
|
| 56 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor. |
|
| 49 | + * |
|
| 50 | + * @param \DateTimeImmutable $dt |
|
| 51 | + */ |
|
| 52 | + public function __construct(\DateTimeImmutable $dt) |
|
| 53 | + { |
|
| 54 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 55 | + parent::__construct($dt); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Clear cached variables on clone. |
|
| 60 | - */ |
|
| 61 | - public function __clone() |
|
| 62 | - { |
|
| 63 | - $this->_formatted = null; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Clear cached variables on clone. |
|
| 60 | + */ |
|
| 61 | + public function __clone() |
|
| 62 | + { |
|
| 63 | + $this->_formatted = null; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - protected function _encodedContentDER(): string |
|
| 71 | - { |
|
| 72 | - if (!isset($this->_formatted)) { |
|
| 73 | - $dt = $this->_dateTime->setTimezone( |
|
| 74 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | - $this->_formatted = $dt->format("YmdHis"); |
|
| 76 | - // if fractions were used |
|
| 77 | - $frac = $dt->format("u"); |
|
| 78 | - if ($frac != 0) { |
|
| 79 | - $frac = rtrim($frac, "0"); |
|
| 80 | - $this->_formatted .= ".$frac"; |
|
| 81 | - } |
|
| 82 | - // timezone |
|
| 83 | - $this->_formatted .= "Z"; |
|
| 84 | - } |
|
| 85 | - return $this->_formatted; |
|
| 86 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + protected function _encodedContentDER(): string |
|
| 71 | + { |
|
| 72 | + if (!isset($this->_formatted)) { |
|
| 73 | + $dt = $this->_dateTime->setTimezone( |
|
| 74 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | + $this->_formatted = $dt->format("YmdHis"); |
|
| 76 | + // if fractions were used |
|
| 77 | + $frac = $dt->format("u"); |
|
| 78 | + if ($frac != 0) { |
|
| 79 | + $frac = rtrim($frac, "0"); |
|
| 80 | + $this->_formatted .= ".$frac"; |
|
| 81 | + } |
|
| 82 | + // timezone |
|
| 83 | + $this->_formatted .= "Z"; |
|
| 84 | + } |
|
| 85 | + return $this->_formatted; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * |
|
| 90 | - * {@inheritdoc} |
|
| 91 | - * @return self |
|
| 92 | - */ |
|
| 93 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 94 | - string $data, int &$offset): ElementBase |
|
| 95 | - { |
|
| 96 | - $idx = $offset; |
|
| 97 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 98 | - $str = substr($data, $idx, $length); |
|
| 99 | - $idx += $length; |
|
| 100 | - /** @var $match string[] */ |
|
| 101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | - } |
|
| 104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | - if (isset($match[7])) { |
|
| 106 | - $frac = $match[7]; |
|
| 107 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | - throw new DecodeException( |
|
| 110 | - "Fractional seconds must omit trailing zeroes."); |
|
| 111 | - } |
|
| 112 | - $frac = $frac; |
|
| 113 | - } else { |
|
| 114 | - $frac = '0'; |
|
| 115 | - } |
|
| 116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | - self::TZ_UTC; |
|
| 118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | - if (!$dt) { |
|
| 121 | - throw new DecodeException( |
|
| 122 | - "Failed to decode GeneralizedTime: " . |
|
| 123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | - } |
|
| 125 | - $offset = $idx; |
|
| 126 | - return new self($dt); |
|
| 127 | - } |
|
| 88 | + /** |
|
| 89 | + * |
|
| 90 | + * {@inheritdoc} |
|
| 91 | + * @return self |
|
| 92 | + */ |
|
| 93 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 94 | + string $data, int &$offset): ElementBase |
|
| 95 | + { |
|
| 96 | + $idx = $offset; |
|
| 97 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 98 | + $str = substr($data, $idx, $length); |
|
| 99 | + $idx += $length; |
|
| 100 | + /** @var $match string[] */ |
|
| 101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | + } |
|
| 104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | + if (isset($match[7])) { |
|
| 106 | + $frac = $match[7]; |
|
| 107 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | + throw new DecodeException( |
|
| 110 | + "Fractional seconds must omit trailing zeroes."); |
|
| 111 | + } |
|
| 112 | + $frac = $frac; |
|
| 113 | + } else { |
|
| 114 | + $frac = '0'; |
|
| 115 | + } |
|
| 116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | + self::TZ_UTC; |
|
| 118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | + if (!$dt) { |
|
| 121 | + throw new DecodeException( |
|
| 122 | + "Failed to decode GeneralizedTime: " . |
|
| 123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | + } |
|
| 125 | + $offset = $idx; |
|
| 126 | + return new self($dt); |
|
| 127 | + } |
|
| 128 | 128 | } |