@@ -12,17 +12,17 @@ |
||
12 | 12 | */ |
13 | 13 | class ApplicationType extends DERTaggedType |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected static function _decodeFromDER(Identifier $identifier, |
|
20 | - string $data, int &$offset): ElementBase |
|
21 | - { |
|
22 | - $idx = $offset; |
|
23 | - $type = new self($identifier, $data, $idx); |
|
24 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
25 | - $offset = $idx + $length; |
|
26 | - return $type; |
|
27 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected static function _decodeFromDER(Identifier $identifier, |
|
20 | + string $data, int &$offset): ElementBase |
|
21 | + { |
|
22 | + $idx = $offset; |
|
23 | + $type = new self($identifier, $data, $idx); |
|
24 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
25 | + $offset = $idx + $length; |
|
26 | + return $type; |
|
27 | + } |
|
28 | 28 | } |
@@ -19,107 +19,107 @@ |
||
19 | 19 | * May be encoded back to complete DER encoding. |
20 | 20 | */ |
21 | 21 | class DERTaggedType extends TaggedType implements |
22 | - ExplicitTagging, |
|
23 | - ImplicitTagging |
|
22 | + ExplicitTagging, |
|
23 | + ImplicitTagging |
|
24 | 24 | { |
25 | - /** |
|
26 | - * Identifier. |
|
27 | - * |
|
28 | - * @var Identifier |
|
29 | - */ |
|
30 | - private $_identifier; |
|
25 | + /** |
|
26 | + * Identifier. |
|
27 | + * |
|
28 | + * @var Identifier |
|
29 | + */ |
|
30 | + private $_identifier; |
|
31 | 31 | |
32 | - /** |
|
33 | - * DER data. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $_data; |
|
32 | + /** |
|
33 | + * DER data. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $_data; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Offset to data. |
|
41 | - * |
|
42 | - * @var int |
|
43 | - */ |
|
44 | - private $_offset; |
|
39 | + /** |
|
40 | + * Offset to data. |
|
41 | + * |
|
42 | + * @var int |
|
43 | + */ |
|
44 | + private $_offset; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Constructor. |
|
48 | - * |
|
49 | - * @param Identifier $identifier |
|
50 | - * @param string $data |
|
51 | - * @param int $offset Offset to next byte after identifier |
|
52 | - */ |
|
53 | - public function __construct(Identifier $identifier, string $data, |
|
54 | - int $offset) |
|
55 | - { |
|
56 | - $this->_identifier = $identifier; |
|
57 | - $this->_data = $data; |
|
58 | - $this->_offset = $offset; |
|
59 | - $this->_typeTag = $identifier->intTag(); |
|
60 | - } |
|
46 | + /** |
|
47 | + * Constructor. |
|
48 | + * |
|
49 | + * @param Identifier $identifier |
|
50 | + * @param string $data |
|
51 | + * @param int $offset Offset to next byte after identifier |
|
52 | + */ |
|
53 | + public function __construct(Identifier $identifier, string $data, |
|
54 | + int $offset) |
|
55 | + { |
|
56 | + $this->_identifier = $identifier; |
|
57 | + $this->_data = $data; |
|
58 | + $this->_offset = $offset; |
|
59 | + $this->_typeTag = $identifier->intTag(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * |
|
64 | - * @see \ASN1\Element::typeClass() |
|
65 | - * @return int |
|
66 | - */ |
|
67 | - public function typeClass(): int |
|
68 | - { |
|
69 | - return $this->_identifier->typeClass(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * |
|
64 | + * @see \ASN1\Element::typeClass() |
|
65 | + * @return int |
|
66 | + */ |
|
67 | + public function typeClass(): int |
|
68 | + { |
|
69 | + return $this->_identifier->typeClass(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * |
|
74 | - * @see \ASN1\Element::isConstructed() |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function isConstructed(): bool |
|
78 | - { |
|
79 | - return $this->_identifier->isConstructed(); |
|
80 | - } |
|
72 | + /** |
|
73 | + * |
|
74 | + * @see \ASN1\Element::isConstructed() |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function isConstructed(): bool |
|
78 | + { |
|
79 | + return $this->_identifier->isConstructed(); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * |
|
84 | - * @see \ASN1\Element::_encodedContentDER() |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function _encodedContentDER(): string |
|
88 | - { |
|
89 | - $idx = $this->_offset; |
|
90 | - $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | - return substr($this->_data, $idx, $length); |
|
92 | - } |
|
82 | + /** |
|
83 | + * |
|
84 | + * @see \ASN1\Element::_encodedContentDER() |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function _encodedContentDER(): string |
|
88 | + { |
|
89 | + $idx = $this->_offset; |
|
90 | + $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | + return substr($this->_data, $idx, $length); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * |
|
96 | - * {@inheritdoc} |
|
97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | - * @return UnspecifiedType |
|
99 | - */ |
|
100 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | - { |
|
102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | - $cls = self::_determineImplClass($identifier); |
|
104 | - $idx = $this->_offset; |
|
105 | - /** @var \ASN1\Feature\ElementBase $element */ |
|
106 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
107 | - return $element->asUnspecified(); |
|
108 | - } |
|
94 | + /** |
|
95 | + * |
|
96 | + * {@inheritdoc} |
|
97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | + * @return UnspecifiedType |
|
99 | + */ |
|
100 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | + { |
|
102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | + $cls = self::_determineImplClass($identifier); |
|
104 | + $idx = $this->_offset; |
|
105 | + /** @var \ASN1\Feature\ElementBase $element */ |
|
106 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
107 | + return $element->asUnspecified(); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * |
|
112 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
113 | - * @return UnspecifiedType |
|
114 | - */ |
|
115 | - public function explicit($expectedTag = null): UnspecifiedType |
|
116 | - { |
|
117 | - $idx = $this->_offset; |
|
118 | - Length::expectFromDER($this->_data, $idx); |
|
119 | - $element = Element::fromDER($this->_data, $idx); |
|
120 | - if (isset($expectedTag)) { |
|
121 | - $element->expectType($expectedTag); |
|
122 | - } |
|
123 | - return $element->asUnspecified(); |
|
124 | - } |
|
110 | + /** |
|
111 | + * |
|
112 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
113 | + * @return UnspecifiedType |
|
114 | + */ |
|
115 | + public function explicit($expectedTag = null): UnspecifiedType |
|
116 | + { |
|
117 | + $idx = $this->_offset; |
|
118 | + Length::expectFromDER($this->_data, $idx); |
|
119 | + $element = Element::fromDER($this->_data, $idx); |
|
120 | + if (isset($expectedTag)) { |
|
121 | + $element->expectType($expectedTag); |
|
122 | + } |
|
123 | + return $element->asUnspecified(); |
|
124 | + } |
|
125 | 125 | } |
@@ -12,17 +12,17 @@ |
||
12 | 12 | */ |
13 | 13 | class PrivateType extends DERTaggedType |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected static function _decodeFromDER(Identifier $identifier, |
|
20 | - string $data, int &$offset): ElementBase |
|
21 | - { |
|
22 | - $idx = $offset; |
|
23 | - $type = new self($identifier, $data, $idx); |
|
24 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
25 | - $offset = $idx + $length; |
|
26 | - return $type; |
|
27 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected static function _decodeFromDER(Identifier $identifier, |
|
20 | + string $data, int &$offset): ElementBase |
|
21 | + { |
|
22 | + $idx = $offset; |
|
23 | + $type = new self($identifier, $data, $idx); |
|
24 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
25 | + $offset = $idx + $length; |
|
26 | + return $type; |
|
27 | + } |
|
28 | 28 | } |
@@ -13,339 +13,339 @@ |
||
13 | 13 | * Base class for the constructed types. |
14 | 14 | */ |
15 | 15 | abstract class Structure extends Element implements |
16 | - \Countable, |
|
17 | - \IteratorAggregate |
|
16 | + \Countable, |
|
17 | + \IteratorAggregate |
|
18 | 18 | { |
19 | - use UniversalClass; |
|
19 | + use UniversalClass; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Array of elements in the structure. |
|
23 | - * |
|
24 | - * @var Element[] $_elements |
|
25 | - */ |
|
26 | - protected $_elements; |
|
21 | + /** |
|
22 | + * Array of elements in the structure. |
|
23 | + * |
|
24 | + * @var Element[] $_elements |
|
25 | + */ |
|
26 | + protected $_elements; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Lookup table for the tagged elements. |
|
30 | - * |
|
31 | - * @var TaggedType[]|null $_taggedMap |
|
32 | - */ |
|
33 | - private $_taggedMap; |
|
28 | + /** |
|
29 | + * Lookup table for the tagged elements. |
|
30 | + * |
|
31 | + * @var TaggedType[]|null $_taggedMap |
|
32 | + */ |
|
33 | + private $_taggedMap; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
37 | - * |
|
38 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
39 | - */ |
|
40 | - private $_unspecifiedTypes; |
|
35 | + /** |
|
36 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
37 | + * |
|
38 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
39 | + */ |
|
40 | + private $_unspecifiedTypes; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Constructor. |
|
44 | - * |
|
45 | - * @param ElementBase ...$elements Any number of elements |
|
46 | - */ |
|
47 | - public function __construct(ElementBase ...$elements) |
|
48 | - { |
|
49 | - $this->_elements = array_map( |
|
50 | - function (ElementBase $el) { |
|
51 | - return $el->asElement(); |
|
52 | - }, $elements); |
|
53 | - } |
|
42 | + /** |
|
43 | + * Constructor. |
|
44 | + * |
|
45 | + * @param ElementBase ...$elements Any number of elements |
|
46 | + */ |
|
47 | + public function __construct(ElementBase ...$elements) |
|
48 | + { |
|
49 | + $this->_elements = array_map( |
|
50 | + function (ElementBase $el) { |
|
51 | + return $el->asElement(); |
|
52 | + }, $elements); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Clone magic method. |
|
57 | - */ |
|
58 | - public function __clone() |
|
59 | - { |
|
60 | - // clear cache-variables |
|
61 | - $this->_taggedMap = null; |
|
62 | - $this->_unspecifiedTypes = null; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Clone magic method. |
|
57 | + */ |
|
58 | + public function __clone() |
|
59 | + { |
|
60 | + // clear cache-variables |
|
61 | + $this->_taggedMap = null; |
|
62 | + $this->_unspecifiedTypes = null; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * @see \ASN1\Element::isConstructed() |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function isConstructed(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * @see \ASN1\Element::isConstructed() |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function isConstructed(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * |
|
77 | - * @see \ASN1\Element::_encodedContentDER() |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - protected function _encodedContentDER(): string |
|
81 | - { |
|
82 | - $data = ""; |
|
83 | - foreach ($this->_elements as $element) { |
|
84 | - $data .= $element->toDER(); |
|
85 | - } |
|
86 | - return $data; |
|
87 | - } |
|
75 | + /** |
|
76 | + * |
|
77 | + * @see \ASN1\Element::_encodedContentDER() |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + protected function _encodedContentDER(): string |
|
81 | + { |
|
82 | + $data = ""; |
|
83 | + foreach ($this->_elements as $element) { |
|
84 | + $data .= $element->toDER(); |
|
85 | + } |
|
86 | + return $data; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - * @see \ASN1\Element::_decodeFromDER() |
|
93 | - * @return self |
|
94 | - */ |
|
95 | - protected static function _decodeFromDER(Identifier $identifier, |
|
96 | - string $data, int &$offset): ElementBase |
|
97 | - { |
|
98 | - $idx = $offset; |
|
99 | - if (!$identifier->isConstructed()) { |
|
100 | - throw new DecodeException( |
|
101 | - "Structured element must have constructed bit set."); |
|
102 | - } |
|
103 | - $length = Length::expectFromDER($data, $idx); |
|
104 | - $end = $idx + $length->intLength(); |
|
105 | - $elements = []; |
|
106 | - while ($idx < $end) { |
|
107 | - $elements[] = Element::fromDER($data, $idx); |
|
108 | - // check that element didn't overflow length |
|
109 | - if ($idx > $end) { |
|
110 | - throw new DecodeException( |
|
111 | - "Structure's content overflows length."); |
|
112 | - } |
|
113 | - } |
|
114 | - $offset = $idx; |
|
115 | - // return instance by static late binding |
|
116 | - return new static(...$elements); |
|
117 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + * @see \ASN1\Element::_decodeFromDER() |
|
93 | + * @return self |
|
94 | + */ |
|
95 | + protected static function _decodeFromDER(Identifier $identifier, |
|
96 | + string $data, int &$offset): ElementBase |
|
97 | + { |
|
98 | + $idx = $offset; |
|
99 | + if (!$identifier->isConstructed()) { |
|
100 | + throw new DecodeException( |
|
101 | + "Structured element must have constructed bit set."); |
|
102 | + } |
|
103 | + $length = Length::expectFromDER($data, $idx); |
|
104 | + $end = $idx + $length->intLength(); |
|
105 | + $elements = []; |
|
106 | + while ($idx < $end) { |
|
107 | + $elements[] = Element::fromDER($data, $idx); |
|
108 | + // check that element didn't overflow length |
|
109 | + if ($idx > $end) { |
|
110 | + throw new DecodeException( |
|
111 | + "Structure's content overflows length."); |
|
112 | + } |
|
113 | + } |
|
114 | + $offset = $idx; |
|
115 | + // return instance by static late binding |
|
116 | + return new static(...$elements); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Explode DER structure to DER encoded components that it contains. |
|
121 | - * |
|
122 | - * @param string $data |
|
123 | - * @throws DecodeException |
|
124 | - * @return string[] |
|
125 | - */ |
|
126 | - public static function explodeDER(string $data): array |
|
127 | - { |
|
128 | - $offset = 0; |
|
129 | - $identifier = Identifier::fromDER($data, $offset); |
|
130 | - if (!$identifier->isConstructed()) { |
|
131 | - throw new DecodeException("Element is not constructed."); |
|
132 | - } |
|
133 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
134 | - $end = $offset + $length; |
|
135 | - $parts = []; |
|
136 | - while ($offset < $end) { |
|
137 | - // start of the element |
|
138 | - $idx = $offset; |
|
139 | - // skip identifier |
|
140 | - Identifier::fromDER($data, $offset); |
|
141 | - // decode element length |
|
142 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
143 | - // extract der encoding of the element |
|
144 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
145 | - // update offset over content |
|
146 | - $offset += $length; |
|
147 | - } |
|
148 | - return $parts; |
|
149 | - } |
|
119 | + /** |
|
120 | + * Explode DER structure to DER encoded components that it contains. |
|
121 | + * |
|
122 | + * @param string $data |
|
123 | + * @throws DecodeException |
|
124 | + * @return string[] |
|
125 | + */ |
|
126 | + public static function explodeDER(string $data): array |
|
127 | + { |
|
128 | + $offset = 0; |
|
129 | + $identifier = Identifier::fromDER($data, $offset); |
|
130 | + if (!$identifier->isConstructed()) { |
|
131 | + throw new DecodeException("Element is not constructed."); |
|
132 | + } |
|
133 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
134 | + $end = $offset + $length; |
|
135 | + $parts = []; |
|
136 | + while ($offset < $end) { |
|
137 | + // start of the element |
|
138 | + $idx = $offset; |
|
139 | + // skip identifier |
|
140 | + Identifier::fromDER($data, $offset); |
|
141 | + // decode element length |
|
142 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
143 | + // extract der encoding of the element |
|
144 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
145 | + // update offset over content |
|
146 | + $offset += $length; |
|
147 | + } |
|
148 | + return $parts; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get self with an element at the given index replaced by another. |
|
153 | - * |
|
154 | - * @param int $idx Element index |
|
155 | - * @param Element $el New element to insert into the structure |
|
156 | - * @throws \OutOfBoundsException |
|
157 | - * @return self |
|
158 | - */ |
|
159 | - public function withReplaced(int $idx, Element $el): self |
|
160 | - { |
|
161 | - if (!isset($this->_elements[$idx])) { |
|
162 | - throw new \OutOfBoundsException( |
|
163 | - "Structure doesn't have element at index $idx."); |
|
164 | - } |
|
165 | - $obj = clone $this; |
|
166 | - $obj->_elements[$idx] = $el; |
|
167 | - return $obj; |
|
168 | - } |
|
151 | + /** |
|
152 | + * Get self with an element at the given index replaced by another. |
|
153 | + * |
|
154 | + * @param int $idx Element index |
|
155 | + * @param Element $el New element to insert into the structure |
|
156 | + * @throws \OutOfBoundsException |
|
157 | + * @return self |
|
158 | + */ |
|
159 | + public function withReplaced(int $idx, Element $el): self |
|
160 | + { |
|
161 | + if (!isset($this->_elements[$idx])) { |
|
162 | + throw new \OutOfBoundsException( |
|
163 | + "Structure doesn't have element at index $idx."); |
|
164 | + } |
|
165 | + $obj = clone $this; |
|
166 | + $obj->_elements[$idx] = $el; |
|
167 | + return $obj; |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get self with an element inserted before the given index. |
|
172 | - * |
|
173 | - * @param int $idx Element index |
|
174 | - * @param Element $el New element to insert into the structure |
|
175 | - * @throws \OutOfBoundsException |
|
176 | - * @return self |
|
177 | - */ |
|
178 | - public function withInserted(int $idx, Element $el): self |
|
179 | - { |
|
180 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
181 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
182 | - } |
|
183 | - $obj = clone $this; |
|
184 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
185 | - return $obj; |
|
186 | - } |
|
170 | + /** |
|
171 | + * Get self with an element inserted before the given index. |
|
172 | + * |
|
173 | + * @param int $idx Element index |
|
174 | + * @param Element $el New element to insert into the structure |
|
175 | + * @throws \OutOfBoundsException |
|
176 | + * @return self |
|
177 | + */ |
|
178 | + public function withInserted(int $idx, Element $el): self |
|
179 | + { |
|
180 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
181 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
182 | + } |
|
183 | + $obj = clone $this; |
|
184 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
185 | + return $obj; |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Get self with an element appended to the end. |
|
190 | - * |
|
191 | - * @param Element $el Element to insert into the structure |
|
192 | - * @return self |
|
193 | - */ |
|
194 | - public function withAppended(Element $el): self |
|
195 | - { |
|
196 | - $obj = clone $this; |
|
197 | - array_push($obj->_elements, $el); |
|
198 | - return $obj; |
|
199 | - } |
|
188 | + /** |
|
189 | + * Get self with an element appended to the end. |
|
190 | + * |
|
191 | + * @param Element $el Element to insert into the structure |
|
192 | + * @return self |
|
193 | + */ |
|
194 | + public function withAppended(Element $el): self |
|
195 | + { |
|
196 | + $obj = clone $this; |
|
197 | + array_push($obj->_elements, $el); |
|
198 | + return $obj; |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Get self with an element prepended in the beginning. |
|
203 | - * |
|
204 | - * @param Element $el Element to insert into the structure |
|
205 | - * @return self |
|
206 | - */ |
|
207 | - public function withPrepended(Element $el): self |
|
208 | - { |
|
209 | - $obj = clone $this; |
|
210 | - array_unshift($obj->_elements, $el); |
|
211 | - return $obj; |
|
212 | - } |
|
201 | + /** |
|
202 | + * Get self with an element prepended in the beginning. |
|
203 | + * |
|
204 | + * @param Element $el Element to insert into the structure |
|
205 | + * @return self |
|
206 | + */ |
|
207 | + public function withPrepended(Element $el): self |
|
208 | + { |
|
209 | + $obj = clone $this; |
|
210 | + array_unshift($obj->_elements, $el); |
|
211 | + return $obj; |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * Get self with an element at the given index removed. |
|
216 | - * |
|
217 | - * @param int $idx Element index |
|
218 | - * @throws \OutOfBoundsException |
|
219 | - * @return self |
|
220 | - */ |
|
221 | - public function withoutElement(int $idx): self |
|
222 | - { |
|
223 | - if (!isset($this->_elements[$idx])) { |
|
224 | - throw new \OutOfBoundsException( |
|
225 | - "Structure doesn't have element at index $idx."); |
|
226 | - } |
|
227 | - $obj = clone $this; |
|
228 | - array_splice($obj->_elements, $idx, 1); |
|
229 | - return $obj; |
|
230 | - } |
|
214 | + /** |
|
215 | + * Get self with an element at the given index removed. |
|
216 | + * |
|
217 | + * @param int $idx Element index |
|
218 | + * @throws \OutOfBoundsException |
|
219 | + * @return self |
|
220 | + */ |
|
221 | + public function withoutElement(int $idx): self |
|
222 | + { |
|
223 | + if (!isset($this->_elements[$idx])) { |
|
224 | + throw new \OutOfBoundsException( |
|
225 | + "Structure doesn't have element at index $idx."); |
|
226 | + } |
|
227 | + $obj = clone $this; |
|
228 | + array_splice($obj->_elements, $idx, 1); |
|
229 | + return $obj; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Get elements in the structure. |
|
234 | - * |
|
235 | - * @return UnspecifiedType[] |
|
236 | - */ |
|
237 | - public function elements(): array |
|
238 | - { |
|
239 | - if (!isset($this->_unspecifiedTypes)) { |
|
240 | - $this->_unspecifiedTypes = array_map( |
|
241 | - function (Element $el) { |
|
242 | - return new UnspecifiedType($el); |
|
243 | - }, $this->_elements); |
|
244 | - } |
|
245 | - return $this->_unspecifiedTypes; |
|
246 | - } |
|
232 | + /** |
|
233 | + * Get elements in the structure. |
|
234 | + * |
|
235 | + * @return UnspecifiedType[] |
|
236 | + */ |
|
237 | + public function elements(): array |
|
238 | + { |
|
239 | + if (!isset($this->_unspecifiedTypes)) { |
|
240 | + $this->_unspecifiedTypes = array_map( |
|
241 | + function (Element $el) { |
|
242 | + return new UnspecifiedType($el); |
|
243 | + }, $this->_elements); |
|
244 | + } |
|
245 | + return $this->_unspecifiedTypes; |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Check whether the structure has an element at the given index, optionally |
|
250 | - * satisfying given tag expectation. |
|
251 | - * |
|
252 | - * @param int $idx Index 0..n |
|
253 | - * @param int|null $expectedTag Optional type tag expectation |
|
254 | - * @return bool |
|
255 | - */ |
|
256 | - public function has(int $idx, $expectedTag = null): bool |
|
257 | - { |
|
258 | - if (!isset($this->_elements[$idx])) { |
|
259 | - return false; |
|
260 | - } |
|
261 | - if (isset($expectedTag)) { |
|
262 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
263 | - return false; |
|
264 | - } |
|
265 | - } |
|
266 | - return true; |
|
267 | - } |
|
248 | + /** |
|
249 | + * Check whether the structure has an element at the given index, optionally |
|
250 | + * satisfying given tag expectation. |
|
251 | + * |
|
252 | + * @param int $idx Index 0..n |
|
253 | + * @param int|null $expectedTag Optional type tag expectation |
|
254 | + * @return bool |
|
255 | + */ |
|
256 | + public function has(int $idx, $expectedTag = null): bool |
|
257 | + { |
|
258 | + if (!isset($this->_elements[$idx])) { |
|
259 | + return false; |
|
260 | + } |
|
261 | + if (isset($expectedTag)) { |
|
262 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
263 | + return false; |
|
264 | + } |
|
265 | + } |
|
266 | + return true; |
|
267 | + } |
|
268 | 268 | |
269 | - /** |
|
270 | - * Get the element at the given index, optionally checking that the element |
|
271 | - * has a given tag. |
|
272 | - * |
|
273 | - * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
274 | - * done with <code>UnspecifiedType</code>. |
|
275 | - * |
|
276 | - * @param int $idx Index 0..n |
|
277 | - * @param int|null $expectedTag Optional type tag expectation |
|
278 | - * @throws \OutOfBoundsException If element doesn't exists |
|
279 | - * @throws \UnexpectedValueException If expectation fails |
|
280 | - * @return UnspecifiedType |
|
281 | - */ |
|
282 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
283 | - { |
|
284 | - if (!isset($this->_elements[$idx])) { |
|
285 | - throw new \OutOfBoundsException( |
|
286 | - "Structure doesn't have an element at index $idx."); |
|
287 | - } |
|
288 | - $element = $this->_elements[$idx]; |
|
289 | - if (isset($expectedTag)) { |
|
290 | - $element->expectType($expectedTag); |
|
291 | - } |
|
292 | - return new UnspecifiedType($element); |
|
293 | - } |
|
269 | + /** |
|
270 | + * Get the element at the given index, optionally checking that the element |
|
271 | + * has a given tag. |
|
272 | + * |
|
273 | + * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
274 | + * done with <code>UnspecifiedType</code>. |
|
275 | + * |
|
276 | + * @param int $idx Index 0..n |
|
277 | + * @param int|null $expectedTag Optional type tag expectation |
|
278 | + * @throws \OutOfBoundsException If element doesn't exists |
|
279 | + * @throws \UnexpectedValueException If expectation fails |
|
280 | + * @return UnspecifiedType |
|
281 | + */ |
|
282 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
283 | + { |
|
284 | + if (!isset($this->_elements[$idx])) { |
|
285 | + throw new \OutOfBoundsException( |
|
286 | + "Structure doesn't have an element at index $idx."); |
|
287 | + } |
|
288 | + $element = $this->_elements[$idx]; |
|
289 | + if (isset($expectedTag)) { |
|
290 | + $element->expectType($expectedTag); |
|
291 | + } |
|
292 | + return new UnspecifiedType($element); |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Check whether the structure contains a context specific element with a |
|
297 | - * given tag. |
|
298 | - * |
|
299 | - * @param int $tag Tag number |
|
300 | - * @return bool |
|
301 | - */ |
|
302 | - public function hasTagged(int $tag): bool |
|
303 | - { |
|
304 | - // lazily build lookup map |
|
305 | - if (!isset($this->_taggedMap)) { |
|
306 | - $this->_taggedMap = []; |
|
307 | - foreach ($this->_elements as $element) { |
|
308 | - if ($element->isTagged()) { |
|
309 | - $this->_taggedMap[$element->tag()] = $element; |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - return isset($this->_taggedMap[$tag]); |
|
314 | - } |
|
295 | + /** |
|
296 | + * Check whether the structure contains a context specific element with a |
|
297 | + * given tag. |
|
298 | + * |
|
299 | + * @param int $tag Tag number |
|
300 | + * @return bool |
|
301 | + */ |
|
302 | + public function hasTagged(int $tag): bool |
|
303 | + { |
|
304 | + // lazily build lookup map |
|
305 | + if (!isset($this->_taggedMap)) { |
|
306 | + $this->_taggedMap = []; |
|
307 | + foreach ($this->_elements as $element) { |
|
308 | + if ($element->isTagged()) { |
|
309 | + $this->_taggedMap[$element->tag()] = $element; |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + return isset($this->_taggedMap[$tag]); |
|
314 | + } |
|
315 | 315 | |
316 | - /** |
|
317 | - * Get a context specific element tagged with a given tag. |
|
318 | - * |
|
319 | - * @param int $tag |
|
320 | - * @throws \LogicException If tag doesn't exists |
|
321 | - * @return TaggedType |
|
322 | - */ |
|
323 | - public function getTagged(int $tag): TaggedType |
|
324 | - { |
|
325 | - if (!$this->hasTagged($tag)) { |
|
326 | - throw new \LogicException("No tagged element for tag $tag."); |
|
327 | - } |
|
328 | - return $this->_taggedMap[$tag]; |
|
329 | - } |
|
316 | + /** |
|
317 | + * Get a context specific element tagged with a given tag. |
|
318 | + * |
|
319 | + * @param int $tag |
|
320 | + * @throws \LogicException If tag doesn't exists |
|
321 | + * @return TaggedType |
|
322 | + */ |
|
323 | + public function getTagged(int $tag): TaggedType |
|
324 | + { |
|
325 | + if (!$this->hasTagged($tag)) { |
|
326 | + throw new \LogicException("No tagged element for tag $tag."); |
|
327 | + } |
|
328 | + return $this->_taggedMap[$tag]; |
|
329 | + } |
|
330 | 330 | |
331 | - /** |
|
332 | - * |
|
333 | - * @see \Countable::count() |
|
334 | - * @return int |
|
335 | - */ |
|
336 | - public function count(): int |
|
337 | - { |
|
338 | - return count($this->_elements); |
|
339 | - } |
|
331 | + /** |
|
332 | + * |
|
333 | + * @see \Countable::count() |
|
334 | + * @return int |
|
335 | + */ |
|
336 | + public function count(): int |
|
337 | + { |
|
338 | + return count($this->_elements); |
|
339 | + } |
|
340 | 340 | |
341 | - /** |
|
342 | - * Get an iterator for the UnspecifiedElement objects. |
|
343 | - * |
|
344 | - * @see \IteratorAggregate::getIterator() |
|
345 | - * @return \ArrayIterator |
|
346 | - */ |
|
347 | - public function getIterator(): \ArrayIterator |
|
348 | - { |
|
349 | - return new \ArrayIterator($this->elements()); |
|
350 | - } |
|
341 | + /** |
|
342 | + * Get an iterator for the UnspecifiedElement objects. |
|
343 | + * |
|
344 | + * @see \IteratorAggregate::getIterator() |
|
345 | + * @return \ArrayIterator |
|
346 | + */ |
|
347 | + public function getIterator(): \ArrayIterator |
|
348 | + { |
|
349 | + return new \ArrayIterator($this->elements()); |
|
350 | + } |
|
351 | 351 | } |
@@ -10,88 +10,88 @@ |
||
10 | 10 | */ |
11 | 11 | abstract class TimeType extends Element |
12 | 12 | { |
13 | - /** |
|
14 | - * UTC timezone. |
|
15 | - * |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - const TZ_UTC = "UTC"; |
|
13 | + /** |
|
14 | + * UTC timezone. |
|
15 | + * |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + const TZ_UTC = "UTC"; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Date and time. |
|
22 | - * |
|
23 | - * @var \DateTimeImmutable $_dateTime |
|
24 | - */ |
|
25 | - protected $_dateTime; |
|
20 | + /** |
|
21 | + * Date and time. |
|
22 | + * |
|
23 | + * @var \DateTimeImmutable $_dateTime |
|
24 | + */ |
|
25 | + protected $_dateTime; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * @param \DateTimeImmutable $dt |
|
31 | - */ |
|
32 | - public function __construct(\DateTimeImmutable $dt) |
|
33 | - { |
|
34 | - $this->_dateTime = $dt; |
|
35 | - } |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * @param \DateTimeImmutable $dt |
|
31 | + */ |
|
32 | + public function __construct(\DateTimeImmutable $dt) |
|
33 | + { |
|
34 | + $this->_dateTime = $dt; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Initialize from datetime string. |
|
39 | - * |
|
40 | - * @link http://php.net/manual/en/datetime.formats.php |
|
41 | - * @param string $time Time string |
|
42 | - * @param string|null $tz Timezone, if null use default. |
|
43 | - * @throws \RuntimeException |
|
44 | - * @return self |
|
45 | - */ |
|
46 | - public static function fromString(string $time, string $tz = null): self |
|
47 | - { |
|
48 | - try { |
|
49 | - if (!isset($tz)) { |
|
50 | - $tz = date_default_timezone_get(); |
|
51 | - } |
|
52 | - return new static( |
|
53 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
54 | - } catch (\Exception $e) { |
|
55 | - throw new \RuntimeException( |
|
56 | - "Failed to create DateTime: " . |
|
57 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
58 | - } |
|
59 | - } |
|
37 | + /** |
|
38 | + * Initialize from datetime string. |
|
39 | + * |
|
40 | + * @link http://php.net/manual/en/datetime.formats.php |
|
41 | + * @param string $time Time string |
|
42 | + * @param string|null $tz Timezone, if null use default. |
|
43 | + * @throws \RuntimeException |
|
44 | + * @return self |
|
45 | + */ |
|
46 | + public static function fromString(string $time, string $tz = null): self |
|
47 | + { |
|
48 | + try { |
|
49 | + if (!isset($tz)) { |
|
50 | + $tz = date_default_timezone_get(); |
|
51 | + } |
|
52 | + return new static( |
|
53 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
54 | + } catch (\Exception $e) { |
|
55 | + throw new \RuntimeException( |
|
56 | + "Failed to create DateTime: " . |
|
57 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Get the date and time. |
|
63 | - * |
|
64 | - * @return \DateTimeImmutable |
|
65 | - */ |
|
66 | - public function dateTime(): \DateTimeImmutable |
|
67 | - { |
|
68 | - return $this->_dateTime; |
|
69 | - } |
|
61 | + /** |
|
62 | + * Get the date and time. |
|
63 | + * |
|
64 | + * @return \DateTimeImmutable |
|
65 | + */ |
|
66 | + public function dateTime(): \DateTimeImmutable |
|
67 | + { |
|
68 | + return $this->_dateTime; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Create DateTimeZone object from string. |
|
73 | - * |
|
74 | - * @param string $tz |
|
75 | - * @throws \UnexpectedValueException If timezone is invalid |
|
76 | - * @return \DateTimeZone |
|
77 | - */ |
|
78 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
79 | - { |
|
80 | - try { |
|
81 | - return new \DateTimeZone($tz); |
|
82 | - } catch (\Exception $e) { |
|
83 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
84 | - } |
|
85 | - } |
|
71 | + /** |
|
72 | + * Create DateTimeZone object from string. |
|
73 | + * |
|
74 | + * @param string $tz |
|
75 | + * @throws \UnexpectedValueException If timezone is invalid |
|
76 | + * @return \DateTimeZone |
|
77 | + */ |
|
78 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
79 | + { |
|
80 | + try { |
|
81 | + return new \DateTimeZone($tz); |
|
82 | + } catch (\Exception $e) { |
|
83 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
84 | + } |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Get last error caused by DateTimeImmutable. |
|
89 | - * |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
93 | - { |
|
94 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
95 | - return implode(", ", $errors); |
|
96 | - } |
|
87 | + /** |
|
88 | + * Get last error caused by DateTimeImmutable. |
|
89 | + * |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
93 | + { |
|
94 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
95 | + return implode(", ", $errors); |
|
96 | + } |
|
97 | 97 | } |
@@ -16,85 +16,85 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class TaggedType extends Element |
18 | 18 | { |
19 | - /** |
|
20 | - * |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected static function _decodeFromDER(Identifier $identifier, |
|
24 | - string $data, int &$offset): ElementBase |
|
25 | - { |
|
26 | - $idx = $offset; |
|
27 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
28 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
29 | - $offset = $idx + $length; |
|
30 | - return $type; |
|
31 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected static function _decodeFromDER(Identifier $identifier, |
|
24 | + string $data, int &$offset): ElementBase |
|
25 | + { |
|
26 | + $idx = $offset; |
|
27 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
28 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
29 | + $offset = $idx + $length; |
|
30 | + return $type; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Check whether element supports explicit tagging. |
|
35 | - * |
|
36 | - * @param int|null $expectedTag Optional outer tag expectation |
|
37 | - * @throws \UnexpectedValueException If expectation fails |
|
38 | - * @return ExplicitTagging |
|
39 | - */ |
|
40 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
41 | - { |
|
42 | - $el = $this; |
|
43 | - if (!$el instanceof ExplicitTagging) { |
|
44 | - throw new \UnexpectedValueException( |
|
45 | - "Element doesn't implement explicit tagging."); |
|
46 | - } |
|
47 | - if (isset($expectedTag)) { |
|
48 | - $el->expectTagged($expectedTag); |
|
49 | - } |
|
50 | - return $el; |
|
51 | - } |
|
33 | + /** |
|
34 | + * Check whether element supports explicit tagging. |
|
35 | + * |
|
36 | + * @param int|null $expectedTag Optional outer tag expectation |
|
37 | + * @throws \UnexpectedValueException If expectation fails |
|
38 | + * @return ExplicitTagging |
|
39 | + */ |
|
40 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
41 | + { |
|
42 | + $el = $this; |
|
43 | + if (!$el instanceof ExplicitTagging) { |
|
44 | + throw new \UnexpectedValueException( |
|
45 | + "Element doesn't implement explicit tagging."); |
|
46 | + } |
|
47 | + if (isset($expectedTag)) { |
|
48 | + $el->expectTagged($expectedTag); |
|
49 | + } |
|
50 | + return $el; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the wrapped inner element employing explicit tagging. |
|
55 | - * |
|
56 | - * @param int|null $expectedTag Optional outer tag expectation |
|
57 | - * @throws \UnexpectedValueException If expectation fails |
|
58 | - * @return UnspecifiedType |
|
59 | - */ |
|
60 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
61 | - { |
|
62 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
63 | - } |
|
53 | + /** |
|
54 | + * Get the wrapped inner element employing explicit tagging. |
|
55 | + * |
|
56 | + * @param int|null $expectedTag Optional outer tag expectation |
|
57 | + * @throws \UnexpectedValueException If expectation fails |
|
58 | + * @return UnspecifiedType |
|
59 | + */ |
|
60 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
61 | + { |
|
62 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Check whether element supports implicit tagging. |
|
67 | - * |
|
68 | - * @param int|null $expectedTag Optional outer tag expectation |
|
69 | - * @throws \UnexpectedValueException If expectation fails |
|
70 | - * @return ImplicitTagging |
|
71 | - */ |
|
72 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
73 | - { |
|
74 | - $el = $this; |
|
75 | - if (!$el instanceof ImplicitTagging) { |
|
76 | - throw new \UnexpectedValueException( |
|
77 | - "Element doesn't implement implicit tagging."); |
|
78 | - } |
|
79 | - if (isset($expectedTag)) { |
|
80 | - $el->expectTagged($expectedTag); |
|
81 | - } |
|
82 | - return $el; |
|
83 | - } |
|
65 | + /** |
|
66 | + * Check whether element supports implicit tagging. |
|
67 | + * |
|
68 | + * @param int|null $expectedTag Optional outer tag expectation |
|
69 | + * @throws \UnexpectedValueException If expectation fails |
|
70 | + * @return ImplicitTagging |
|
71 | + */ |
|
72 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
73 | + { |
|
74 | + $el = $this; |
|
75 | + if (!$el instanceof ImplicitTagging) { |
|
76 | + throw new \UnexpectedValueException( |
|
77 | + "Element doesn't implement implicit tagging."); |
|
78 | + } |
|
79 | + if (isset($expectedTag)) { |
|
80 | + $el->expectTagged($expectedTag); |
|
81 | + } |
|
82 | + return $el; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get the wrapped inner element employing implicit tagging. |
|
87 | - * |
|
88 | - * @param int $tag Type tag of the inner element |
|
89 | - * @param int|null $expectedTag Optional outer tag expectation |
|
90 | - * @param int $expectedClass Optional inner type class expectation |
|
91 | - * @throws \UnexpectedValueException If expectation fails |
|
92 | - * @return UnspecifiedType |
|
93 | - */ |
|
94 | - public function asImplicit(int $tag, $expectedTag = null, |
|
95 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
96 | - { |
|
97 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
98 | - $expectedClass); |
|
99 | - } |
|
85 | + /** |
|
86 | + * Get the wrapped inner element employing implicit tagging. |
|
87 | + * |
|
88 | + * @param int $tag Type tag of the inner element |
|
89 | + * @param int|null $expectedTag Optional outer tag expectation |
|
90 | + * @param int $expectedClass Optional inner type class expectation |
|
91 | + * @throws \UnexpectedValueException If expectation fails |
|
92 | + * @return UnspecifiedType |
|
93 | + */ |
|
94 | + public function asImplicit(int $tag, $expectedTag = null, |
|
95 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
96 | + { |
|
97 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
98 | + $expectedClass); |
|
99 | + } |
|
100 | 100 | } |
@@ -16,665 +16,665 @@ |
||
16 | 16 | */ |
17 | 17 | class UnspecifiedType implements ElementBase |
18 | 18 | { |
19 | - /** |
|
20 | - * The wrapped element. |
|
21 | - * |
|
22 | - * @var Element |
|
23 | - */ |
|
24 | - private $_element; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param Element $el |
|
30 | - */ |
|
31 | - public function __construct(Element $el) |
|
32 | - { |
|
33 | - $this->_element = $el; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Initialize from DER data. |
|
38 | - * |
|
39 | - * @param string $data DER encoded data |
|
40 | - * @return self |
|
41 | - */ |
|
42 | - public static function fromDER(string $data): self |
|
43 | - { |
|
44 | - return Element::fromDER($data)->asUnspecified(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Initialize from ElementBase interface. |
|
49 | - * |
|
50 | - * @param ElementBase $el |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromElementBase(ElementBase $el): self |
|
54 | - { |
|
55 | - // if element is already wrapped |
|
56 | - if ($el instanceof self) { |
|
57 | - return $el; |
|
58 | - } |
|
59 | - return new self($el->asElement()); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Compatibility method to dispatch calls to the wrapped element. |
|
64 | - * |
|
65 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
66 | - * @param string $mtd Method name |
|
67 | - * @param array $args Arguments |
|
68 | - * @return mixed |
|
69 | - */ |
|
70 | - public function __call($mtd, array $args) |
|
71 | - { |
|
72 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Get the wrapped element as a context specific tagged type. |
|
77 | - * |
|
78 | - * @throws \UnexpectedValueException If the element is not tagged |
|
79 | - * @return TaggedType |
|
80 | - */ |
|
81 | - public function asTagged(): TaggedType |
|
82 | - { |
|
83 | - if (!$this->_element instanceof TaggedType) { |
|
84 | - throw new \UnexpectedValueException( |
|
85 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
86 | - } |
|
87 | - return $this->_element; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Get the wrapped element as an application specific type. |
|
92 | - * |
|
93 | - * @throws \UnexpectedValueException If element is not application specific |
|
94 | - * @return \ASN1\Type\Tagged\ApplicationType |
|
95 | - */ |
|
96 | - public function asApplication(): Tagged\ApplicationType |
|
97 | - { |
|
98 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
99 | - throw new \UnexpectedValueException( |
|
100 | - "Application type expected, got " . |
|
101 | - $this->_typeDescriptorString()); |
|
102 | - } |
|
103 | - return $this->_element; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get the wrapped element as a private tagged type. |
|
108 | - * |
|
109 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
110 | - * @return \ASN1\Type\Tagged\PrivateType |
|
111 | - */ |
|
112 | - public function asPrivate(): Tagged\PrivateType |
|
113 | - { |
|
114 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
115 | - throw new \UnexpectedValueException( |
|
116 | - "Private type expected, got " . $this->_typeDescriptorString()); |
|
117 | - } |
|
118 | - return $this->_element; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get the wrapped element as a boolean type. |
|
123 | - * |
|
124 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
125 | - * @return \ASN1\Type\Primitive\Boolean |
|
126 | - */ |
|
127 | - public function asBoolean(): Primitive\Boolean |
|
128 | - { |
|
129 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
130 | - throw new \UnexpectedValueException( |
|
131 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
132 | - } |
|
133 | - return $this->_element; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the wrapped element as an integer type. |
|
138 | - * |
|
139 | - * @throws \UnexpectedValueException If the element is not an integer |
|
140 | - * @return \ASN1\Type\Primitive\Integer |
|
141 | - */ |
|
142 | - public function asInteger(): Primitive\Integer |
|
143 | - { |
|
144 | - if (!$this->_element instanceof Primitive\Integer) { |
|
145 | - throw new \UnexpectedValueException( |
|
146 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
147 | - } |
|
148 | - return $this->_element; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Get the wrapped element as a bit string type. |
|
153 | - * |
|
154 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
155 | - * @return \ASN1\Type\Primitive\BitString |
|
156 | - */ |
|
157 | - public function asBitString(): Primitive\BitString |
|
158 | - { |
|
159 | - if (!$this->_element instanceof Primitive\BitString) { |
|
160 | - throw new \UnexpectedValueException( |
|
161 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
162 | - } |
|
163 | - return $this->_element; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get the wrapped element as an octet string type. |
|
168 | - * |
|
169 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
170 | - * @return \ASN1\Type\Primitive\OctetString |
|
171 | - */ |
|
172 | - public function asOctetString(): Primitive\OctetString |
|
173 | - { |
|
174 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
175 | - throw new \UnexpectedValueException( |
|
176 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
177 | - } |
|
178 | - return $this->_element; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the wrapped element as a null type. |
|
183 | - * |
|
184 | - * @throws \UnexpectedValueException If the element is not a null |
|
185 | - * @return \ASN1\Type\Primitive\NullType |
|
186 | - */ |
|
187 | - public function asNull(): Primitive\NullType |
|
188 | - { |
|
189 | - if (!$this->_element instanceof Primitive\NullType) { |
|
190 | - throw new \UnexpectedValueException( |
|
191 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
192 | - } |
|
193 | - return $this->_element; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Get the wrapped element as an object identifier type. |
|
198 | - * |
|
199 | - * @throws \UnexpectedValueException If the element is not an object |
|
200 | - * identifier |
|
201 | - * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
202 | - */ |
|
203 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
204 | - { |
|
205 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
206 | - throw new \UnexpectedValueException( |
|
207 | - $this->_generateExceptionMessage( |
|
208 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
209 | - } |
|
210 | - return $this->_element; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get the wrapped element as an object descriptor type. |
|
215 | - * |
|
216 | - * @throws \UnexpectedValueException If the element is not an object |
|
217 | - * descriptor |
|
218 | - * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
219 | - */ |
|
220 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
221 | - { |
|
222 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
223 | - throw new \UnexpectedValueException( |
|
224 | - $this->_generateExceptionMessage( |
|
225 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
226 | - } |
|
227 | - return $this->_element; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Get the wrapped element as a real type. |
|
232 | - * |
|
233 | - * @throws \UnexpectedValueException If the element is not a real |
|
234 | - * @return \ASN1\Type\Primitive\Real |
|
235 | - */ |
|
236 | - public function asReal(): Primitive\Real |
|
237 | - { |
|
238 | - if (!$this->_element instanceof Primitive\Real) { |
|
239 | - throw new \UnexpectedValueException( |
|
240 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
241 | - } |
|
242 | - return $this->_element; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Get the wrapped element as an enumerated type. |
|
247 | - * |
|
248 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
249 | - * @return \ASN1\Type\Primitive\Enumerated |
|
250 | - */ |
|
251 | - public function asEnumerated(): Primitive\Enumerated |
|
252 | - { |
|
253 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
254 | - throw new \UnexpectedValueException( |
|
255 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
256 | - } |
|
257 | - return $this->_element; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Get the wrapped element as a UTF8 string type. |
|
262 | - * |
|
263 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
264 | - * @return \ASN1\Type\Primitive\UTF8String |
|
265 | - */ |
|
266 | - public function asUTF8String(): Primitive\UTF8String |
|
267 | - { |
|
268 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
269 | - throw new \UnexpectedValueException( |
|
270 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
271 | - } |
|
272 | - return $this->_element; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Get the wrapped element as a relative OID type. |
|
277 | - * |
|
278 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
279 | - * @return \ASN1\Type\Primitive\RelativeOID |
|
280 | - */ |
|
281 | - public function asRelativeOID(): Primitive\RelativeOID |
|
282 | - { |
|
283 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
284 | - throw new \UnexpectedValueException( |
|
285 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
286 | - } |
|
287 | - return $this->_element; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get the wrapped element as a sequence type. |
|
292 | - * |
|
293 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
294 | - * @return \ASN1\Type\Constructed\Sequence |
|
295 | - */ |
|
296 | - public function asSequence(): Constructed\Sequence |
|
297 | - { |
|
298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | - throw new \UnexpectedValueException( |
|
300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | - } |
|
302 | - return $this->_element; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Get the wrapped element as a set type. |
|
307 | - * |
|
308 | - * @throws \UnexpectedValueException If the element is not a set |
|
309 | - * @return \ASN1\Type\Constructed\Set |
|
310 | - */ |
|
311 | - public function asSet(): Constructed\Set |
|
312 | - { |
|
313 | - if (!$this->_element instanceof Constructed\Set) { |
|
314 | - throw new \UnexpectedValueException( |
|
315 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
316 | - } |
|
317 | - return $this->_element; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Get the wrapped element as a numeric string type. |
|
322 | - * |
|
323 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
324 | - * @return \ASN1\Type\Primitive\NumericString |
|
325 | - */ |
|
326 | - public function asNumericString(): Primitive\NumericString |
|
327 | - { |
|
328 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
329 | - throw new \UnexpectedValueException( |
|
330 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
331 | - } |
|
332 | - return $this->_element; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Get the wrapped element as a printable string type. |
|
337 | - * |
|
338 | - * @throws \UnexpectedValueException If the element is not a printable |
|
339 | - * string |
|
340 | - * @return \ASN1\Type\Primitive\PrintableString |
|
341 | - */ |
|
342 | - public function asPrintableString(): Primitive\PrintableString |
|
343 | - { |
|
344 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
345 | - throw new \UnexpectedValueException( |
|
346 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
347 | - } |
|
348 | - return $this->_element; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get the wrapped element as a T61 string type. |
|
353 | - * |
|
354 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
355 | - * @return \ASN1\Type\Primitive\T61String |
|
356 | - */ |
|
357 | - public function asT61String(): Primitive\T61String |
|
358 | - { |
|
359 | - if (!$this->_element instanceof Primitive\T61String) { |
|
360 | - throw new \UnexpectedValueException( |
|
361 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
362 | - } |
|
363 | - return $this->_element; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Get the wrapped element as a videotex string type. |
|
368 | - * |
|
369 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
370 | - * @return \ASN1\Type\Primitive\VideotexString |
|
371 | - */ |
|
372 | - public function asVideotexString(): Primitive\VideotexString |
|
373 | - { |
|
374 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
375 | - throw new \UnexpectedValueException( |
|
376 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
377 | - } |
|
378 | - return $this->_element; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Get the wrapped element as a IA5 string type. |
|
383 | - * |
|
384 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
385 | - * @return \ASN1\Type\Primitive\IA5String |
|
386 | - */ |
|
387 | - public function asIA5String(): Primitive\IA5String |
|
388 | - { |
|
389 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
390 | - throw new \UnexpectedValueException( |
|
391 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
392 | - } |
|
393 | - return $this->_element; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Get the wrapped element as an UTC time type. |
|
398 | - * |
|
399 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
400 | - * @return \ASN1\Type\Primitive\UTCTime |
|
401 | - */ |
|
402 | - public function asUTCTime(): Primitive\UTCTime |
|
403 | - { |
|
404 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
405 | - throw new \UnexpectedValueException( |
|
406 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
407 | - } |
|
408 | - return $this->_element; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Get the wrapped element as a generalized time type. |
|
413 | - * |
|
414 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
415 | - * time |
|
416 | - * @return \ASN1\Type\Primitive\GeneralizedTime |
|
417 | - */ |
|
418 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
419 | - { |
|
420 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
421 | - throw new \UnexpectedValueException( |
|
422 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
423 | - } |
|
424 | - return $this->_element; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Get the wrapped element as a graphic string type. |
|
429 | - * |
|
430 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
431 | - * @return \ASN1\Type\Primitive\GraphicString |
|
432 | - */ |
|
433 | - public function asGraphicString(): Primitive\GraphicString |
|
434 | - { |
|
435 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
436 | - throw new \UnexpectedValueException( |
|
437 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
438 | - } |
|
439 | - return $this->_element; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Get the wrapped element as a visible string type. |
|
444 | - * |
|
445 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
446 | - * @return \ASN1\Type\Primitive\VisibleString |
|
447 | - */ |
|
448 | - public function asVisibleString(): Primitive\VisibleString |
|
449 | - { |
|
450 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
451 | - throw new \UnexpectedValueException( |
|
452 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
453 | - } |
|
454 | - return $this->_element; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the wrapped element as a general string type. |
|
459 | - * |
|
460 | - * @throws \UnexpectedValueException If the element is not general string |
|
461 | - * @return \ASN1\Type\Primitive\GeneralString |
|
462 | - */ |
|
463 | - public function asGeneralString(): Primitive\GeneralString |
|
464 | - { |
|
465 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
466 | - throw new \UnexpectedValueException( |
|
467 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
468 | - } |
|
469 | - return $this->_element; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Get the wrapped element as a universal string type. |
|
474 | - * |
|
475 | - * @throws \UnexpectedValueException If the element is not a universal |
|
476 | - * string |
|
477 | - * @return \ASN1\Type\Primitive\UniversalString |
|
478 | - */ |
|
479 | - public function asUniversalString(): Primitive\UniversalString |
|
480 | - { |
|
481 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
482 | - throw new \UnexpectedValueException( |
|
483 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
484 | - } |
|
485 | - return $this->_element; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * Get the wrapped element as a character string type. |
|
490 | - * |
|
491 | - * @throws \UnexpectedValueException If the element is not a character |
|
492 | - * string |
|
493 | - * @return \ASN1\Type\Primitive\CharacterString |
|
494 | - */ |
|
495 | - public function asCharacterString(): Primitive\CharacterString |
|
496 | - { |
|
497 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
498 | - throw new \UnexpectedValueException( |
|
499 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
500 | - } |
|
501 | - return $this->_element; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Get the wrapped element as a BMP string type. |
|
506 | - * |
|
507 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
508 | - * @return \ASN1\Type\Primitive\BMPString |
|
509 | - */ |
|
510 | - public function asBMPString(): Primitive\BMPString |
|
511 | - { |
|
512 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
513 | - throw new \UnexpectedValueException( |
|
514 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
515 | - } |
|
516 | - return $this->_element; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * Get the wrapped element as any string type. |
|
521 | - * |
|
522 | - * @throws \UnexpectedValueException If the element is not a string |
|
523 | - * @return StringType |
|
524 | - */ |
|
525 | - public function asString(): StringType |
|
526 | - { |
|
527 | - if (!$this->_element instanceof StringType) { |
|
528 | - throw new \UnexpectedValueException( |
|
529 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
530 | - } |
|
531 | - return $this->_element; |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * Get the wrapped element as any time type. |
|
536 | - * |
|
537 | - * @throws \UnexpectedValueException If the element is not a time |
|
538 | - * @return TimeType |
|
539 | - */ |
|
540 | - public function asTime(): TimeType |
|
541 | - { |
|
542 | - if (!$this->_element instanceof TimeType) { |
|
543 | - throw new \UnexpectedValueException( |
|
544 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
545 | - } |
|
546 | - return $this->_element; |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
551 | - * |
|
552 | - * @param int $tag Type tag of the expected element |
|
553 | - * @return string |
|
554 | - */ |
|
555 | - private function _generateExceptionMessage(int $tag): string |
|
556 | - { |
|
557 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
558 | - $this->_typeDescriptorString()); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Get textual description of the wrapped element for debugging purposes. |
|
563 | - * |
|
564 | - * @return string |
|
565 | - */ |
|
566 | - private function _typeDescriptorString(): string |
|
567 | - { |
|
568 | - $type_cls = $this->_element->typeClass(); |
|
569 | - $tag = $this->_element->tag(); |
|
570 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
571 | - return Element::tagToName($tag); |
|
572 | - } |
|
573 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
574 | - } |
|
575 | - |
|
576 | - /** |
|
577 | - * |
|
578 | - * @see \ASN1\Feature\Encodable::toDER() |
|
579 | - * @return string |
|
580 | - */ |
|
581 | - public function toDER(): string |
|
582 | - { |
|
583 | - return $this->_element->toDER(); |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * |
|
588 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
589 | - * @return int |
|
590 | - */ |
|
591 | - public function typeClass(): int |
|
592 | - { |
|
593 | - return $this->_element->typeClass(); |
|
594 | - } |
|
595 | - |
|
596 | - /** |
|
597 | - * |
|
598 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
599 | - * @return bool |
|
600 | - */ |
|
601 | - public function isConstructed(): bool |
|
602 | - { |
|
603 | - return $this->_element->isConstructed(); |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * |
|
608 | - * @see \ASN1\Feature\ElementBase::tag() |
|
609 | - * @return int |
|
610 | - */ |
|
611 | - public function tag(): int |
|
612 | - { |
|
613 | - return $this->_element->tag(); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * |
|
618 | - * {@inheritdoc} |
|
619 | - * @see \ASN1\Feature\ElementBase::isType() |
|
620 | - * @return bool |
|
621 | - */ |
|
622 | - public function isType(int $tag): bool |
|
623 | - { |
|
624 | - return $this->_element->isType($tag); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * |
|
629 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
630 | - * type strictness. |
|
631 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
632 | - * @return ElementBase |
|
633 | - */ |
|
634 | - public function expectType(int $tag): ElementBase |
|
635 | - { |
|
636 | - return $this->_element->expectType($tag); |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * |
|
641 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
642 | - * @return bool |
|
643 | - */ |
|
644 | - public function isTagged(): bool |
|
645 | - { |
|
646 | - return $this->_element->isTagged(); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * |
|
651 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
652 | - * type strictness. |
|
653 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
654 | - * @return TaggedType |
|
655 | - */ |
|
656 | - public function expectTagged($tag = null): TaggedType |
|
657 | - { |
|
658 | - return $this->_element->expectTagged($tag); |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * |
|
663 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
664 | - * @return Element |
|
665 | - */ |
|
666 | - public function asElement(): Element |
|
667 | - { |
|
668 | - return $this->_element; |
|
669 | - } |
|
670 | - |
|
671 | - /** |
|
672 | - * |
|
673 | - * {@inheritdoc} |
|
674 | - * @return UnspecifiedType |
|
675 | - */ |
|
676 | - public function asUnspecified(): UnspecifiedType |
|
677 | - { |
|
678 | - return $this; |
|
679 | - } |
|
19 | + /** |
|
20 | + * The wrapped element. |
|
21 | + * |
|
22 | + * @var Element |
|
23 | + */ |
|
24 | + private $_element; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param Element $el |
|
30 | + */ |
|
31 | + public function __construct(Element $el) |
|
32 | + { |
|
33 | + $this->_element = $el; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Initialize from DER data. |
|
38 | + * |
|
39 | + * @param string $data DER encoded data |
|
40 | + * @return self |
|
41 | + */ |
|
42 | + public static function fromDER(string $data): self |
|
43 | + { |
|
44 | + return Element::fromDER($data)->asUnspecified(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Initialize from ElementBase interface. |
|
49 | + * |
|
50 | + * @param ElementBase $el |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromElementBase(ElementBase $el): self |
|
54 | + { |
|
55 | + // if element is already wrapped |
|
56 | + if ($el instanceof self) { |
|
57 | + return $el; |
|
58 | + } |
|
59 | + return new self($el->asElement()); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Compatibility method to dispatch calls to the wrapped element. |
|
64 | + * |
|
65 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
66 | + * @param string $mtd Method name |
|
67 | + * @param array $args Arguments |
|
68 | + * @return mixed |
|
69 | + */ |
|
70 | + public function __call($mtd, array $args) |
|
71 | + { |
|
72 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Get the wrapped element as a context specific tagged type. |
|
77 | + * |
|
78 | + * @throws \UnexpectedValueException If the element is not tagged |
|
79 | + * @return TaggedType |
|
80 | + */ |
|
81 | + public function asTagged(): TaggedType |
|
82 | + { |
|
83 | + if (!$this->_element instanceof TaggedType) { |
|
84 | + throw new \UnexpectedValueException( |
|
85 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
86 | + } |
|
87 | + return $this->_element; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Get the wrapped element as an application specific type. |
|
92 | + * |
|
93 | + * @throws \UnexpectedValueException If element is not application specific |
|
94 | + * @return \ASN1\Type\Tagged\ApplicationType |
|
95 | + */ |
|
96 | + public function asApplication(): Tagged\ApplicationType |
|
97 | + { |
|
98 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
99 | + throw new \UnexpectedValueException( |
|
100 | + "Application type expected, got " . |
|
101 | + $this->_typeDescriptorString()); |
|
102 | + } |
|
103 | + return $this->_element; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get the wrapped element as a private tagged type. |
|
108 | + * |
|
109 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
110 | + * @return \ASN1\Type\Tagged\PrivateType |
|
111 | + */ |
|
112 | + public function asPrivate(): Tagged\PrivateType |
|
113 | + { |
|
114 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
115 | + throw new \UnexpectedValueException( |
|
116 | + "Private type expected, got " . $this->_typeDescriptorString()); |
|
117 | + } |
|
118 | + return $this->_element; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get the wrapped element as a boolean type. |
|
123 | + * |
|
124 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
125 | + * @return \ASN1\Type\Primitive\Boolean |
|
126 | + */ |
|
127 | + public function asBoolean(): Primitive\Boolean |
|
128 | + { |
|
129 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
130 | + throw new \UnexpectedValueException( |
|
131 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
132 | + } |
|
133 | + return $this->_element; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the wrapped element as an integer type. |
|
138 | + * |
|
139 | + * @throws \UnexpectedValueException If the element is not an integer |
|
140 | + * @return \ASN1\Type\Primitive\Integer |
|
141 | + */ |
|
142 | + public function asInteger(): Primitive\Integer |
|
143 | + { |
|
144 | + if (!$this->_element instanceof Primitive\Integer) { |
|
145 | + throw new \UnexpectedValueException( |
|
146 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
147 | + } |
|
148 | + return $this->_element; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Get the wrapped element as a bit string type. |
|
153 | + * |
|
154 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
155 | + * @return \ASN1\Type\Primitive\BitString |
|
156 | + */ |
|
157 | + public function asBitString(): Primitive\BitString |
|
158 | + { |
|
159 | + if (!$this->_element instanceof Primitive\BitString) { |
|
160 | + throw new \UnexpectedValueException( |
|
161 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
162 | + } |
|
163 | + return $this->_element; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get the wrapped element as an octet string type. |
|
168 | + * |
|
169 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
170 | + * @return \ASN1\Type\Primitive\OctetString |
|
171 | + */ |
|
172 | + public function asOctetString(): Primitive\OctetString |
|
173 | + { |
|
174 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
175 | + throw new \UnexpectedValueException( |
|
176 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
177 | + } |
|
178 | + return $this->_element; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the wrapped element as a null type. |
|
183 | + * |
|
184 | + * @throws \UnexpectedValueException If the element is not a null |
|
185 | + * @return \ASN1\Type\Primitive\NullType |
|
186 | + */ |
|
187 | + public function asNull(): Primitive\NullType |
|
188 | + { |
|
189 | + if (!$this->_element instanceof Primitive\NullType) { |
|
190 | + throw new \UnexpectedValueException( |
|
191 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
192 | + } |
|
193 | + return $this->_element; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Get the wrapped element as an object identifier type. |
|
198 | + * |
|
199 | + * @throws \UnexpectedValueException If the element is not an object |
|
200 | + * identifier |
|
201 | + * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
202 | + */ |
|
203 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
204 | + { |
|
205 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
206 | + throw new \UnexpectedValueException( |
|
207 | + $this->_generateExceptionMessage( |
|
208 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
209 | + } |
|
210 | + return $this->_element; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get the wrapped element as an object descriptor type. |
|
215 | + * |
|
216 | + * @throws \UnexpectedValueException If the element is not an object |
|
217 | + * descriptor |
|
218 | + * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
219 | + */ |
|
220 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
221 | + { |
|
222 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
223 | + throw new \UnexpectedValueException( |
|
224 | + $this->_generateExceptionMessage( |
|
225 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
226 | + } |
|
227 | + return $this->_element; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Get the wrapped element as a real type. |
|
232 | + * |
|
233 | + * @throws \UnexpectedValueException If the element is not a real |
|
234 | + * @return \ASN1\Type\Primitive\Real |
|
235 | + */ |
|
236 | + public function asReal(): Primitive\Real |
|
237 | + { |
|
238 | + if (!$this->_element instanceof Primitive\Real) { |
|
239 | + throw new \UnexpectedValueException( |
|
240 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
241 | + } |
|
242 | + return $this->_element; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Get the wrapped element as an enumerated type. |
|
247 | + * |
|
248 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
249 | + * @return \ASN1\Type\Primitive\Enumerated |
|
250 | + */ |
|
251 | + public function asEnumerated(): Primitive\Enumerated |
|
252 | + { |
|
253 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
254 | + throw new \UnexpectedValueException( |
|
255 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
256 | + } |
|
257 | + return $this->_element; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Get the wrapped element as a UTF8 string type. |
|
262 | + * |
|
263 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
264 | + * @return \ASN1\Type\Primitive\UTF8String |
|
265 | + */ |
|
266 | + public function asUTF8String(): Primitive\UTF8String |
|
267 | + { |
|
268 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
269 | + throw new \UnexpectedValueException( |
|
270 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
271 | + } |
|
272 | + return $this->_element; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Get the wrapped element as a relative OID type. |
|
277 | + * |
|
278 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
279 | + * @return \ASN1\Type\Primitive\RelativeOID |
|
280 | + */ |
|
281 | + public function asRelativeOID(): Primitive\RelativeOID |
|
282 | + { |
|
283 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
284 | + throw new \UnexpectedValueException( |
|
285 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
286 | + } |
|
287 | + return $this->_element; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get the wrapped element as a sequence type. |
|
292 | + * |
|
293 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
294 | + * @return \ASN1\Type\Constructed\Sequence |
|
295 | + */ |
|
296 | + public function asSequence(): Constructed\Sequence |
|
297 | + { |
|
298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | + throw new \UnexpectedValueException( |
|
300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | + } |
|
302 | + return $this->_element; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Get the wrapped element as a set type. |
|
307 | + * |
|
308 | + * @throws \UnexpectedValueException If the element is not a set |
|
309 | + * @return \ASN1\Type\Constructed\Set |
|
310 | + */ |
|
311 | + public function asSet(): Constructed\Set |
|
312 | + { |
|
313 | + if (!$this->_element instanceof Constructed\Set) { |
|
314 | + throw new \UnexpectedValueException( |
|
315 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
316 | + } |
|
317 | + return $this->_element; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Get the wrapped element as a numeric string type. |
|
322 | + * |
|
323 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
324 | + * @return \ASN1\Type\Primitive\NumericString |
|
325 | + */ |
|
326 | + public function asNumericString(): Primitive\NumericString |
|
327 | + { |
|
328 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
329 | + throw new \UnexpectedValueException( |
|
330 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
331 | + } |
|
332 | + return $this->_element; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Get the wrapped element as a printable string type. |
|
337 | + * |
|
338 | + * @throws \UnexpectedValueException If the element is not a printable |
|
339 | + * string |
|
340 | + * @return \ASN1\Type\Primitive\PrintableString |
|
341 | + */ |
|
342 | + public function asPrintableString(): Primitive\PrintableString |
|
343 | + { |
|
344 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
345 | + throw new \UnexpectedValueException( |
|
346 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
347 | + } |
|
348 | + return $this->_element; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get the wrapped element as a T61 string type. |
|
353 | + * |
|
354 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
355 | + * @return \ASN1\Type\Primitive\T61String |
|
356 | + */ |
|
357 | + public function asT61String(): Primitive\T61String |
|
358 | + { |
|
359 | + if (!$this->_element instanceof Primitive\T61String) { |
|
360 | + throw new \UnexpectedValueException( |
|
361 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
362 | + } |
|
363 | + return $this->_element; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Get the wrapped element as a videotex string type. |
|
368 | + * |
|
369 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
370 | + * @return \ASN1\Type\Primitive\VideotexString |
|
371 | + */ |
|
372 | + public function asVideotexString(): Primitive\VideotexString |
|
373 | + { |
|
374 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
375 | + throw new \UnexpectedValueException( |
|
376 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
377 | + } |
|
378 | + return $this->_element; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Get the wrapped element as a IA5 string type. |
|
383 | + * |
|
384 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
385 | + * @return \ASN1\Type\Primitive\IA5String |
|
386 | + */ |
|
387 | + public function asIA5String(): Primitive\IA5String |
|
388 | + { |
|
389 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
390 | + throw new \UnexpectedValueException( |
|
391 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
392 | + } |
|
393 | + return $this->_element; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Get the wrapped element as an UTC time type. |
|
398 | + * |
|
399 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
400 | + * @return \ASN1\Type\Primitive\UTCTime |
|
401 | + */ |
|
402 | + public function asUTCTime(): Primitive\UTCTime |
|
403 | + { |
|
404 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
405 | + throw new \UnexpectedValueException( |
|
406 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
407 | + } |
|
408 | + return $this->_element; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Get the wrapped element as a generalized time type. |
|
413 | + * |
|
414 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
415 | + * time |
|
416 | + * @return \ASN1\Type\Primitive\GeneralizedTime |
|
417 | + */ |
|
418 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
419 | + { |
|
420 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
421 | + throw new \UnexpectedValueException( |
|
422 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
423 | + } |
|
424 | + return $this->_element; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Get the wrapped element as a graphic string type. |
|
429 | + * |
|
430 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
431 | + * @return \ASN1\Type\Primitive\GraphicString |
|
432 | + */ |
|
433 | + public function asGraphicString(): Primitive\GraphicString |
|
434 | + { |
|
435 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
436 | + throw new \UnexpectedValueException( |
|
437 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
438 | + } |
|
439 | + return $this->_element; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Get the wrapped element as a visible string type. |
|
444 | + * |
|
445 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
446 | + * @return \ASN1\Type\Primitive\VisibleString |
|
447 | + */ |
|
448 | + public function asVisibleString(): Primitive\VisibleString |
|
449 | + { |
|
450 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
451 | + throw new \UnexpectedValueException( |
|
452 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
453 | + } |
|
454 | + return $this->_element; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the wrapped element as a general string type. |
|
459 | + * |
|
460 | + * @throws \UnexpectedValueException If the element is not general string |
|
461 | + * @return \ASN1\Type\Primitive\GeneralString |
|
462 | + */ |
|
463 | + public function asGeneralString(): Primitive\GeneralString |
|
464 | + { |
|
465 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
466 | + throw new \UnexpectedValueException( |
|
467 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
468 | + } |
|
469 | + return $this->_element; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Get the wrapped element as a universal string type. |
|
474 | + * |
|
475 | + * @throws \UnexpectedValueException If the element is not a universal |
|
476 | + * string |
|
477 | + * @return \ASN1\Type\Primitive\UniversalString |
|
478 | + */ |
|
479 | + public function asUniversalString(): Primitive\UniversalString |
|
480 | + { |
|
481 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
482 | + throw new \UnexpectedValueException( |
|
483 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
484 | + } |
|
485 | + return $this->_element; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * Get the wrapped element as a character string type. |
|
490 | + * |
|
491 | + * @throws \UnexpectedValueException If the element is not a character |
|
492 | + * string |
|
493 | + * @return \ASN1\Type\Primitive\CharacterString |
|
494 | + */ |
|
495 | + public function asCharacterString(): Primitive\CharacterString |
|
496 | + { |
|
497 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
498 | + throw new \UnexpectedValueException( |
|
499 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
500 | + } |
|
501 | + return $this->_element; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Get the wrapped element as a BMP string type. |
|
506 | + * |
|
507 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
508 | + * @return \ASN1\Type\Primitive\BMPString |
|
509 | + */ |
|
510 | + public function asBMPString(): Primitive\BMPString |
|
511 | + { |
|
512 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
513 | + throw new \UnexpectedValueException( |
|
514 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
515 | + } |
|
516 | + return $this->_element; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * Get the wrapped element as any string type. |
|
521 | + * |
|
522 | + * @throws \UnexpectedValueException If the element is not a string |
|
523 | + * @return StringType |
|
524 | + */ |
|
525 | + public function asString(): StringType |
|
526 | + { |
|
527 | + if (!$this->_element instanceof StringType) { |
|
528 | + throw new \UnexpectedValueException( |
|
529 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
530 | + } |
|
531 | + return $this->_element; |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * Get the wrapped element as any time type. |
|
536 | + * |
|
537 | + * @throws \UnexpectedValueException If the element is not a time |
|
538 | + * @return TimeType |
|
539 | + */ |
|
540 | + public function asTime(): TimeType |
|
541 | + { |
|
542 | + if (!$this->_element instanceof TimeType) { |
|
543 | + throw new \UnexpectedValueException( |
|
544 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
545 | + } |
|
546 | + return $this->_element; |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
551 | + * |
|
552 | + * @param int $tag Type tag of the expected element |
|
553 | + * @return string |
|
554 | + */ |
|
555 | + private function _generateExceptionMessage(int $tag): string |
|
556 | + { |
|
557 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
558 | + $this->_typeDescriptorString()); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Get textual description of the wrapped element for debugging purposes. |
|
563 | + * |
|
564 | + * @return string |
|
565 | + */ |
|
566 | + private function _typeDescriptorString(): string |
|
567 | + { |
|
568 | + $type_cls = $this->_element->typeClass(); |
|
569 | + $tag = $this->_element->tag(); |
|
570 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
571 | + return Element::tagToName($tag); |
|
572 | + } |
|
573 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
574 | + } |
|
575 | + |
|
576 | + /** |
|
577 | + * |
|
578 | + * @see \ASN1\Feature\Encodable::toDER() |
|
579 | + * @return string |
|
580 | + */ |
|
581 | + public function toDER(): string |
|
582 | + { |
|
583 | + return $this->_element->toDER(); |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * |
|
588 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
589 | + * @return int |
|
590 | + */ |
|
591 | + public function typeClass(): int |
|
592 | + { |
|
593 | + return $this->_element->typeClass(); |
|
594 | + } |
|
595 | + |
|
596 | + /** |
|
597 | + * |
|
598 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
599 | + * @return bool |
|
600 | + */ |
|
601 | + public function isConstructed(): bool |
|
602 | + { |
|
603 | + return $this->_element->isConstructed(); |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * |
|
608 | + * @see \ASN1\Feature\ElementBase::tag() |
|
609 | + * @return int |
|
610 | + */ |
|
611 | + public function tag(): int |
|
612 | + { |
|
613 | + return $this->_element->tag(); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * |
|
618 | + * {@inheritdoc} |
|
619 | + * @see \ASN1\Feature\ElementBase::isType() |
|
620 | + * @return bool |
|
621 | + */ |
|
622 | + public function isType(int $tag): bool |
|
623 | + { |
|
624 | + return $this->_element->isType($tag); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * |
|
629 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
630 | + * type strictness. |
|
631 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
632 | + * @return ElementBase |
|
633 | + */ |
|
634 | + public function expectType(int $tag): ElementBase |
|
635 | + { |
|
636 | + return $this->_element->expectType($tag); |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * |
|
641 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
642 | + * @return bool |
|
643 | + */ |
|
644 | + public function isTagged(): bool |
|
645 | + { |
|
646 | + return $this->_element->isTagged(); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * |
|
651 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
652 | + * type strictness. |
|
653 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
654 | + * @return TaggedType |
|
655 | + */ |
|
656 | + public function expectTagged($tag = null): TaggedType |
|
657 | + { |
|
658 | + return $this->_element->expectTagged($tag); |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * |
|
663 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
664 | + * @return Element |
|
665 | + */ |
|
666 | + public function asElement(): Element |
|
667 | + { |
|
668 | + return $this->_element; |
|
669 | + } |
|
670 | + |
|
671 | + /** |
|
672 | + * |
|
673 | + * {@inheritdoc} |
|
674 | + * @return UnspecifiedType |
|
675 | + */ |
|
676 | + public function asUnspecified(): UnspecifiedType |
|
677 | + { |
|
678 | + return $this; |
|
679 | + } |
|
680 | 680 | } |
@@ -13,41 +13,41 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class PrimitiveString extends StringType |
15 | 15 | { |
16 | - use PrimitiveType; |
|
16 | + use PrimitiveType; |
|
17 | 17 | |
18 | - /** |
|
19 | - * |
|
20 | - * @see \ASN1\Element::_encodedContentDER() |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - protected function _encodedContentDER(): string |
|
24 | - { |
|
25 | - return $this->_string; |
|
26 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * @see \ASN1\Element::_encodedContentDER() |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + protected function _encodedContentDER(): string |
|
24 | + { |
|
25 | + return $this->_string; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * {@inheritdoc} |
|
31 | - * @see \ASN1\Element::_decodeFromDER() |
|
32 | - * @return self |
|
33 | - */ |
|
34 | - protected static function _decodeFromDER(Identifier $identifier, |
|
35 | - string $data, int &$offset): ElementBase |
|
36 | - { |
|
37 | - $idx = $offset; |
|
38 | - if (!$identifier->isPrimitive()) { |
|
39 | - throw new DecodeException("DER encoded string must be primitive."); |
|
40 | - } |
|
41 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
42 | - $str = $length ? substr($data, $idx, $length) : ""; |
|
43 | - // substr should never return false, since length is |
|
44 | - // checked by Length::expectFromDER. |
|
45 | - assert(is_string($str), new DecodeException("substr")); |
|
46 | - $offset = $idx + $length; |
|
47 | - try { |
|
48 | - return new static($str); |
|
49 | - } catch (\InvalidArgumentException $e) { |
|
50 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
51 | - } |
|
52 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * {@inheritdoc} |
|
31 | + * @see \ASN1\Element::_decodeFromDER() |
|
32 | + * @return self |
|
33 | + */ |
|
34 | + protected static function _decodeFromDER(Identifier $identifier, |
|
35 | + string $data, int &$offset): ElementBase |
|
36 | + { |
|
37 | + $idx = $offset; |
|
38 | + if (!$identifier->isPrimitive()) { |
|
39 | + throw new DecodeException("DER encoded string must be primitive."); |
|
40 | + } |
|
41 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
42 | + $str = $length ? substr($data, $idx, $length) : ""; |
|
43 | + // substr should never return false, since length is |
|
44 | + // checked by Length::expectFromDER. |
|
45 | + assert(is_string($str), new DecodeException("substr")); |
|
46 | + $offset = $idx + $length; |
|
47 | + try { |
|
48 | + return new static($str); |
|
49 | + } catch (\InvalidArgumentException $e) { |
|
50 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | } |
@@ -16,273 +16,273 @@ |
||
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): self |
|
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): self |
|
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, |
|
117 | - string $data, int &$offset): ElementBase |
|
118 | - { |
|
119 | - $idx = $offset; |
|
120 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
121 | - // if length is zero, value is zero (spec 8.5.2) |
|
122 | - if (!$length) { |
|
123 | - $obj = new self(self::NR3_ZERO); |
|
124 | - } else { |
|
125 | - $bytes = substr($data, $idx, $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; |
|
136 | - return $obj; |
|
137 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * {@inheritdoc} |
|
114 | + * @return self |
|
115 | + */ |
|
116 | + protected static function _decodeFromDER(Identifier $identifier, |
|
117 | + string $data, int &$offset): ElementBase |
|
118 | + { |
|
119 | + $idx = $offset; |
|
120 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
121 | + // if length is zero, value is zero (spec 8.5.2) |
|
122 | + if (!$length) { |
|
123 | + $obj = new self(self::NR3_ZERO); |
|
124 | + } else { |
|
125 | + $bytes = substr($data, $idx, $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; |
|
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 | - /** @var $match string[] */ |
|
197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | - $parts = explode(".", $match[1]); |
|
199 | - $m = ltrim($parts[0], "0"); |
|
200 | - $e = intval($match[2]); |
|
201 | - // if mantissa had decimals |
|
202 | - if (count($parts) == 2) { |
|
203 | - $d = rtrim($parts[1], "0"); |
|
204 | - $e -= strlen($d); |
|
205 | - $m .= $d; |
|
206 | - } |
|
207 | - } else { |
|
208 | - // explode from decimal |
|
209 | - $parts = explode(".", $str); |
|
210 | - $m = ltrim($parts[0], "0"); |
|
211 | - // if number had decimals |
|
212 | - if (count($parts) == 2) { |
|
213 | - // exponent is negative number of the decimals |
|
214 | - $e = -strlen($parts[1]); |
|
215 | - // append decimals to the mantissa |
|
216 | - $m .= $parts[1]; |
|
217 | - } else { |
|
218 | - $e = 0; |
|
219 | - } |
|
220 | - // shift trailing zeroes from the mantissa to the exponent |
|
221 | - while (substr($m, -1) === "0") { |
|
222 | - $e++; |
|
223 | - $m = substr($m, 0, -1); |
|
224 | - } |
|
225 | - } |
|
226 | - /* 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 | + /** @var $match string[] */ |
|
197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | + $parts = explode(".", $match[1]); |
|
199 | + $m = ltrim($parts[0], "0"); |
|
200 | + $e = intval($match[2]); |
|
201 | + // if mantissa had decimals |
|
202 | + if (count($parts) == 2) { |
|
203 | + $d = rtrim($parts[1], "0"); |
|
204 | + $e -= strlen($d); |
|
205 | + $m .= $d; |
|
206 | + } |
|
207 | + } else { |
|
208 | + // explode from decimal |
|
209 | + $parts = explode(".", $str); |
|
210 | + $m = ltrim($parts[0], "0"); |
|
211 | + // if number had decimals |
|
212 | + if (count($parts) == 2) { |
|
213 | + // exponent is negative number of the decimals |
|
214 | + $e = -strlen($parts[1]); |
|
215 | + // append decimals to the mantissa |
|
216 | + $m .= $parts[1]; |
|
217 | + } else { |
|
218 | + $e = 0; |
|
219 | + } |
|
220 | + // shift trailing zeroes from the mantissa to the exponent |
|
221 | + while (substr($m, -1) === "0") { |
|
222 | + $e++; |
|
223 | + $m = substr($m, 0, -1); |
|
224 | + } |
|
225 | + } |
|
226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
228 | - if (0 == $e) { |
|
229 | - $es = "+"; |
|
230 | - } else { |
|
231 | - $es = $e < 0 ? "-" : ""; |
|
232 | - } |
|
233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | - } |
|
228 | + if (0 == $e) { |
|
229 | + $es = "+"; |
|
230 | + } else { |
|
231 | + $es = $e < 0 ? "-" : ""; |
|
232 | + } |
|
233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * Convert NR3 form number to decimal. |
|
238 | - * |
|
239 | - * @param string $str |
|
240 | - * @throws \UnexpectedValueException |
|
241 | - * @return float |
|
242 | - */ |
|
243 | - private static function _nr3ToDecimal(string $str): float |
|
244 | - { |
|
245 | - /** @var $match string[] */ |
|
246 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
247 | - throw new \UnexpectedValueException( |
|
248 | - "'$str' is not a valid NR3 form real."); |
|
249 | - } |
|
250 | - $m = $match[2]; |
|
251 | - // if number started with minus sign |
|
252 | - $inv = $match[1] == "-"; |
|
253 | - $e = intval($match[3]); |
|
254 | - // positive exponent |
|
255 | - if ($e > 0) { |
|
256 | - // pad with trailing zeroes |
|
257 | - $num = $m . str_repeat("0", $e); |
|
258 | - } else if ($e < 0) { |
|
259 | - // pad with leading zeroes |
|
260 | - if (strlen($m) < abs($e)) { |
|
261 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
262 | - } |
|
263 | - // insert decimal point |
|
264 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
265 | - } else { |
|
266 | - $num = empty($m) ? "0" : $m; |
|
267 | - } |
|
268 | - // if number is negative |
|
269 | - if ($inv) { |
|
270 | - $num = "-$num"; |
|
271 | - } |
|
272 | - return floatval($num); |
|
273 | - } |
|
236 | + /** |
|
237 | + * Convert NR3 form number to decimal. |
|
238 | + * |
|
239 | + * @param string $str |
|
240 | + * @throws \UnexpectedValueException |
|
241 | + * @return float |
|
242 | + */ |
|
243 | + private static function _nr3ToDecimal(string $str): float |
|
244 | + { |
|
245 | + /** @var $match string[] */ |
|
246 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
247 | + throw new \UnexpectedValueException( |
|
248 | + "'$str' is not a valid NR3 form real."); |
|
249 | + } |
|
250 | + $m = $match[2]; |
|
251 | + // if number started with minus sign |
|
252 | + $inv = $match[1] == "-"; |
|
253 | + $e = intval($match[3]); |
|
254 | + // positive exponent |
|
255 | + if ($e > 0) { |
|
256 | + // pad with trailing zeroes |
|
257 | + $num = $m . str_repeat("0", $e); |
|
258 | + } else if ($e < 0) { |
|
259 | + // pad with leading zeroes |
|
260 | + if (strlen($m) < abs($e)) { |
|
261 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
262 | + } |
|
263 | + // insert decimal point |
|
264 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
265 | + } else { |
|
266 | + $num = empty($m) ? "0" : $m; |
|
267 | + } |
|
268 | + // if number is negative |
|
269 | + if ($inv) { |
|
270 | + $num = "-$num"; |
|
271 | + } |
|
272 | + return floatval($num); |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Test that number is valid for this context. |
|
277 | - * |
|
278 | - * @param mixed $num |
|
279 | - * @return bool |
|
280 | - */ |
|
281 | - private static function _validateNumber($num): bool |
|
282 | - { |
|
283 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
284 | - return false; |
|
285 | - } |
|
286 | - return true; |
|
287 | - } |
|
275 | + /** |
|
276 | + * Test that number is valid for this context. |
|
277 | + * |
|
278 | + * @param mixed $num |
|
279 | + * @return bool |
|
280 | + */ |
|
281 | + private static function _validateNumber($num): bool |
|
282 | + { |
|
283 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
284 | + return false; |
|
285 | + } |
|
286 | + return true; |
|
287 | + } |
|
288 | 288 | } |