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