@@ -14,271 +14,271 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Real extends Element |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 18 | - use PrimitiveType; |
|
| 17 | + use UniversalClass; |
|
| 18 | + use PrimitiveType; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 20 | + /** |
|
| 21 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Regex pattern to parse PHP exponent number format. |
|
| 29 | - * |
|
| 30 | - * @link http://php.net/manual/en/language.types.float.php |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 34 | - '([+\-]?'. // sign |
|
| 35 | - '(?:'. |
|
| 36 | - '\d+'. // LNUM |
|
| 37 | - '|'. |
|
| 38 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 39 | - '))[eE]'. |
|
| 40 | - '([+\-]?\d+)'. // exponent |
|
| 41 | - '$/'; /* @formatter:on */ |
|
| 27 | + /** |
|
| 28 | + * Regex pattern to parse PHP exponent number format. |
|
| 29 | + * |
|
| 30 | + * @link http://php.net/manual/en/language.types.float.php |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 34 | + '([+\-]?'. // sign |
|
| 35 | + '(?:'. |
|
| 36 | + '\d+'. // LNUM |
|
| 37 | + '|'. |
|
| 38 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 39 | + '))[eE]'. |
|
| 40 | + '([+\-]?\d+)'. // exponent |
|
| 41 | + '$/'; /* @formatter:on */ |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Number zero represented in NR3 form. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - const NR3_ZERO = ".E+0"; |
|
| 43 | + /** |
|
| 44 | + * Number zero represented in NR3 form. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + const NR3_ZERO = ".E+0"; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Number in NR3 form. |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - private $_number; |
|
| 50 | + /** |
|
| 51 | + * Number in NR3 form. |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + private $_number; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Constructor. |
|
| 59 | - * |
|
| 60 | - * @param string $number Number in NR3 form. |
|
| 61 | - */ |
|
| 62 | - public function __construct($number) |
|
| 63 | - { |
|
| 64 | - $this->_typeTag = self::TYPE_REAL; |
|
| 65 | - if (!self::_validateNumber($number)) { |
|
| 66 | - throw new \InvalidArgumentException( |
|
| 67 | - "'$number' is not a valid NR3 form real."); |
|
| 68 | - } |
|
| 69 | - $this->_number = $number; |
|
| 70 | - } |
|
| 57 | + /** |
|
| 58 | + * Constructor. |
|
| 59 | + * |
|
| 60 | + * @param string $number Number in NR3 form. |
|
| 61 | + */ |
|
| 62 | + public function __construct($number) |
|
| 63 | + { |
|
| 64 | + $this->_typeTag = self::TYPE_REAL; |
|
| 65 | + if (!self::_validateNumber($number)) { |
|
| 66 | + throw new \InvalidArgumentException( |
|
| 67 | + "'$number' is not a valid NR3 form real."); |
|
| 68 | + } |
|
| 69 | + $this->_number = $number; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Initialize from float. |
|
| 74 | - * |
|
| 75 | - * @param float $number |
|
| 76 | - * @return self |
|
| 77 | - */ |
|
| 78 | - public static function fromFloat($number) |
|
| 79 | - { |
|
| 80 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 81 | - } |
|
| 72 | + /** |
|
| 73 | + * Initialize from float. |
|
| 74 | + * |
|
| 75 | + * @param float $number |
|
| 76 | + * @return self |
|
| 77 | + */ |
|
| 78 | + public static function fromFloat($number) |
|
| 79 | + { |
|
| 80 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Get number as a float. |
|
| 85 | - * |
|
| 86 | - * @return float |
|
| 87 | - */ |
|
| 88 | - public function float() |
|
| 89 | - { |
|
| 90 | - return self::_nr3ToDecimal($this->_number); |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Get number as a float. |
|
| 85 | + * |
|
| 86 | + * @return float |
|
| 87 | + */ |
|
| 88 | + public function float() |
|
| 89 | + { |
|
| 90 | + return self::_nr3ToDecimal($this->_number); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * |
|
| 95 | - * {@inheritdoc} |
|
| 96 | - */ |
|
| 97 | - protected function _encodedContentDER() |
|
| 98 | - { |
|
| 99 | - /* if the real value is the value zero, there shall be no contents |
|
| 93 | + /** |
|
| 94 | + * |
|
| 95 | + * {@inheritdoc} |
|
| 96 | + */ |
|
| 97 | + protected function _encodedContentDER() |
|
| 98 | + { |
|
| 99 | + /* if the real value is the value zero, there shall be no contents |
|
| 100 | 100 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 101 | - if (self::NR3_ZERO == $this->_number) { |
|
| 102 | - return ""; |
|
| 103 | - } |
|
| 104 | - // encode in NR3 decimal encoding |
|
| 105 | - $data = chr(0x03) . $this->_number; |
|
| 106 | - return $data; |
|
| 107 | - } |
|
| 101 | + if (self::NR3_ZERO == $this->_number) { |
|
| 102 | + return ""; |
|
| 103 | + } |
|
| 104 | + // encode in NR3 decimal encoding |
|
| 105 | + $data = chr(0x03) . $this->_number; |
|
| 106 | + return $data; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * |
|
| 111 | - * {@inheritdoc} |
|
| 112 | - * @return self |
|
| 113 | - */ |
|
| 114 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 115 | - &$offset) |
|
| 116 | - { |
|
| 117 | - $idx = $offset; |
|
| 118 | - $length = Length::expectFromDER($data, $idx); |
|
| 119 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 120 | - if (!$length->length()) { |
|
| 121 | - $obj = new self(self::NR3_ZERO); |
|
| 122 | - } else { |
|
| 123 | - $bytes = substr($data, $idx, $length->length()); |
|
| 124 | - $byte = ord($bytes[0]); |
|
| 125 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 126 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 127 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 128 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 129 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 130 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - $offset = $idx + $length->length(); |
|
| 134 | - return $obj; |
|
| 135 | - } |
|
| 109 | + /** |
|
| 110 | + * |
|
| 111 | + * {@inheritdoc} |
|
| 112 | + * @return self |
|
| 113 | + */ |
|
| 114 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 115 | + &$offset) |
|
| 116 | + { |
|
| 117 | + $idx = $offset; |
|
| 118 | + $length = Length::expectFromDER($data, $idx); |
|
| 119 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 120 | + if (!$length->length()) { |
|
| 121 | + $obj = new self(self::NR3_ZERO); |
|
| 122 | + } else { |
|
| 123 | + $bytes = substr($data, $idx, $length->length()); |
|
| 124 | + $byte = ord($bytes[0]); |
|
| 125 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 126 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 127 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 128 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 129 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 130 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + $offset = $idx + $length->length(); |
|
| 134 | + return $obj; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * |
|
| 139 | - * @todo Implement |
|
| 140 | - * @param string $data |
|
| 141 | - */ |
|
| 142 | - protected static function _decodeBinaryEncoding($data) |
|
| 143 | - { |
|
| 144 | - throw new \RuntimeException( |
|
| 145 | - "Binary encoding of REAL is not implemented."); |
|
| 146 | - } |
|
| 137 | + /** |
|
| 138 | + * |
|
| 139 | + * @todo Implement |
|
| 140 | + * @param string $data |
|
| 141 | + */ |
|
| 142 | + protected static function _decodeBinaryEncoding($data) |
|
| 143 | + { |
|
| 144 | + throw new \RuntimeException( |
|
| 145 | + "Binary encoding of REAL is not implemented."); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * |
|
| 150 | - * @param string $data |
|
| 151 | - * @throws \RuntimeException |
|
| 152 | - * @return \ASN1\Type\Primitive\Real |
|
| 153 | - */ |
|
| 154 | - protected static function _decodeDecimalEncoding($data) |
|
| 155 | - { |
|
| 156 | - $nr = ord($data[0]) & 0x03; |
|
| 157 | - if ($nr != 0x03) { |
|
| 158 | - throw new \RuntimeException("Only NR3 form supported."); |
|
| 159 | - } |
|
| 160 | - $str = substr($data, 1); |
|
| 161 | - return new self($str); |
|
| 162 | - } |
|
| 148 | + /** |
|
| 149 | + * |
|
| 150 | + * @param string $data |
|
| 151 | + * @throws \RuntimeException |
|
| 152 | + * @return \ASN1\Type\Primitive\Real |
|
| 153 | + */ |
|
| 154 | + protected static function _decodeDecimalEncoding($data) |
|
| 155 | + { |
|
| 156 | + $nr = ord($data[0]) & 0x03; |
|
| 157 | + if ($nr != 0x03) { |
|
| 158 | + throw new \RuntimeException("Only NR3 form supported."); |
|
| 159 | + } |
|
| 160 | + $str = substr($data, 1); |
|
| 161 | + return new self($str); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * |
|
| 166 | - * @todo Implement |
|
| 167 | - * @param string $data |
|
| 168 | - */ |
|
| 169 | - protected static function _decodeSpecialRealValue($data) |
|
| 170 | - { |
|
| 171 | - if (strlen($data) != 1) { |
|
| 172 | - throw new DecodeException( |
|
| 173 | - "SpecialRealValue must have one content octet."); |
|
| 174 | - } |
|
| 175 | - $byte = ord($data[0]); |
|
| 176 | - if ($byte == 0x40) { // positive infinity |
|
| 177 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 178 | - } else if ($byte == 0x41) { // negative infinity |
|
| 179 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 180 | - } else { |
|
| 181 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 182 | - } |
|
| 183 | - } |
|
| 164 | + /** |
|
| 165 | + * |
|
| 166 | + * @todo Implement |
|
| 167 | + * @param string $data |
|
| 168 | + */ |
|
| 169 | + protected static function _decodeSpecialRealValue($data) |
|
| 170 | + { |
|
| 171 | + if (strlen($data) != 1) { |
|
| 172 | + throw new DecodeException( |
|
| 173 | + "SpecialRealValue must have one content octet."); |
|
| 174 | + } |
|
| 175 | + $byte = ord($data[0]); |
|
| 176 | + if ($byte == 0x40) { // positive infinity |
|
| 177 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 178 | + } else if ($byte == 0x41) { // negative infinity |
|
| 179 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 180 | + } else { |
|
| 181 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Convert decimal number string to NR3 form. |
|
| 187 | - * |
|
| 188 | - * @param string $str |
|
| 189 | - * @return string |
|
| 190 | - */ |
|
| 191 | - private static function _decimalToNR3($str) |
|
| 192 | - { |
|
| 193 | - // if number is in exponent form |
|
| 194 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 195 | - $parts = explode(".", $match[1]); |
|
| 196 | - $m = ltrim($parts[0], "0"); |
|
| 197 | - $e = intval($match[2]); |
|
| 198 | - // if mantissa had decimals |
|
| 199 | - if (count($parts) == 2) { |
|
| 200 | - $d = rtrim($parts[1], "0"); |
|
| 201 | - $e -= strlen($d); |
|
| 202 | - $m .= $d; |
|
| 203 | - } |
|
| 204 | - } else { |
|
| 205 | - // explode from decimal |
|
| 206 | - $parts = explode(".", $str); |
|
| 207 | - $m = ltrim($parts[0], "0"); |
|
| 208 | - // if number had decimals |
|
| 209 | - if (count($parts) == 2) { |
|
| 210 | - // exponent is negative number of the decimals |
|
| 211 | - $e = -strlen($parts[1]); |
|
| 212 | - // append decimals to the mantissa |
|
| 213 | - $m .= $parts[1]; |
|
| 214 | - } else { |
|
| 215 | - $e = 0; |
|
| 216 | - } |
|
| 217 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 218 | - while (substr($m, -1) === "0") { |
|
| 219 | - $e++; |
|
| 220 | - $m = substr($m, 0, -1); |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 185 | + /** |
|
| 186 | + * Convert decimal number string to NR3 form. |
|
| 187 | + * |
|
| 188 | + * @param string $str |
|
| 189 | + * @return string |
|
| 190 | + */ |
|
| 191 | + private static function _decimalToNR3($str) |
|
| 192 | + { |
|
| 193 | + // if number is in exponent form |
|
| 194 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 195 | + $parts = explode(".", $match[1]); |
|
| 196 | + $m = ltrim($parts[0], "0"); |
|
| 197 | + $e = intval($match[2]); |
|
| 198 | + // if mantissa had decimals |
|
| 199 | + if (count($parts) == 2) { |
|
| 200 | + $d = rtrim($parts[1], "0"); |
|
| 201 | + $e -= strlen($d); |
|
| 202 | + $m .= $d; |
|
| 203 | + } |
|
| 204 | + } else { |
|
| 205 | + // explode from decimal |
|
| 206 | + $parts = explode(".", $str); |
|
| 207 | + $m = ltrim($parts[0], "0"); |
|
| 208 | + // if number had decimals |
|
| 209 | + if (count($parts) == 2) { |
|
| 210 | + // exponent is negative number of the decimals |
|
| 211 | + $e = -strlen($parts[1]); |
|
| 212 | + // append decimals to the mantissa |
|
| 213 | + $m .= $parts[1]; |
|
| 214 | + } else { |
|
| 215 | + $e = 0; |
|
| 216 | + } |
|
| 217 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 218 | + while (substr($m, -1) === "0") { |
|
| 219 | + $e++; |
|
| 220 | + $m = substr($m, 0, -1); |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 224 | 224 | (X.690 07-2002, section 11.3.2.6) */ |
| 225 | - if (0 == $e) { |
|
| 226 | - $es = "+"; |
|
| 227 | - } else { |
|
| 228 | - $es = $e < 0 ? "-" : ""; |
|
| 229 | - } |
|
| 230 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 231 | - } |
|
| 225 | + if (0 == $e) { |
|
| 226 | + $es = "+"; |
|
| 227 | + } else { |
|
| 228 | + $es = $e < 0 ? "-" : ""; |
|
| 229 | + } |
|
| 230 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Convert NR3 form number to decimal. |
|
| 235 | - * |
|
| 236 | - * @param string $str |
|
| 237 | - * @throws \UnexpectedValueException |
|
| 238 | - * @return float |
|
| 239 | - */ |
|
| 240 | - private static function _nr3ToDecimal($str) |
|
| 241 | - { |
|
| 242 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 243 | - throw new \UnexpectedValueException( |
|
| 244 | - "'$str' is not a valid NR3 form real."); |
|
| 245 | - } |
|
| 246 | - $m = $match[2]; |
|
| 247 | - // if number started with minus sign |
|
| 248 | - $inv = $match[1] == "-"; |
|
| 249 | - $e = intval($match[3]); |
|
| 250 | - // positive exponent |
|
| 251 | - if ($e > 0) { |
|
| 252 | - // pad with trailing zeroes |
|
| 253 | - $num = $m . str_repeat("0", $e); |
|
| 254 | - } else if ($e < 0) { |
|
| 255 | - // pad with leading zeroes |
|
| 256 | - if (strlen($m) < abs($e)) { |
|
| 257 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 258 | - } |
|
| 259 | - // insert decimal point |
|
| 260 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 261 | - } else { |
|
| 262 | - $num = empty($m) ? "0" : $m; |
|
| 263 | - } |
|
| 264 | - // if number is negative |
|
| 265 | - if ($inv) { |
|
| 266 | - $num = "-$num"; |
|
| 267 | - } |
|
| 268 | - return floatval($num); |
|
| 269 | - } |
|
| 233 | + /** |
|
| 234 | + * Convert NR3 form number to decimal. |
|
| 235 | + * |
|
| 236 | + * @param string $str |
|
| 237 | + * @throws \UnexpectedValueException |
|
| 238 | + * @return float |
|
| 239 | + */ |
|
| 240 | + private static function _nr3ToDecimal($str) |
|
| 241 | + { |
|
| 242 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 243 | + throw new \UnexpectedValueException( |
|
| 244 | + "'$str' is not a valid NR3 form real."); |
|
| 245 | + } |
|
| 246 | + $m = $match[2]; |
|
| 247 | + // if number started with minus sign |
|
| 248 | + $inv = $match[1] == "-"; |
|
| 249 | + $e = intval($match[3]); |
|
| 250 | + // positive exponent |
|
| 251 | + if ($e > 0) { |
|
| 252 | + // pad with trailing zeroes |
|
| 253 | + $num = $m . str_repeat("0", $e); |
|
| 254 | + } else if ($e < 0) { |
|
| 255 | + // pad with leading zeroes |
|
| 256 | + if (strlen($m) < abs($e)) { |
|
| 257 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 258 | + } |
|
| 259 | + // insert decimal point |
|
| 260 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 261 | + } else { |
|
| 262 | + $num = empty($m) ? "0" : $m; |
|
| 263 | + } |
|
| 264 | + // if number is negative |
|
| 265 | + if ($inv) { |
|
| 266 | + $num = "-$num"; |
|
| 267 | + } |
|
| 268 | + return floatval($num); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Test that number is valid for this context. |
|
| 273 | - * |
|
| 274 | - * @param mixed $num |
|
| 275 | - * @return boolean |
|
| 276 | - */ |
|
| 277 | - private static function _validateNumber($num) |
|
| 278 | - { |
|
| 279 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - return true; |
|
| 283 | - } |
|
| 271 | + /** |
|
| 272 | + * Test that number is valid for this context. |
|
| 273 | + * |
|
| 274 | + * @param mixed $num |
|
| 275 | + * @return boolean |
|
| 276 | + */ |
|
| 277 | + private static function _validateNumber($num) |
|
| 278 | + { |
|
| 279 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + return true; |
|
| 283 | + } |
|
| 284 | 284 | } |
@@ -10,27 +10,27 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class T61String extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_T61_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_T61_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - // allow everything since there's literally |
|
| 33 | - // thousands of allowed characters (16 bit composed characters) |
|
| 34 | - return true; |
|
| 35 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + // allow everything since there's literally |
|
| 33 | + // thousands of allowed characters (16 bit composed characters) |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -10,26 +10,26 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class GraphicString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_GRAPHIC_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_GRAPHIC_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - // allow everything |
|
| 33 | - return true; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + // allow everything |
|
| 33 | + return true; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -10,26 +10,26 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class ObjectDescriptor extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $descriptor |
|
| 19 | - */ |
|
| 20 | - public function __construct($descriptor) |
|
| 21 | - { |
|
| 22 | - $this->_string = $descriptor; |
|
| 23 | - $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $descriptor |
|
| 19 | + */ |
|
| 20 | + public function __construct($descriptor) |
|
| 21 | + { |
|
| 22 | + $this->_string = $descriptor; |
|
| 23 | + $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the object descriptor. |
|
| 28 | - * |
|
| 29 | - * @return string |
|
| 30 | - */ |
|
| 31 | - public function descriptor() |
|
| 32 | - { |
|
| 33 | - return $this->_string; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the object descriptor. |
|
| 28 | + * |
|
| 29 | + * @return string |
|
| 30 | + */ |
|
| 31 | + public function descriptor() |
|
| 32 | + { |
|
| 33 | + return $this->_string; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -14,112 +14,112 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class GeneralizedTime extends TimeType |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 18 | - use PrimitiveType; |
|
| 17 | + use UniversalClass; |
|
| 18 | + use PrimitiveType; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Regular expression to parse date. |
|
| 22 | - * |
|
| 23 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 28 | - '(\d\d\d\d)' . /* YYYY */ |
|
| 29 | - '(\d\d)' . /* MM */ |
|
| 30 | - '(\d\d)' . /* DD */ |
|
| 31 | - '(\d\d)' . /* hh */ |
|
| 32 | - '(\d\d)' . /* mm */ |
|
| 33 | - '(\d\d)' . /* ss */ |
|
| 34 | - '(?:\.(\d+))?' . /* frac */ |
|
| 35 | - 'Z' . /* TZ */ |
|
| 36 | - '$#' /* @formatter:on */; |
|
| 20 | + /** |
|
| 21 | + * Regular expression to parse date. |
|
| 22 | + * |
|
| 23 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 28 | + '(\d\d\d\d)' . /* YYYY */ |
|
| 29 | + '(\d\d)' . /* MM */ |
|
| 30 | + '(\d\d)' . /* DD */ |
|
| 31 | + '(\d\d)' . /* hh */ |
|
| 32 | + '(\d\d)' . /* mm */ |
|
| 33 | + '(\d\d)' . /* ss */ |
|
| 34 | + '(?:\.(\d+))?' . /* frac */ |
|
| 35 | + 'Z' . /* TZ */ |
|
| 36 | + '$#' /* @formatter:on */; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Cached formatted date. |
|
| 40 | - * |
|
| 41 | - * @var string|null |
|
| 42 | - */ |
|
| 43 | - private $_formatted; |
|
| 38 | + /** |
|
| 39 | + * Cached formatted date. |
|
| 40 | + * |
|
| 41 | + * @var string|null |
|
| 42 | + */ |
|
| 43 | + private $_formatted; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Constructor. |
|
| 47 | - * |
|
| 48 | - * @param \DateTimeImmutable $dt |
|
| 49 | - */ |
|
| 50 | - public function __construct(\DateTimeImmutable $dt) |
|
| 51 | - { |
|
| 52 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 53 | - parent::__construct($dt); |
|
| 54 | - } |
|
| 45 | + /** |
|
| 46 | + * Constructor. |
|
| 47 | + * |
|
| 48 | + * @param \DateTimeImmutable $dt |
|
| 49 | + */ |
|
| 50 | + public function __construct(\DateTimeImmutable $dt) |
|
| 51 | + { |
|
| 52 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 53 | + parent::__construct($dt); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Clear cached variables on clone. |
|
| 58 | - */ |
|
| 59 | - public function __clone() |
|
| 60 | - { |
|
| 61 | - $this->_formatted = null; |
|
| 62 | - } |
|
| 56 | + /** |
|
| 57 | + * Clear cached variables on clone. |
|
| 58 | + */ |
|
| 59 | + public function __clone() |
|
| 60 | + { |
|
| 61 | + $this->_formatted = null; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * |
|
| 66 | - * {@inheritdoc} |
|
| 67 | - */ |
|
| 68 | - protected function _encodedContentDER() |
|
| 69 | - { |
|
| 70 | - if (!isset($this->_formatted)) { |
|
| 71 | - $dt = $this->_dateTime->setTimezone( |
|
| 72 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 73 | - $this->_formatted = $dt->format("YmdHis"); |
|
| 74 | - // if fractions were used |
|
| 75 | - $frac = $dt->format("u"); |
|
| 76 | - if ($frac != 0) { |
|
| 77 | - $frac = rtrim($frac, "0"); |
|
| 78 | - $this->_formatted .= ".$frac"; |
|
| 79 | - } |
|
| 80 | - // timezone |
|
| 81 | - $this->_formatted .= "Z"; |
|
| 82 | - } |
|
| 83 | - return $this->_formatted; |
|
| 84 | - } |
|
| 64 | + /** |
|
| 65 | + * |
|
| 66 | + * {@inheritdoc} |
|
| 67 | + */ |
|
| 68 | + protected function _encodedContentDER() |
|
| 69 | + { |
|
| 70 | + if (!isset($this->_formatted)) { |
|
| 71 | + $dt = $this->_dateTime->setTimezone( |
|
| 72 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 73 | + $this->_formatted = $dt->format("YmdHis"); |
|
| 74 | + // if fractions were used |
|
| 75 | + $frac = $dt->format("u"); |
|
| 76 | + if ($frac != 0) { |
|
| 77 | + $frac = rtrim($frac, "0"); |
|
| 78 | + $this->_formatted .= ".$frac"; |
|
| 79 | + } |
|
| 80 | + // timezone |
|
| 81 | + $this->_formatted .= "Z"; |
|
| 82 | + } |
|
| 83 | + return $this->_formatted; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * |
|
| 88 | - * {@inheritdoc} |
|
| 89 | - * @return self |
|
| 90 | - */ |
|
| 91 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 92 | - &$offset) |
|
| 93 | - { |
|
| 94 | - $idx = $offset; |
|
| 95 | - $length = Length::expectFromDER($data, $idx); |
|
| 96 | - $str = substr($data, $idx, $length->length()); |
|
| 97 | - $idx += $length->length(); |
|
| 98 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 99 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 100 | - } |
|
| 101 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 102 | - if (isset($match[7])) { |
|
| 103 | - $frac = $match[7]; |
|
| 104 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 105 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 106 | - throw new DecodeException( |
|
| 107 | - "Fractional seconds must omit trailing zeroes."); |
|
| 108 | - } |
|
| 109 | - $frac = (int) $frac; |
|
| 110 | - } else { |
|
| 111 | - $frac = 0; |
|
| 112 | - } |
|
| 113 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 114 | - self::TZ_UTC; |
|
| 115 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 116 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 117 | - if (!$dt) { |
|
| 118 | - throw new DecodeException( |
|
| 119 | - "Failed to decode GeneralizedTime: " . |
|
| 120 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 121 | - } |
|
| 122 | - $offset = $idx; |
|
| 123 | - return new self($dt); |
|
| 124 | - } |
|
| 86 | + /** |
|
| 87 | + * |
|
| 88 | + * {@inheritdoc} |
|
| 89 | + * @return self |
|
| 90 | + */ |
|
| 91 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 92 | + &$offset) |
|
| 93 | + { |
|
| 94 | + $idx = $offset; |
|
| 95 | + $length = Length::expectFromDER($data, $idx); |
|
| 96 | + $str = substr($data, $idx, $length->length()); |
|
| 97 | + $idx += $length->length(); |
|
| 98 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 99 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 100 | + } |
|
| 101 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 102 | + if (isset($match[7])) { |
|
| 103 | + $frac = $match[7]; |
|
| 104 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 105 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 106 | + throw new DecodeException( |
|
| 107 | + "Fractional seconds must omit trailing zeroes."); |
|
| 108 | + } |
|
| 109 | + $frac = (int) $frac; |
|
| 110 | + } else { |
|
| 111 | + $frac = 0; |
|
| 112 | + } |
|
| 113 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 114 | + self::TZ_UTC; |
|
| 115 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 116 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 117 | + if (!$dt) { |
|
| 118 | + throw new DecodeException( |
|
| 119 | + "Failed to decode GeneralizedTime: " . |
|
| 120 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 121 | + } |
|
| 122 | + $offset = $idx; |
|
| 123 | + return new self($dt); |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -7,14 +7,14 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Enumerated extends Integer |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Constructor. |
|
| 12 | - * |
|
| 13 | - * @param int|string $number |
|
| 14 | - */ |
|
| 15 | - public function __construct($number) |
|
| 16 | - { |
|
| 17 | - parent::__construct($number); |
|
| 18 | - $this->_typeTag = self::TYPE_ENUMERATED; |
|
| 19 | - } |
|
| 10 | + /** |
|
| 11 | + * Constructor. |
|
| 12 | + * |
|
| 13 | + * @param int|string $number |
|
| 14 | + */ |
|
| 15 | + public function __construct($number) |
|
| 16 | + { |
|
| 17 | + parent::__construct($number); |
|
| 18 | + $this->_typeTag = self::TYPE_ENUMERATED; |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -14,64 +14,64 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Boolean extends Element |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 18 | - use PrimitiveType; |
|
| 17 | + use UniversalClass; |
|
| 18 | + use PrimitiveType; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Value. |
|
| 22 | - * |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - private $_bool; |
|
| 20 | + /** |
|
| 21 | + * Value. |
|
| 22 | + * |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + private $_bool; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param bool $bool |
|
| 31 | - */ |
|
| 32 | - public function __construct($bool) |
|
| 33 | - { |
|
| 34 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 35 | - $this->_bool = (bool) $bool; |
|
| 36 | - } |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param bool $bool |
|
| 31 | + */ |
|
| 32 | + public function __construct($bool) |
|
| 33 | + { |
|
| 34 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 35 | + $this->_bool = (bool) $bool; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get the value. |
|
| 40 | - * |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function value() |
|
| 44 | - { |
|
| 45 | - return $this->_bool; |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * Get the value. |
|
| 40 | + * |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function value() |
|
| 44 | + { |
|
| 45 | + return $this->_bool; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - */ |
|
| 52 | - protected function _encodedContentDER() |
|
| 53 | - { |
|
| 54 | - return $this->_bool ? chr(0xff) : chr(0); |
|
| 55 | - } |
|
| 48 | + /** |
|
| 49 | + * |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + */ |
|
| 52 | + protected function _encodedContentDER() |
|
| 53 | + { |
|
| 54 | + return $this->_bool ? chr(0xff) : chr(0); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * |
|
| 59 | - * {@inheritdoc} |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 63 | - &$offset) |
|
| 64 | - { |
|
| 65 | - $idx = $offset; |
|
| 66 | - Length::expectFromDER($data, $idx, 1); |
|
| 67 | - $byte = ord($data[$idx++]); |
|
| 68 | - if ($byte != 0) { |
|
| 69 | - if ($byte != 0xff) { |
|
| 70 | - throw new DecodeException( |
|
| 71 | - "DER encoded boolean true must have all bits set to 1."); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - $offset = $idx; |
|
| 75 | - return new self($byte != 0); |
|
| 76 | - } |
|
| 57 | + /** |
|
| 58 | + * |
|
| 59 | + * {@inheritdoc} |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 63 | + &$offset) |
|
| 64 | + { |
|
| 65 | + $idx = $offset; |
|
| 66 | + Length::expectFromDER($data, $idx, 1); |
|
| 67 | + $byte = ord($data[$idx++]); |
|
| 68 | + if ($byte != 0) { |
|
| 69 | + if ($byte != 0xff) { |
|
| 70 | + throw new DecodeException( |
|
| 71 | + "DER encoded boolean true must have all bits set to 1."); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + $offset = $idx; |
|
| 75 | + return new self($byte != 0); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -10,25 +10,25 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class NumericString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -10,16 +10,16 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class OctetString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_OCTET_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_OCTET_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | } |