@@ -12,56 +12,56 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor. |
|
17 | - * |
|
18 | - * @param Element ...$elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor. |
|
17 | + * |
|
18 | + * @param Element ...$elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * |
|
29 | - * Used for DER encoding of *SET* type. |
|
30 | - * |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function sortedSet(): self |
|
34 | - { |
|
35 | - $obj = clone $this; |
|
36 | - usort($obj->_elements, |
|
37 | - function (Element $a, Element $b) { |
|
38 | - if ($a->typeClass() !== $b->typeClass()) { |
|
39 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | - } |
|
41 | - if ($a->tag() === $b->tag()) { |
|
42 | - return 0; |
|
43 | - } |
|
44 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | - }); |
|
46 | - return $obj; |
|
47 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * |
|
29 | + * Used for DER encoding of *SET* type. |
|
30 | + * |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function sortedSet(): self |
|
34 | + { |
|
35 | + $obj = clone $this; |
|
36 | + usort($obj->_elements, |
|
37 | + function (Element $a, Element $b) { |
|
38 | + if ($a->typeClass() !== $b->typeClass()) { |
|
39 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | + } |
|
41 | + if ($a->tag() === $b->tag()) { |
|
42 | + return 0; |
|
43 | + } |
|
44 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | + }); |
|
46 | + return $obj; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Sort by encoding ascending order. |
|
51 | - * |
|
52 | - * Used for DER encoding of *SET OF* type. |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public function sortedSetOf(): self |
|
57 | - { |
|
58 | - $obj = clone $this; |
|
59 | - usort($obj->_elements, |
|
60 | - function (Element $a, Element $b) { |
|
61 | - $a_der = $a->toDER(); |
|
62 | - $b_der = $b->toDER(); |
|
63 | - return strcmp($a_der, $b_der); |
|
64 | - }); |
|
65 | - return $obj; |
|
66 | - } |
|
49 | + /** |
|
50 | + * Sort by encoding ascending order. |
|
51 | + * |
|
52 | + * Used for DER encoding of *SET OF* type. |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public function sortedSetOf(): self |
|
57 | + { |
|
58 | + $obj = clone $this; |
|
59 | + usort($obj->_elements, |
|
60 | + function (Element $a, Element $b) { |
|
61 | + $a_der = $a->toDER(); |
|
62 | + $b_der = $b->toDER(); |
|
63 | + return strcmp($a_der, $b_der); |
|
64 | + }); |
|
65 | + return $obj; |
|
66 | + } |
|
67 | 67 | } |
@@ -31,106 +31,106 @@ |
||
31 | 31 | */ |
32 | 32 | class ConstructedString extends Structure implements StringType |
33 | 33 | { |
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @internal Use `create()` or `createWithTag()` method instead |
|
38 | - * |
|
39 | - * @param Element ...$elements Any number of elements |
|
40 | - */ |
|
41 | - protected function __construct(Element ...$elements) |
|
42 | - { |
|
43 | - parent::__construct(...$elements); |
|
44 | - } |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @internal Use `create()` or `createWithTag()` method instead |
|
38 | + * |
|
39 | + * @param Element ...$elements Any number of elements |
|
40 | + */ |
|
41 | + protected function __construct(Element ...$elements) |
|
42 | + { |
|
43 | + parent::__construct(...$elements); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - public function __toString(): string |
|
50 | - { |
|
51 | - return $this->string(); |
|
52 | - } |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + public function __toString(): string |
|
50 | + { |
|
51 | + return $this->string(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Create from a list of string type elements. |
|
56 | - * |
|
57 | - * All strings must have the same type. |
|
58 | - * |
|
59 | - * @param StringType ...$elements |
|
60 | - * |
|
61 | - * @throws \LogicException |
|
62 | - * |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - public static function create(StringType ...$elements): self |
|
66 | - { |
|
67 | - if (!count($elements)) { |
|
68 | - throw new \LogicException( |
|
69 | - 'No elements, unable to determine type tag.'); |
|
70 | - } |
|
71 | - $tag = $elements[0]->tag(); |
|
72 | - foreach ($elements as $el) { |
|
73 | - if ($el->tag() !== $tag) { |
|
74 | - throw new \LogicException( |
|
75 | - 'All elements in constructed string must have the same type.'); |
|
76 | - } |
|
77 | - } |
|
78 | - return self::createWithTag($tag, ...$elements); |
|
79 | - } |
|
54 | + /** |
|
55 | + * Create from a list of string type elements. |
|
56 | + * |
|
57 | + * All strings must have the same type. |
|
58 | + * |
|
59 | + * @param StringType ...$elements |
|
60 | + * |
|
61 | + * @throws \LogicException |
|
62 | + * |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + public static function create(StringType ...$elements): self |
|
66 | + { |
|
67 | + if (!count($elements)) { |
|
68 | + throw new \LogicException( |
|
69 | + 'No elements, unable to determine type tag.'); |
|
70 | + } |
|
71 | + $tag = $elements[0]->tag(); |
|
72 | + foreach ($elements as $el) { |
|
73 | + if ($el->tag() !== $tag) { |
|
74 | + throw new \LogicException( |
|
75 | + 'All elements in constructed string must have the same type.'); |
|
76 | + } |
|
77 | + } |
|
78 | + return self::createWithTag($tag, ...$elements); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Create from strings with a given type tag. |
|
83 | - * |
|
84 | - * Does not perform any validation on types. |
|
85 | - * |
|
86 | - * @param int $tag Type tag for the constructed string element |
|
87 | - * @param StringType ...$elements Any number of elements |
|
88 | - * |
|
89 | - * @return self |
|
90 | - */ |
|
91 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
92 | - { |
|
93 | - $el = new self(...$elements); |
|
94 | - $el->_typeTag = $tag; |
|
95 | - return $el; |
|
96 | - } |
|
81 | + /** |
|
82 | + * Create from strings with a given type tag. |
|
83 | + * |
|
84 | + * Does not perform any validation on types. |
|
85 | + * |
|
86 | + * @param int $tag Type tag for the constructed string element |
|
87 | + * @param StringType ...$elements Any number of elements |
|
88 | + * |
|
89 | + * @return self |
|
90 | + */ |
|
91 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
92 | + { |
|
93 | + $el = new self(...$elements); |
|
94 | + $el->_typeTag = $tag; |
|
95 | + return $el; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get a list of strings in this structure. |
|
100 | - * |
|
101 | - * @return string[] |
|
102 | - */ |
|
103 | - public function strings(): array |
|
104 | - { |
|
105 | - return array_map(function (StringType $el) { |
|
106 | - return $el->string(); |
|
107 | - }, $this->_elements); |
|
108 | - } |
|
98 | + /** |
|
99 | + * Get a list of strings in this structure. |
|
100 | + * |
|
101 | + * @return string[] |
|
102 | + */ |
|
103 | + public function strings(): array |
|
104 | + { |
|
105 | + return array_map(function (StringType $el) { |
|
106 | + return $el->string(); |
|
107 | + }, $this->_elements); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Get the contained strings concatenated together. |
|
112 | - * |
|
113 | - * NOTE: It's unclear how bit strings with unused bits should be concatenated. |
|
114 | - * |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function string(): string |
|
118 | - { |
|
119 | - return implode('', $this->strings()); |
|
120 | - } |
|
110 | + /** |
|
111 | + * Get the contained strings concatenated together. |
|
112 | + * |
|
113 | + * NOTE: It's unclear how bit strings with unused bits should be concatenated. |
|
114 | + * |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function string(): string |
|
118 | + { |
|
119 | + return implode('', $this->strings()); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * {@inheritdoc} |
|
124 | - * |
|
125 | - * @return self |
|
126 | - */ |
|
127 | - protected static function _decodeFromDER(Identifier $identifier, |
|
128 | - string $data, int &$offset): ElementBase |
|
129 | - { |
|
130 | - /** @var ConstructedString $type */ |
|
131 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
132 | - [$identifier, $data, &$offset]); |
|
133 | - $type->_typeTag = $identifier->intTag(); |
|
134 | - return $type; |
|
135 | - } |
|
122 | + /** |
|
123 | + * {@inheritdoc} |
|
124 | + * |
|
125 | + * @return self |
|
126 | + */ |
|
127 | + protected static function _decodeFromDER(Identifier $identifier, |
|
128 | + string $data, int &$offset): ElementBase |
|
129 | + { |
|
130 | + /** @var ConstructedString $type */ |
|
131 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
132 | + [$identifier, $data, &$offset]); |
|
133 | + $type->_typeTag = $identifier->intTag(); |
|
134 | + return $type; |
|
135 | + } |
|
136 | 136 | } |
@@ -13,15 +13,15 @@ |
||
13 | 13 | */ |
14 | 14 | interface ImplicitTagging extends ElementBase |
15 | 15 | { |
16 | - /** |
|
17 | - * Get implicitly tagged wrapped element. |
|
18 | - * |
|
19 | - * @param int $tag Tag of the element |
|
20 | - * @param int $class Expected type class of the element |
|
21 | - * |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * |
|
24 | - * @return UnspecifiedType |
|
25 | - */ |
|
26 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
16 | + /** |
|
17 | + * Get implicitly tagged wrapped element. |
|
18 | + * |
|
19 | + * @param int $tag Tag of the element |
|
20 | + * @param int $class Expected type class of the element |
|
21 | + * |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * |
|
24 | + * @return UnspecifiedType |
|
25 | + */ |
|
26 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
27 | 27 | } |
@@ -9,10 +9,10 @@ |
||
9 | 9 | */ |
10 | 10 | interface TimeType extends StringType |
11 | 11 | { |
12 | - /** |
|
13 | - * Get the date and time. |
|
14 | - * |
|
15 | - * @return \DateTimeImmutable |
|
16 | - */ |
|
17 | - public function dateTime(): \DateTimeImmutable; |
|
12 | + /** |
|
13 | + * Get the date and time. |
|
14 | + * |
|
15 | + * @return \DateTimeImmutable |
|
16 | + */ |
|
17 | + public function dateTime(): \DateTimeImmutable; |
|
18 | 18 | } |
@@ -11,58 +11,58 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class BaseString extends Element implements StringType |
13 | 13 | { |
14 | - /** |
|
15 | - * String value. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_string; |
|
14 | + /** |
|
15 | + * String value. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Constructor. |
|
23 | - * |
|
24 | - * @param string $string |
|
25 | - * |
|
26 | - * @throws \InvalidArgumentException |
|
27 | - */ |
|
28 | - public function __construct(string $string) |
|
29 | - { |
|
30 | - if (!$this->_validateString($string)) { |
|
31 | - throw new \InvalidArgumentException( |
|
32 | - sprintf('Not a valid %s string.', |
|
33 | - self::tagToName($this->_typeTag))); |
|
34 | - } |
|
35 | - $this->_string = $string; |
|
36 | - } |
|
21 | + /** |
|
22 | + * Constructor. |
|
23 | + * |
|
24 | + * @param string $string |
|
25 | + * |
|
26 | + * @throws \InvalidArgumentException |
|
27 | + */ |
|
28 | + public function __construct(string $string) |
|
29 | + { |
|
30 | + if (!$this->_validateString($string)) { |
|
31 | + throw new \InvalidArgumentException( |
|
32 | + sprintf('Not a valid %s string.', |
|
33 | + self::tagToName($this->_typeTag))); |
|
34 | + } |
|
35 | + $this->_string = $string; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * {@inheritdoc} |
|
40 | - */ |
|
41 | - public function __toString(): string |
|
42 | - { |
|
43 | - return $this->string(); |
|
44 | - } |
|
38 | + /** |
|
39 | + * {@inheritdoc} |
|
40 | + */ |
|
41 | + public function __toString(): string |
|
42 | + { |
|
43 | + return $this->string(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the string value. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function string(): string |
|
52 | - { |
|
53 | - return $this->_string; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get the string value. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function string(): string |
|
52 | + { |
|
53 | + return $this->_string; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Check whether string is valid for the concrete type. |
|
58 | - * |
|
59 | - * @param string $string |
|
60 | - * |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - protected function _validateString(string $string): bool |
|
64 | - { |
|
65 | - // Override in derived classes |
|
66 | - return true; |
|
67 | - } |
|
56 | + /** |
|
57 | + * Check whether string is valid for the concrete type. |
|
58 | + * |
|
59 | + * @param string $string |
|
60 | + * |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + protected function _validateString(string $string): bool |
|
64 | + { |
|
65 | + // Override in derived classes |
|
66 | + return true; |
|
67 | + } |
|
68 | 68 | } |
@@ -17,676 +17,676 @@ |
||
17 | 17 | */ |
18 | 18 | class UnspecifiedType implements ElementBase |
19 | 19 | { |
20 | - /** |
|
21 | - * The wrapped element. |
|
22 | - * |
|
23 | - * @var Element |
|
24 | - */ |
|
25 | - private $_element; |
|
26 | - |
|
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * @param Element $el |
|
31 | - */ |
|
32 | - public function __construct(Element $el) |
|
33 | - { |
|
34 | - $this->_element = $el; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Initialize from DER data. |
|
39 | - * |
|
40 | - * @param string $data DER encoded data |
|
41 | - * |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - public static function fromDER(string $data): self |
|
45 | - { |
|
46 | - return Element::fromDER($data)->asUnspecified(); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Initialize from `ElementBase` interface. |
|
51 | - * |
|
52 | - * @param ElementBase $el |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public static function fromElementBase(ElementBase $el): self |
|
57 | - { |
|
58 | - // if element is already wrapped |
|
59 | - if ($el instanceof self) { |
|
60 | - return $el; |
|
61 | - } |
|
62 | - return new self($el->asElement()); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the wrapped element as a context specific tagged type. |
|
67 | - * |
|
68 | - * @throws \UnexpectedValueException If the element is not tagged |
|
69 | - * |
|
70 | - * @return TaggedType |
|
71 | - */ |
|
72 | - public function asTagged(): TaggedType |
|
73 | - { |
|
74 | - if (!$this->_element instanceof TaggedType) { |
|
75 | - throw new \UnexpectedValueException( |
|
76 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
77 | - } |
|
78 | - return $this->_element; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get the wrapped element as an application specific type. |
|
83 | - * |
|
84 | - * @throws \UnexpectedValueException If element is not application specific |
|
85 | - * |
|
86 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
87 | - */ |
|
88 | - public function asApplication(): Tagged\ApplicationType |
|
89 | - { |
|
90 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
91 | - throw new \UnexpectedValueException( |
|
92 | - 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
93 | - } |
|
94 | - return $this->_element; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get the wrapped element as a private tagged type. |
|
99 | - * |
|
100 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
101 | - * |
|
102 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
103 | - */ |
|
104 | - public function asPrivate(): Tagged\PrivateType |
|
105 | - { |
|
106 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
107 | - throw new \UnexpectedValueException( |
|
108 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
109 | - } |
|
110 | - return $this->_element; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the wrapped element as a boolean type. |
|
115 | - * |
|
116 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
117 | - * |
|
118 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
119 | - */ |
|
120 | - public function asBoolean(): Primitive\Boolean |
|
121 | - { |
|
122 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
123 | - throw new \UnexpectedValueException( |
|
124 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
125 | - } |
|
126 | - return $this->_element; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get the wrapped element as an integer type. |
|
131 | - * |
|
132 | - * @throws \UnexpectedValueException If the element is not an integer |
|
133 | - * |
|
134 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
135 | - */ |
|
136 | - public function asInteger(): Primitive\Integer |
|
137 | - { |
|
138 | - if (!$this->_element instanceof Primitive\Integer) { |
|
139 | - throw new \UnexpectedValueException( |
|
140 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
141 | - } |
|
142 | - return $this->_element; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get the wrapped element as a bit string type. |
|
147 | - * |
|
148 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
149 | - * |
|
150 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
151 | - */ |
|
152 | - public function asBitString(): Primitive\BitString |
|
153 | - { |
|
154 | - if (!$this->_element instanceof Primitive\BitString) { |
|
155 | - throw new \UnexpectedValueException( |
|
156 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
157 | - } |
|
158 | - return $this->_element; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the wrapped element as an octet string type. |
|
163 | - * |
|
164 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
165 | - * |
|
166 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
167 | - */ |
|
168 | - public function asOctetString(): Primitive\OctetString |
|
169 | - { |
|
170 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
171 | - throw new \UnexpectedValueException( |
|
172 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
173 | - } |
|
174 | - return $this->_element; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get the wrapped element as a null type. |
|
179 | - * |
|
180 | - * @throws \UnexpectedValueException If the element is not a null |
|
181 | - * |
|
182 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
183 | - */ |
|
184 | - public function asNull(): Primitive\NullType |
|
185 | - { |
|
186 | - if (!$this->_element instanceof Primitive\NullType) { |
|
187 | - throw new \UnexpectedValueException( |
|
188 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
189 | - } |
|
190 | - return $this->_element; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Get the wrapped element as an object identifier type. |
|
195 | - * |
|
196 | - * @throws \UnexpectedValueException If the element is not an object identifier |
|
197 | - * |
|
198 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
199 | - */ |
|
200 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
201 | - { |
|
202 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
203 | - throw new \UnexpectedValueException( |
|
204 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
205 | - } |
|
206 | - return $this->_element; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get the wrapped element as an object descriptor type. |
|
211 | - * |
|
212 | - * @throws \UnexpectedValueException If the element is not an object descriptor |
|
213 | - * |
|
214 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
215 | - */ |
|
216 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
217 | - { |
|
218 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
219 | - throw new \UnexpectedValueException( |
|
220 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
221 | - } |
|
222 | - return $this->_element; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Get the wrapped element as a real type. |
|
227 | - * |
|
228 | - * @throws \UnexpectedValueException If the element is not a real |
|
229 | - * |
|
230 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
231 | - */ |
|
232 | - public function asReal(): Primitive\Real |
|
233 | - { |
|
234 | - if (!$this->_element instanceof Primitive\Real) { |
|
235 | - throw new \UnexpectedValueException( |
|
236 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
237 | - } |
|
238 | - return $this->_element; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Get the wrapped element as an enumerated type. |
|
243 | - * |
|
244 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
245 | - * |
|
246 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
247 | - */ |
|
248 | - public function asEnumerated(): Primitive\Enumerated |
|
249 | - { |
|
250 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
251 | - throw new \UnexpectedValueException( |
|
252 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
253 | - } |
|
254 | - return $this->_element; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Get the wrapped element as a UTF8 string type. |
|
259 | - * |
|
260 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
261 | - * |
|
262 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
263 | - */ |
|
264 | - public function asUTF8String(): Primitive\UTF8String |
|
265 | - { |
|
266 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
267 | - throw new \UnexpectedValueException( |
|
268 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
269 | - } |
|
270 | - return $this->_element; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get the wrapped element as a relative OID type. |
|
275 | - * |
|
276 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
277 | - * |
|
278 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
279 | - */ |
|
280 | - public function asRelativeOID(): Primitive\RelativeOID |
|
281 | - { |
|
282 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
283 | - throw new \UnexpectedValueException( |
|
284 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
285 | - } |
|
286 | - return $this->_element; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Get the wrapped element as a sequence type. |
|
291 | - * |
|
292 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
293 | - * |
|
294 | - * @return \Sop\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 | - * |
|
310 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
311 | - */ |
|
312 | - public function asSet(): Constructed\Set |
|
313 | - { |
|
314 | - if (!$this->_element instanceof Constructed\Set) { |
|
315 | - throw new \UnexpectedValueException( |
|
316 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
317 | - } |
|
318 | - return $this->_element; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Get the wrapped element as a numeric string type. |
|
323 | - * |
|
324 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
325 | - * |
|
326 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
327 | - */ |
|
328 | - public function asNumericString(): Primitive\NumericString |
|
329 | - { |
|
330 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
331 | - throw new \UnexpectedValueException( |
|
332 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
333 | - } |
|
334 | - return $this->_element; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Get the wrapped element as a printable string type. |
|
339 | - * |
|
340 | - * @throws \UnexpectedValueException If the element is not a printable string |
|
341 | - * |
|
342 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
343 | - */ |
|
344 | - public function asPrintableString(): Primitive\PrintableString |
|
345 | - { |
|
346 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
347 | - throw new \UnexpectedValueException( |
|
348 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
349 | - } |
|
350 | - return $this->_element; |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Get the wrapped element as a T61 string type. |
|
355 | - * |
|
356 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
357 | - * |
|
358 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
359 | - */ |
|
360 | - public function asT61String(): Primitive\T61String |
|
361 | - { |
|
362 | - if (!$this->_element instanceof Primitive\T61String) { |
|
363 | - throw new \UnexpectedValueException( |
|
364 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
365 | - } |
|
366 | - return $this->_element; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Get the wrapped element as a videotex string type. |
|
371 | - * |
|
372 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
373 | - * |
|
374 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
375 | - */ |
|
376 | - public function asVideotexString(): Primitive\VideotexString |
|
377 | - { |
|
378 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
379 | - throw new \UnexpectedValueException( |
|
380 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
381 | - } |
|
382 | - return $this->_element; |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Get the wrapped element as a IA5 string type. |
|
387 | - * |
|
388 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
389 | - * |
|
390 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
391 | - */ |
|
392 | - public function asIA5String(): Primitive\IA5String |
|
393 | - { |
|
394 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
395 | - throw new \UnexpectedValueException( |
|
396 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
397 | - } |
|
398 | - return $this->_element; |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Get the wrapped element as an UTC time type. |
|
403 | - * |
|
404 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
405 | - * |
|
406 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
407 | - */ |
|
408 | - public function asUTCTime(): Primitive\UTCTime |
|
409 | - { |
|
410 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
411 | - throw new \UnexpectedValueException( |
|
412 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
413 | - } |
|
414 | - return $this->_element; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Get the wrapped element as a generalized time type. |
|
419 | - * |
|
420 | - * @throws \UnexpectedValueException If the element is not a generalized time |
|
421 | - * |
|
422 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
423 | - */ |
|
424 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
425 | - { |
|
426 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
427 | - throw new \UnexpectedValueException( |
|
428 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
429 | - } |
|
430 | - return $this->_element; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Get the wrapped element as a graphic string type. |
|
435 | - * |
|
436 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
437 | - * |
|
438 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
439 | - */ |
|
440 | - public function asGraphicString(): Primitive\GraphicString |
|
441 | - { |
|
442 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
443 | - throw new \UnexpectedValueException( |
|
444 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
445 | - } |
|
446 | - return $this->_element; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Get the wrapped element as a visible string type. |
|
451 | - * |
|
452 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
453 | - * |
|
454 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
455 | - */ |
|
456 | - public function asVisibleString(): Primitive\VisibleString |
|
457 | - { |
|
458 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
459 | - throw new \UnexpectedValueException( |
|
460 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
461 | - } |
|
462 | - return $this->_element; |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Get the wrapped element as a general string type. |
|
467 | - * |
|
468 | - * @throws \UnexpectedValueException If the element is not general string |
|
469 | - * |
|
470 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
471 | - */ |
|
472 | - public function asGeneralString(): Primitive\GeneralString |
|
473 | - { |
|
474 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
475 | - throw new \UnexpectedValueException( |
|
476 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
477 | - } |
|
478 | - return $this->_element; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Get the wrapped element as a universal string type. |
|
483 | - * |
|
484 | - * @throws \UnexpectedValueException If the element is not a universal string |
|
485 | - * |
|
486 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
487 | - */ |
|
488 | - public function asUniversalString(): Primitive\UniversalString |
|
489 | - { |
|
490 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
491 | - throw new \UnexpectedValueException( |
|
492 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
493 | - } |
|
494 | - return $this->_element; |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Get the wrapped element as a character string type. |
|
499 | - * |
|
500 | - * @throws \UnexpectedValueException If the element is not a character string |
|
501 | - * |
|
502 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
503 | - */ |
|
504 | - public function asCharacterString(): Primitive\CharacterString |
|
505 | - { |
|
506 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
507 | - throw new \UnexpectedValueException( |
|
508 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
509 | - } |
|
510 | - return $this->_element; |
|
511 | - } |
|
512 | - |
|
513 | - /** |
|
514 | - * Get the wrapped element as a BMP string type. |
|
515 | - * |
|
516 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
517 | - * |
|
518 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
519 | - */ |
|
520 | - public function asBMPString(): Primitive\BMPString |
|
521 | - { |
|
522 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
523 | - throw new \UnexpectedValueException( |
|
524 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
525 | - } |
|
526 | - return $this->_element; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Get the wrapped element as a constructed string type. |
|
531 | - * |
|
532 | - * @throws \UnexpectedValueException If the element is not a constructed string |
|
533 | - * |
|
534 | - * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
535 | - */ |
|
536 | - public function asConstructedString(): Constructed\ConstructedString |
|
537 | - { |
|
538 | - if (!$this->_element instanceof Constructed\ConstructedString) { |
|
539 | - throw new \UnexpectedValueException( |
|
540 | - $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
541 | - } |
|
542 | - return $this->_element; |
|
543 | - } |
|
544 | - |
|
545 | - /** |
|
546 | - * Get the wrapped element as any string type. |
|
547 | - * |
|
548 | - * @throws \UnexpectedValueException If the element is not a string type |
|
549 | - * |
|
550 | - * @return StringType |
|
551 | - */ |
|
552 | - public function asString(): StringType |
|
553 | - { |
|
554 | - if (!$this->_element instanceof StringType) { |
|
555 | - throw new \UnexpectedValueException( |
|
556 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
557 | - } |
|
558 | - return $this->_element; |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Get the wrapped element as any time type. |
|
563 | - * |
|
564 | - * @throws \UnexpectedValueException If the element is not a time type |
|
565 | - * |
|
566 | - * @return TimeType |
|
567 | - */ |
|
568 | - public function asTime(): TimeType |
|
569 | - { |
|
570 | - if (!$this->_element instanceof TimeType) { |
|
571 | - throw new \UnexpectedValueException( |
|
572 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
573 | - } |
|
574 | - return $this->_element; |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * {@inheritdoc} |
|
579 | - */ |
|
580 | - public function asElement(): Element |
|
581 | - { |
|
582 | - return $this->_element; |
|
583 | - } |
|
584 | - |
|
585 | - /** |
|
586 | - * {@inheritdoc} |
|
587 | - */ |
|
588 | - public function asUnspecified(): UnspecifiedType |
|
589 | - { |
|
590 | - return $this; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * {@inheritdoc} |
|
595 | - */ |
|
596 | - public function toDER(): string |
|
597 | - { |
|
598 | - return $this->_element->toDER(); |
|
599 | - } |
|
600 | - |
|
601 | - /** |
|
602 | - * {@inheritdoc} |
|
603 | - */ |
|
604 | - public function typeClass(): int |
|
605 | - { |
|
606 | - return $this->_element->typeClass(); |
|
607 | - } |
|
608 | - |
|
609 | - /** |
|
610 | - * {@inheritdoc} |
|
611 | - */ |
|
612 | - public function tag(): int |
|
613 | - { |
|
614 | - return $this->_element->tag(); |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * {@inheritdoc} |
|
619 | - */ |
|
620 | - public function isConstructed(): bool |
|
621 | - { |
|
622 | - return $this->_element->isConstructed(); |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * {@inheritdoc} |
|
627 | - */ |
|
628 | - public function isType(int $tag): bool |
|
629 | - { |
|
630 | - return $this->_element->isType($tag); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * {@inheritdoc} |
|
635 | - */ |
|
636 | - public function isTagged(): bool |
|
637 | - { |
|
638 | - return $this->_element->isTagged(); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * {@inheritdoc} |
|
643 | - * |
|
644 | - * Consider using any of the `as*` accessor methods instead. |
|
645 | - */ |
|
646 | - public function expectType(int $tag): ElementBase |
|
647 | - { |
|
648 | - return $this->_element->expectType($tag); |
|
649 | - } |
|
650 | - |
|
651 | - /** |
|
652 | - * {@inheritdoc} |
|
653 | - * |
|
654 | - * Consider using `asTagged()` method instead and chaining |
|
655 | - * with `TaggedType::asExplicit()` or `TaggedType::asImplicit()`. |
|
656 | - */ |
|
657 | - public function expectTagged(?int $tag = null): TaggedType |
|
658 | - { |
|
659 | - return $this->_element->expectTagged($tag); |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * Generate message for exceptions thrown by `as*` methods. |
|
664 | - * |
|
665 | - * @param int $tag Type tag of the expected element |
|
666 | - * |
|
667 | - * @return string |
|
668 | - */ |
|
669 | - private function _generateExceptionMessage(int $tag): string |
|
670 | - { |
|
671 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
672 | - $this->_typeDescriptorString()); |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Get textual description of the wrapped element for debugging purposes. |
|
677 | - * |
|
678 | - * @return string |
|
679 | - */ |
|
680 | - private function _typeDescriptorString(): string |
|
681 | - { |
|
682 | - $type_cls = $this->_element->typeClass(); |
|
683 | - $tag = $this->_element->tag(); |
|
684 | - $str = $this->_element->isConstructed() ? 'constructed ' : 'primitive '; |
|
685 | - if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
686 | - $str .= Element::tagToName($tag); |
|
687 | - } else { |
|
688 | - $str .= Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
689 | - } |
|
690 | - return $str; |
|
691 | - } |
|
20 | + /** |
|
21 | + * The wrapped element. |
|
22 | + * |
|
23 | + * @var Element |
|
24 | + */ |
|
25 | + private $_element; |
|
26 | + |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * @param Element $el |
|
31 | + */ |
|
32 | + public function __construct(Element $el) |
|
33 | + { |
|
34 | + $this->_element = $el; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Initialize from DER data. |
|
39 | + * |
|
40 | + * @param string $data DER encoded data |
|
41 | + * |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + public static function fromDER(string $data): self |
|
45 | + { |
|
46 | + return Element::fromDER($data)->asUnspecified(); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Initialize from `ElementBase` interface. |
|
51 | + * |
|
52 | + * @param ElementBase $el |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public static function fromElementBase(ElementBase $el): self |
|
57 | + { |
|
58 | + // if element is already wrapped |
|
59 | + if ($el instanceof self) { |
|
60 | + return $el; |
|
61 | + } |
|
62 | + return new self($el->asElement()); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the wrapped element as a context specific tagged type. |
|
67 | + * |
|
68 | + * @throws \UnexpectedValueException If the element is not tagged |
|
69 | + * |
|
70 | + * @return TaggedType |
|
71 | + */ |
|
72 | + public function asTagged(): TaggedType |
|
73 | + { |
|
74 | + if (!$this->_element instanceof TaggedType) { |
|
75 | + throw new \UnexpectedValueException( |
|
76 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
77 | + } |
|
78 | + return $this->_element; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get the wrapped element as an application specific type. |
|
83 | + * |
|
84 | + * @throws \UnexpectedValueException If element is not application specific |
|
85 | + * |
|
86 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
87 | + */ |
|
88 | + public function asApplication(): Tagged\ApplicationType |
|
89 | + { |
|
90 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
91 | + throw new \UnexpectedValueException( |
|
92 | + 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
93 | + } |
|
94 | + return $this->_element; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get the wrapped element as a private tagged type. |
|
99 | + * |
|
100 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
101 | + * |
|
102 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
103 | + */ |
|
104 | + public function asPrivate(): Tagged\PrivateType |
|
105 | + { |
|
106 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
107 | + throw new \UnexpectedValueException( |
|
108 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
109 | + } |
|
110 | + return $this->_element; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the wrapped element as a boolean type. |
|
115 | + * |
|
116 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
117 | + * |
|
118 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
119 | + */ |
|
120 | + public function asBoolean(): Primitive\Boolean |
|
121 | + { |
|
122 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
123 | + throw new \UnexpectedValueException( |
|
124 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
125 | + } |
|
126 | + return $this->_element; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get the wrapped element as an integer type. |
|
131 | + * |
|
132 | + * @throws \UnexpectedValueException If the element is not an integer |
|
133 | + * |
|
134 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
135 | + */ |
|
136 | + public function asInteger(): Primitive\Integer |
|
137 | + { |
|
138 | + if (!$this->_element instanceof Primitive\Integer) { |
|
139 | + throw new \UnexpectedValueException( |
|
140 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
141 | + } |
|
142 | + return $this->_element; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get the wrapped element as a bit string type. |
|
147 | + * |
|
148 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
149 | + * |
|
150 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
151 | + */ |
|
152 | + public function asBitString(): Primitive\BitString |
|
153 | + { |
|
154 | + if (!$this->_element instanceof Primitive\BitString) { |
|
155 | + throw new \UnexpectedValueException( |
|
156 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
157 | + } |
|
158 | + return $this->_element; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the wrapped element as an octet string type. |
|
163 | + * |
|
164 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
165 | + * |
|
166 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
167 | + */ |
|
168 | + public function asOctetString(): Primitive\OctetString |
|
169 | + { |
|
170 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
171 | + throw new \UnexpectedValueException( |
|
172 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
173 | + } |
|
174 | + return $this->_element; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get the wrapped element as a null type. |
|
179 | + * |
|
180 | + * @throws \UnexpectedValueException If the element is not a null |
|
181 | + * |
|
182 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
183 | + */ |
|
184 | + public function asNull(): Primitive\NullType |
|
185 | + { |
|
186 | + if (!$this->_element instanceof Primitive\NullType) { |
|
187 | + throw new \UnexpectedValueException( |
|
188 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
189 | + } |
|
190 | + return $this->_element; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Get the wrapped element as an object identifier type. |
|
195 | + * |
|
196 | + * @throws \UnexpectedValueException If the element is not an object identifier |
|
197 | + * |
|
198 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
199 | + */ |
|
200 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
201 | + { |
|
202 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
203 | + throw new \UnexpectedValueException( |
|
204 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
205 | + } |
|
206 | + return $this->_element; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get the wrapped element as an object descriptor type. |
|
211 | + * |
|
212 | + * @throws \UnexpectedValueException If the element is not an object descriptor |
|
213 | + * |
|
214 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
215 | + */ |
|
216 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
217 | + { |
|
218 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
219 | + throw new \UnexpectedValueException( |
|
220 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
221 | + } |
|
222 | + return $this->_element; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Get the wrapped element as a real type. |
|
227 | + * |
|
228 | + * @throws \UnexpectedValueException If the element is not a real |
|
229 | + * |
|
230 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
231 | + */ |
|
232 | + public function asReal(): Primitive\Real |
|
233 | + { |
|
234 | + if (!$this->_element instanceof Primitive\Real) { |
|
235 | + throw new \UnexpectedValueException( |
|
236 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
237 | + } |
|
238 | + return $this->_element; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Get the wrapped element as an enumerated type. |
|
243 | + * |
|
244 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
245 | + * |
|
246 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
247 | + */ |
|
248 | + public function asEnumerated(): Primitive\Enumerated |
|
249 | + { |
|
250 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
251 | + throw new \UnexpectedValueException( |
|
252 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
253 | + } |
|
254 | + return $this->_element; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Get the wrapped element as a UTF8 string type. |
|
259 | + * |
|
260 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
261 | + * |
|
262 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
263 | + */ |
|
264 | + public function asUTF8String(): Primitive\UTF8String |
|
265 | + { |
|
266 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
267 | + throw new \UnexpectedValueException( |
|
268 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
269 | + } |
|
270 | + return $this->_element; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get the wrapped element as a relative OID type. |
|
275 | + * |
|
276 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
277 | + * |
|
278 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
279 | + */ |
|
280 | + public function asRelativeOID(): Primitive\RelativeOID |
|
281 | + { |
|
282 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
283 | + throw new \UnexpectedValueException( |
|
284 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
285 | + } |
|
286 | + return $this->_element; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Get the wrapped element as a sequence type. |
|
291 | + * |
|
292 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
293 | + * |
|
294 | + * @return \Sop\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 | + * |
|
310 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
311 | + */ |
|
312 | + public function asSet(): Constructed\Set |
|
313 | + { |
|
314 | + if (!$this->_element instanceof Constructed\Set) { |
|
315 | + throw new \UnexpectedValueException( |
|
316 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
317 | + } |
|
318 | + return $this->_element; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Get the wrapped element as a numeric string type. |
|
323 | + * |
|
324 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
325 | + * |
|
326 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
327 | + */ |
|
328 | + public function asNumericString(): Primitive\NumericString |
|
329 | + { |
|
330 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
331 | + throw new \UnexpectedValueException( |
|
332 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
333 | + } |
|
334 | + return $this->_element; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Get the wrapped element as a printable string type. |
|
339 | + * |
|
340 | + * @throws \UnexpectedValueException If the element is not a printable string |
|
341 | + * |
|
342 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
343 | + */ |
|
344 | + public function asPrintableString(): Primitive\PrintableString |
|
345 | + { |
|
346 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
347 | + throw new \UnexpectedValueException( |
|
348 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
349 | + } |
|
350 | + return $this->_element; |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Get the wrapped element as a T61 string type. |
|
355 | + * |
|
356 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
357 | + * |
|
358 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
359 | + */ |
|
360 | + public function asT61String(): Primitive\T61String |
|
361 | + { |
|
362 | + if (!$this->_element instanceof Primitive\T61String) { |
|
363 | + throw new \UnexpectedValueException( |
|
364 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
365 | + } |
|
366 | + return $this->_element; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Get the wrapped element as a videotex string type. |
|
371 | + * |
|
372 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
373 | + * |
|
374 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
375 | + */ |
|
376 | + public function asVideotexString(): Primitive\VideotexString |
|
377 | + { |
|
378 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
379 | + throw new \UnexpectedValueException( |
|
380 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
381 | + } |
|
382 | + return $this->_element; |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Get the wrapped element as a IA5 string type. |
|
387 | + * |
|
388 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
389 | + * |
|
390 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
391 | + */ |
|
392 | + public function asIA5String(): Primitive\IA5String |
|
393 | + { |
|
394 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
395 | + throw new \UnexpectedValueException( |
|
396 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
397 | + } |
|
398 | + return $this->_element; |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Get the wrapped element as an UTC time type. |
|
403 | + * |
|
404 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
405 | + * |
|
406 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
407 | + */ |
|
408 | + public function asUTCTime(): Primitive\UTCTime |
|
409 | + { |
|
410 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
411 | + throw new \UnexpectedValueException( |
|
412 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
413 | + } |
|
414 | + return $this->_element; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Get the wrapped element as a generalized time type. |
|
419 | + * |
|
420 | + * @throws \UnexpectedValueException If the element is not a generalized time |
|
421 | + * |
|
422 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
423 | + */ |
|
424 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
425 | + { |
|
426 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
427 | + throw new \UnexpectedValueException( |
|
428 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
429 | + } |
|
430 | + return $this->_element; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Get the wrapped element as a graphic string type. |
|
435 | + * |
|
436 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
437 | + * |
|
438 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
439 | + */ |
|
440 | + public function asGraphicString(): Primitive\GraphicString |
|
441 | + { |
|
442 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
443 | + throw new \UnexpectedValueException( |
|
444 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
445 | + } |
|
446 | + return $this->_element; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Get the wrapped element as a visible string type. |
|
451 | + * |
|
452 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
453 | + * |
|
454 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
455 | + */ |
|
456 | + public function asVisibleString(): Primitive\VisibleString |
|
457 | + { |
|
458 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
459 | + throw new \UnexpectedValueException( |
|
460 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
461 | + } |
|
462 | + return $this->_element; |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Get the wrapped element as a general string type. |
|
467 | + * |
|
468 | + * @throws \UnexpectedValueException If the element is not general string |
|
469 | + * |
|
470 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
471 | + */ |
|
472 | + public function asGeneralString(): Primitive\GeneralString |
|
473 | + { |
|
474 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
475 | + throw new \UnexpectedValueException( |
|
476 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
477 | + } |
|
478 | + return $this->_element; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Get the wrapped element as a universal string type. |
|
483 | + * |
|
484 | + * @throws \UnexpectedValueException If the element is not a universal string |
|
485 | + * |
|
486 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
487 | + */ |
|
488 | + public function asUniversalString(): Primitive\UniversalString |
|
489 | + { |
|
490 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
491 | + throw new \UnexpectedValueException( |
|
492 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
493 | + } |
|
494 | + return $this->_element; |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Get the wrapped element as a character string type. |
|
499 | + * |
|
500 | + * @throws \UnexpectedValueException If the element is not a character string |
|
501 | + * |
|
502 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
503 | + */ |
|
504 | + public function asCharacterString(): Primitive\CharacterString |
|
505 | + { |
|
506 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
507 | + throw new \UnexpectedValueException( |
|
508 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
509 | + } |
|
510 | + return $this->_element; |
|
511 | + } |
|
512 | + |
|
513 | + /** |
|
514 | + * Get the wrapped element as a BMP string type. |
|
515 | + * |
|
516 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
517 | + * |
|
518 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
519 | + */ |
|
520 | + public function asBMPString(): Primitive\BMPString |
|
521 | + { |
|
522 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
523 | + throw new \UnexpectedValueException( |
|
524 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
525 | + } |
|
526 | + return $this->_element; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Get the wrapped element as a constructed string type. |
|
531 | + * |
|
532 | + * @throws \UnexpectedValueException If the element is not a constructed string |
|
533 | + * |
|
534 | + * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
535 | + */ |
|
536 | + public function asConstructedString(): Constructed\ConstructedString |
|
537 | + { |
|
538 | + if (!$this->_element instanceof Constructed\ConstructedString) { |
|
539 | + throw new \UnexpectedValueException( |
|
540 | + $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
541 | + } |
|
542 | + return $this->_element; |
|
543 | + } |
|
544 | + |
|
545 | + /** |
|
546 | + * Get the wrapped element as any string type. |
|
547 | + * |
|
548 | + * @throws \UnexpectedValueException If the element is not a string type |
|
549 | + * |
|
550 | + * @return StringType |
|
551 | + */ |
|
552 | + public function asString(): StringType |
|
553 | + { |
|
554 | + if (!$this->_element instanceof StringType) { |
|
555 | + throw new \UnexpectedValueException( |
|
556 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
557 | + } |
|
558 | + return $this->_element; |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Get the wrapped element as any time type. |
|
563 | + * |
|
564 | + * @throws \UnexpectedValueException If the element is not a time type |
|
565 | + * |
|
566 | + * @return TimeType |
|
567 | + */ |
|
568 | + public function asTime(): TimeType |
|
569 | + { |
|
570 | + if (!$this->_element instanceof TimeType) { |
|
571 | + throw new \UnexpectedValueException( |
|
572 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
573 | + } |
|
574 | + return $this->_element; |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * {@inheritdoc} |
|
579 | + */ |
|
580 | + public function asElement(): Element |
|
581 | + { |
|
582 | + return $this->_element; |
|
583 | + } |
|
584 | + |
|
585 | + /** |
|
586 | + * {@inheritdoc} |
|
587 | + */ |
|
588 | + public function asUnspecified(): UnspecifiedType |
|
589 | + { |
|
590 | + return $this; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * {@inheritdoc} |
|
595 | + */ |
|
596 | + public function toDER(): string |
|
597 | + { |
|
598 | + return $this->_element->toDER(); |
|
599 | + } |
|
600 | + |
|
601 | + /** |
|
602 | + * {@inheritdoc} |
|
603 | + */ |
|
604 | + public function typeClass(): int |
|
605 | + { |
|
606 | + return $this->_element->typeClass(); |
|
607 | + } |
|
608 | + |
|
609 | + /** |
|
610 | + * {@inheritdoc} |
|
611 | + */ |
|
612 | + public function tag(): int |
|
613 | + { |
|
614 | + return $this->_element->tag(); |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * {@inheritdoc} |
|
619 | + */ |
|
620 | + public function isConstructed(): bool |
|
621 | + { |
|
622 | + return $this->_element->isConstructed(); |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * {@inheritdoc} |
|
627 | + */ |
|
628 | + public function isType(int $tag): bool |
|
629 | + { |
|
630 | + return $this->_element->isType($tag); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * {@inheritdoc} |
|
635 | + */ |
|
636 | + public function isTagged(): bool |
|
637 | + { |
|
638 | + return $this->_element->isTagged(); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * {@inheritdoc} |
|
643 | + * |
|
644 | + * Consider using any of the `as*` accessor methods instead. |
|
645 | + */ |
|
646 | + public function expectType(int $tag): ElementBase |
|
647 | + { |
|
648 | + return $this->_element->expectType($tag); |
|
649 | + } |
|
650 | + |
|
651 | + /** |
|
652 | + * {@inheritdoc} |
|
653 | + * |
|
654 | + * Consider using `asTagged()` method instead and chaining |
|
655 | + * with `TaggedType::asExplicit()` or `TaggedType::asImplicit()`. |
|
656 | + */ |
|
657 | + public function expectTagged(?int $tag = null): TaggedType |
|
658 | + { |
|
659 | + return $this->_element->expectTagged($tag); |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * Generate message for exceptions thrown by `as*` methods. |
|
664 | + * |
|
665 | + * @param int $tag Type tag of the expected element |
|
666 | + * |
|
667 | + * @return string |
|
668 | + */ |
|
669 | + private function _generateExceptionMessage(int $tag): string |
|
670 | + { |
|
671 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
672 | + $this->_typeDescriptorString()); |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Get textual description of the wrapped element for debugging purposes. |
|
677 | + * |
|
678 | + * @return string |
|
679 | + */ |
|
680 | + private function _typeDescriptorString(): string |
|
681 | + { |
|
682 | + $type_cls = $this->_element->typeClass(); |
|
683 | + $tag = $this->_element->tag(); |
|
684 | + $str = $this->_element->isConstructed() ? 'constructed ' : 'primitive '; |
|
685 | + if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
686 | + $str .= Element::tagToName($tag); |
|
687 | + } else { |
|
688 | + $str .= Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
689 | + } |
|
690 | + return $str; |
|
691 | + } |
|
692 | 692 | } |
@@ -11,111 +11,111 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class BaseTime extends Element implements TimeType |
13 | 13 | { |
14 | - /** |
|
15 | - * UTC timezone. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - const TZ_UTC = 'UTC'; |
|
14 | + /** |
|
15 | + * UTC timezone. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + const TZ_UTC = 'UTC'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Date and time. |
|
23 | - * |
|
24 | - * @var \DateTimeImmutable |
|
25 | - */ |
|
26 | - protected $_dateTime; |
|
21 | + /** |
|
22 | + * Date and time. |
|
23 | + * |
|
24 | + * @var \DateTimeImmutable |
|
25 | + */ |
|
26 | + protected $_dateTime; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param \DateTimeImmutable $dt |
|
32 | - */ |
|
33 | - public function __construct(\DateTimeImmutable $dt) |
|
34 | - { |
|
35 | - $this->_dateTime = $dt; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param \DateTimeImmutable $dt |
|
32 | + */ |
|
33 | + public function __construct(\DateTimeImmutable $dt) |
|
34 | + { |
|
35 | + $this->_dateTime = $dt; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * {@inheritdoc} |
|
40 | - */ |
|
41 | - public function __toString(): string |
|
42 | - { |
|
43 | - return $this->string(); |
|
44 | - } |
|
38 | + /** |
|
39 | + * {@inheritdoc} |
|
40 | + */ |
|
41 | + public function __toString(): string |
|
42 | + { |
|
43 | + return $this->string(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Initialize from datetime string. |
|
48 | - * |
|
49 | - * @see http://php.net/manual/en/datetime.formats.php |
|
50 | - * |
|
51 | - * @param string $time Time string |
|
52 | - * @param null|string $tz timezone, if null use default |
|
53 | - * |
|
54 | - * @throws \RuntimeException |
|
55 | - * |
|
56 | - * @return self |
|
57 | - */ |
|
58 | - public static function fromString(string $time, ?string $tz = null): self |
|
59 | - { |
|
60 | - try { |
|
61 | - if (!isset($tz)) { |
|
62 | - $tz = date_default_timezone_get(); |
|
63 | - } |
|
64 | - return new static( |
|
65 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
66 | - } catch (\Exception $e) { |
|
67 | - throw new \RuntimeException( |
|
68 | - 'Failed to create DateTime: ' . |
|
69 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
70 | - } |
|
71 | - } |
|
46 | + /** |
|
47 | + * Initialize from datetime string. |
|
48 | + * |
|
49 | + * @see http://php.net/manual/en/datetime.formats.php |
|
50 | + * |
|
51 | + * @param string $time Time string |
|
52 | + * @param null|string $tz timezone, if null use default |
|
53 | + * |
|
54 | + * @throws \RuntimeException |
|
55 | + * |
|
56 | + * @return self |
|
57 | + */ |
|
58 | + public static function fromString(string $time, ?string $tz = null): self |
|
59 | + { |
|
60 | + try { |
|
61 | + if (!isset($tz)) { |
|
62 | + $tz = date_default_timezone_get(); |
|
63 | + } |
|
64 | + return new static( |
|
65 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
66 | + } catch (\Exception $e) { |
|
67 | + throw new \RuntimeException( |
|
68 | + 'Failed to create DateTime: ' . |
|
69 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get the date and time. |
|
75 | - * |
|
76 | - * @return \DateTimeImmutable |
|
77 | - */ |
|
78 | - public function dateTime(): \DateTimeImmutable |
|
79 | - { |
|
80 | - return $this->_dateTime; |
|
81 | - } |
|
73 | + /** |
|
74 | + * Get the date and time. |
|
75 | + * |
|
76 | + * @return \DateTimeImmutable |
|
77 | + */ |
|
78 | + public function dateTime(): \DateTimeImmutable |
|
79 | + { |
|
80 | + return $this->_dateTime; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Get the date and time as a type specific string. |
|
85 | - * |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function string(): string |
|
89 | - { |
|
90 | - return $this->_encodedContentDER(); |
|
91 | - } |
|
83 | + /** |
|
84 | + * Get the date and time as a type specific string. |
|
85 | + * |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function string(): string |
|
89 | + { |
|
90 | + return $this->_encodedContentDER(); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Create `DateTimeZone` object from string. |
|
95 | - * |
|
96 | - * @param string $tz |
|
97 | - * |
|
98 | - * @throws \UnexpectedValueException If timezone is invalid |
|
99 | - * |
|
100 | - * @return \DateTimeZone |
|
101 | - */ |
|
102 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
103 | - { |
|
104 | - try { |
|
105 | - return new \DateTimeZone($tz); |
|
106 | - } catch (\Exception $e) { |
|
107 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
108 | - } |
|
109 | - } |
|
93 | + /** |
|
94 | + * Create `DateTimeZone` object from string. |
|
95 | + * |
|
96 | + * @param string $tz |
|
97 | + * |
|
98 | + * @throws \UnexpectedValueException If timezone is invalid |
|
99 | + * |
|
100 | + * @return \DateTimeZone |
|
101 | + */ |
|
102 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
103 | + { |
|
104 | + try { |
|
105 | + return new \DateTimeZone($tz); |
|
106 | + } catch (\Exception $e) { |
|
107 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Get last error caused by `DateTimeImmutable`. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
117 | - { |
|
118 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
119 | - return implode(', ', $errors); |
|
120 | - } |
|
111 | + /** |
|
112 | + * Get last error caused by `DateTimeImmutable`. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
117 | + { |
|
118 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
119 | + return implode(', ', $errors); |
|
120 | + } |
|
121 | 121 | } |
@@ -18,36 +18,36 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class PrimitiveString extends BaseString |
20 | 20 | { |
21 | - use PrimitiveType; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * {@inheritdoc} |
|
25 | - */ |
|
26 | - protected function _encodedContentDER(): string |
|
27 | - { |
|
28 | - return $this->_string; |
|
29 | - } |
|
23 | + /** |
|
24 | + * {@inheritdoc} |
|
25 | + */ |
|
26 | + protected function _encodedContentDER(): string |
|
27 | + { |
|
28 | + return $this->_string; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * {@inheritdoc} |
|
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 | - } |
|
31 | + /** |
|
32 | + * {@inheritdoc} |
|
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 | } |
@@ -17,70 +17,70 @@ |
||
17 | 17 | */ |
18 | 18 | class UTCTime extends BaseTime |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = '#^' . |
|
31 | - '(\d\d)' . // YY |
|
32 | - '(\d\d)' . // MM |
|
33 | - '(\d\d)' . // DD |
|
34 | - '(\d\d)' . // hh |
|
35 | - '(\d\d)' . // mm |
|
36 | - '(\d\d)' . // ss |
|
37 | - 'Z' . // TZ |
|
38 | - '$#'; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = '#^' . |
|
31 | + '(\d\d)' . // YY |
|
32 | + '(\d\d)' . // MM |
|
33 | + '(\d\d)' . // DD |
|
34 | + '(\d\d)' . // hh |
|
35 | + '(\d\d)' . // mm |
|
36 | + '(\d\d)' . // ss |
|
37 | + 'Z' . // TZ |
|
38 | + '$#'; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param \DateTimeImmutable $dt |
|
44 | - */ |
|
45 | - public function __construct(\DateTimeImmutable $dt) |
|
46 | - { |
|
47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | - parent::__construct($dt); |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param \DateTimeImmutable $dt |
|
44 | + */ |
|
45 | + public function __construct(\DateTimeImmutable $dt) |
|
46 | + { |
|
47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | + parent::__construct($dt); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | - return $dt->format('ymdHis\\Z'); |
|
58 | - } |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | + return $dt->format('ymdHis\\Z'); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected static function _decodeFromDER(Identifier $identifier, |
|
64 | - string $data, int &$offset): ElementBase |
|
65 | - { |
|
66 | - $idx = $offset; |
|
67 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
68 | - $str = substr($data, $idx, $length); |
|
69 | - $idx += $length; |
|
70 | - /** @var string[] $match */ |
|
71 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
72 | - throw new DecodeException('Invalid UTCTime format.'); |
|
73 | - } |
|
74 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
75 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
76 | - $dt = \DateTimeImmutable::createFromFormat('!ymdHisT', $time, |
|
77 | - self::_createTimeZone(self::TZ_UTC)); |
|
78 | - if (!$dt) { |
|
79 | - throw new DecodeException( |
|
80 | - 'Failed to decode UTCTime: ' . |
|
81 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
82 | - } |
|
83 | - $offset = $idx; |
|
84 | - return new self($dt); |
|
85 | - } |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected static function _decodeFromDER(Identifier $identifier, |
|
64 | + string $data, int &$offset): ElementBase |
|
65 | + { |
|
66 | + $idx = $offset; |
|
67 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
68 | + $str = substr($data, $idx, $length); |
|
69 | + $idx += $length; |
|
70 | + /** @var string[] $match */ |
|
71 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
72 | + throw new DecodeException('Invalid UTCTime format.'); |
|
73 | + } |
|
74 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
75 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
76 | + $dt = \DateTimeImmutable::createFromFormat('!ymdHisT', $time, |
|
77 | + self::_createTimeZone(self::TZ_UTC)); |
|
78 | + if (!$dt) { |
|
79 | + throw new DecodeException( |
|
80 | + 'Failed to decode UTCTime: ' . |
|
81 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
82 | + } |
|
83 | + $offset = $idx; |
|
84 | + return new self($dt); |
|
85 | + } |
|
86 | 86 | } |