GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 584f81...7e40d7 )
by Joni
02:01
created
lib/ASN1/Type/Tagged/DERTaggedType.php 1 patch
Indentation   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -20,105 +20,105 @@
 block discarded – undo
20 20
  * May be encoded back to complete DER encoding.
21 21
  */
22 22
 class DERTaggedType extends TaggedType implements 
23
-    ExplicitTagging,
24
-    ImplicitTagging
23
+	ExplicitTagging,
24
+	ImplicitTagging
25 25
 {
26
-    /**
27
-     * Identifier.
28
-     *
29
-     * @var Identifier
30
-     */
31
-    private $_identifier;
26
+	/**
27
+	 * Identifier.
28
+	 *
29
+	 * @var Identifier
30
+	 */
31
+	private $_identifier;
32 32
     
33
-    /**
34
-     * DER data.
35
-     *
36
-     * @var string
37
-     */
38
-    private $_data;
33
+	/**
34
+	 * DER data.
35
+	 *
36
+	 * @var string
37
+	 */
38
+	private $_data;
39 39
     
40
-    /**
41
-     * Offset to data.
42
-     *
43
-     * @var int
44
-     */
45
-    private $_offset;
40
+	/**
41
+	 * Offset to data.
42
+	 *
43
+	 * @var int
44
+	 */
45
+	private $_offset;
46 46
     
47
-    /**
48
-     * Constructor.
49
-     *
50
-     * @param Identifier $identifier
51
-     * @param string $data
52
-     * @param int $offset Offset to next byte after identifier
53
-     */
54
-    public function __construct(Identifier $identifier, string $data, int $offset)
55
-    {
56
-        $this->_identifier = $identifier;
57
-        $this->_data = $data;
58
-        $this->_offset = $offset;
59
-        $this->_typeTag = $identifier->intTag();
60
-    }
47
+	/**
48
+	 * Constructor.
49
+	 *
50
+	 * @param Identifier $identifier
51
+	 * @param string $data
52
+	 * @param int $offset Offset to next byte after identifier
53
+	 */
54
+	public function __construct(Identifier $identifier, string $data, 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
-        $element = $cls::_decodeFromDER($identifier, $this->_data, $idx);
106
-        return new UnspecifiedType($element);
107
-    }
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
+		$element = $cls::_decodeFromDER($identifier, $this->_data, $idx);
106
+		return new UnspecifiedType($element);
107
+	}
108 108
     
109
-    /**
110
-     *
111
-     * @see \ASN1\Type\Tagged\ExplicitTagging::explicit()
112
-     * @return UnspecifiedType
113
-     */
114
-    public function explicit($expectedTag = null): UnspecifiedType
115
-    {
116
-        $idx = $this->_offset;
117
-        Length::expectFromDER($this->_data, $idx);
118
-        $element = Element::fromDER($this->_data, $idx);
119
-        if (isset($expectedTag)) {
120
-            $element->expectType($expectedTag);
121
-        }
122
-        return new UnspecifiedType($element);
123
-    }
109
+	/**
110
+	 *
111
+	 * @see \ASN1\Type\Tagged\ExplicitTagging::explicit()
112
+	 * @return UnspecifiedType
113
+	 */
114
+	public function explicit($expectedTag = null): UnspecifiedType
115
+	{
116
+		$idx = $this->_offset;
117
+		Length::expectFromDER($this->_data, $idx);
118
+		$element = Element::fromDER($this->_data, $idx);
119
+		if (isset($expectedTag)) {
120
+			$element->expectType($expectedTag);
121
+		}
122
+		return new UnspecifiedType($element);
123
+	}
124 124
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Tagged/ImplicitlyTaggedType.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -16,56 +16,56 @@
 block discarded – undo
16 16
  * decoding the data.
17 17
  */
18 18
 class ImplicitlyTaggedType extends ContextSpecificTaggedType implements 
19
-    ImplicitTagging
19
+	ImplicitTagging
20 20
 {
21
-    /**
22
-     * Constructor.
23
-     *
24
-     * @param int $tag
25
-     * @param Element $element
26
-     */
27
-    public function __construct(int $tag, Element $element)
28
-    {
29
-        $this->_typeTag = $tag;
30
-        $this->_element = $element;
31
-    }
21
+	/**
22
+	 * Constructor.
23
+	 *
24
+	 * @param int $tag
25
+	 * @param Element $element
26
+	 */
27
+	public function __construct(int $tag, Element $element)
28
+	{
29
+		$this->_typeTag = $tag;
30
+		$this->_element = $element;
31
+	}
32 32
     
33
-    /**
34
-     *
35
-     * @see \ASN1\Element::isConstructed()
36
-     * @return bool
37
-     */
38
-    public function isConstructed(): bool
39
-    {
40
-        // depends on the underlying type
41
-        return $this->_element->isConstructed();
42
-    }
33
+	/**
34
+	 *
35
+	 * @see \ASN1\Element::isConstructed()
36
+	 * @return bool
37
+	 */
38
+	public function isConstructed(): bool
39
+	{
40
+		// depends on the underlying type
41
+		return $this->_element->isConstructed();
42
+	}
43 43
     
44
-    /**
45
-     *
46
-     * @see \ASN1\Element::_encodedContentDER()
47
-     * @return string
48
-     */
49
-    protected function _encodedContentDER(): string
50
-    {
51
-        return $this->_element->_encodedContentDER();
52
-    }
44
+	/**
45
+	 *
46
+	 * @see \ASN1\Element::_encodedContentDER()
47
+	 * @return string
48
+	 */
49
+	protected function _encodedContentDER(): string
50
+	{
51
+		return $this->_element->_encodedContentDER();
52
+	}
53 53
     
54
-    /**
55
-     *
56
-     * {@inheritdoc}
57
-     * @see \ASN1\Type\Tagged\ImplicitTagging::implicit()
58
-     * @return UnspecifiedType
59
-     */
60
-    public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType
61
-    {
62
-        $this->_element->expectType($tag);
63
-        if ($this->_element->typeClass() != $class) {
64
-            throw new \UnexpectedValueException(
65
-                sprintf("Type class %s expected, got %s.",
66
-                    Identifier::classToName($class),
67
-                    Identifier::classToName($this->_element->typeClass())));
68
-        }
69
-        return new UnspecifiedType($this->_element);
70
-    }
54
+	/**
55
+	 *
56
+	 * {@inheritdoc}
57
+	 * @see \ASN1\Type\Tagged\ImplicitTagging::implicit()
58
+	 * @return UnspecifiedType
59
+	 */
60
+	public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType
61
+	{
62
+		$this->_element->expectType($tag);
63
+		if ($this->_element->typeClass() != $class) {
64
+			throw new \UnexpectedValueException(
65
+				sprintf("Type class %s expected, got %s.",
66
+					Identifier::classToName($class),
67
+					Identifier::classToName($this->_element->typeClass())));
68
+		}
69
+		return new UnspecifiedType($this->_element);
70
+	}
71 71
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Tagged/ExplicitlyTaggedType.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -14,51 +14,51 @@
 block discarded – undo
14 14
  * is not changed.
15 15
  */
16 16
 class ExplicitlyTaggedType extends ContextSpecificTaggedType implements 
17
-    ExplicitTagging
17
+	ExplicitTagging
18 18
 {
19
-    /**
20
-     * Constructor.
21
-     *
22
-     * @param int $tag Tag number
23
-     * @param Element $element Wrapped element
24
-     */
25
-    public function __construct(int $tag, Element $element)
26
-    {
27
-        $this->_typeTag = $tag;
28
-        $this->_element = $element;
29
-    }
19
+	/**
20
+	 * Constructor.
21
+	 *
22
+	 * @param int $tag Tag number
23
+	 * @param Element $element Wrapped element
24
+	 */
25
+	public function __construct(int $tag, Element $element)
26
+	{
27
+		$this->_typeTag = $tag;
28
+		$this->_element = $element;
29
+	}
30 30
     
31
-    /**
32
-     *
33
-     * @see \ASN1\Element::isConstructed()
34
-     * @return bool
35
-     */
36
-    public function isConstructed(): bool
37
-    {
38
-        return true;
39
-    }
31
+	/**
32
+	 *
33
+	 * @see \ASN1\Element::isConstructed()
34
+	 * @return bool
35
+	 */
36
+	public function isConstructed(): bool
37
+	{
38
+		return true;
39
+	}
40 40
     
41
-    /**
42
-     *
43
-     * @see \ASN1\Element::_encodedContentDER()
44
-     * @return string
45
-     */
46
-    protected function _encodedContentDER(): string
47
-    {
48
-        return $this->_element->toDER();
49
-    }
41
+	/**
42
+	 *
43
+	 * @see \ASN1\Element::_encodedContentDER()
44
+	 * @return string
45
+	 */
46
+	protected function _encodedContentDER(): string
47
+	{
48
+		return $this->_element->toDER();
49
+	}
50 50
     
51
-    /**
52
-     *
53
-     * {@inheritdoc}
54
-     * @see \ASN1\Type\Tagged\ExplicitTagging::explicit()
55
-     * @return UnspecifiedType
56
-     */
57
-    public function explicit($expectedTag = null): UnspecifiedType
58
-    {
59
-        if (isset($expectedTag)) {
60
-            $this->_element->expectType($expectedTag);
61
-        }
62
-        return new UnspecifiedType($this->_element);
63
-    }
51
+	/**
52
+	 *
53
+	 * {@inheritdoc}
54
+	 * @see \ASN1\Type\Tagged\ExplicitTagging::explicit()
55
+	 * @return UnspecifiedType
56
+	 */
57
+	public function explicit($expectedTag = null): UnspecifiedType
58
+	{
59
+		if (isset($expectedTag)) {
60
+			$this->_element->expectType($expectedTag);
61
+		}
62
+		return new UnspecifiedType($this->_element);
63
+	}
64 64
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Tagged/ImplicitTagging.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -13,13 +13,13 @@
 block discarded – undo
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
-     * @throws \UnexpectedValueException If expectation fails
22
-     * @return \ASN1\Type\UnspecifiedType
23
-     */
24
-    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
+	 * @throws \UnexpectedValueException If expectation fails
22
+	 * @return \ASN1\Type\UnspecifiedType
23
+	 */
24
+	public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType;
25 25
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Structure.php 1 patch
Indentation   +310 added lines, -310 removed lines patch added patch discarded remove patch
@@ -14,336 +14,336 @@
 block discarded – undo
14 14
  * Base class for the constructed types.
15 15
  */
16 16
 abstract class Structure extends Element implements 
17
-    \Countable,
18
-    \IteratorAggregate
17
+	\Countable,
18
+	\IteratorAggregate
19 19
 {
20
-    use UniversalClass;
20
+	use UniversalClass;
21 21
     
22
-    /**
23
-     * Array of elements in the structure.
24
-     *
25
-     * @var Element[] $_elements
26
-     */
27
-    protected $_elements;
22
+	/**
23
+	 * Array of elements in the structure.
24
+	 *
25
+	 * @var Element[] $_elements
26
+	 */
27
+	protected $_elements;
28 28
     
29
-    /**
30
-     * Lookup table for the tagged elements.
31
-     *
32
-     * @var TaggedType[]|null $_taggedMap
33
-     */
34
-    private $_taggedMap;
29
+	/**
30
+	 * Lookup table for the tagged elements.
31
+	 *
32
+	 * @var TaggedType[]|null $_taggedMap
33
+	 */
34
+	private $_taggedMap;
35 35
     
36
-    /**
37
-     * Cache variable of elements wrapped into UnspecifiedType objects.
38
-     *
39
-     * @var UnspecifiedType[]|null $_unspecifiedTypes
40
-     */
41
-    private $_unspecifiedTypes;
36
+	/**
37
+	 * Cache variable of elements wrapped into UnspecifiedType objects.
38
+	 *
39
+	 * @var UnspecifiedType[]|null $_unspecifiedTypes
40
+	 */
41
+	private $_unspecifiedTypes;
42 42
     
43
-    /**
44
-     * Constructor.
45
-     *
46
-     * @param Element ...$elements Any number of elements
47
-     */
48
-    public function __construct(Element ...$elements)
49
-    {
50
-        $this->_elements = $elements;
51
-    }
43
+	/**
44
+	 * Constructor.
45
+	 *
46
+	 * @param Element ...$elements Any number of elements
47
+	 */
48
+	public function __construct(Element ...$elements)
49
+	{
50
+		$this->_elements = $elements;
51
+	}
52 52
     
53
-    /**
54
-     * Clone magic method.
55
-     */
56
-    public function __clone()
57
-    {
58
-        // clear cache-variables
59
-        $this->_taggedMap = null;
60
-        $this->_unspecifiedTypes = null;
61
-    }
53
+	/**
54
+	 * Clone magic method.
55
+	 */
56
+	public function __clone()
57
+	{
58
+		// clear cache-variables
59
+		$this->_taggedMap = null;
60
+		$this->_unspecifiedTypes = null;
61
+	}
62 62
     
63
-    /**
64
-     *
65
-     * @see \ASN1\Element::isConstructed()
66
-     * @return bool
67
-     */
68
-    public function isConstructed(): bool
69
-    {
70
-        return true;
71
-    }
63
+	/**
64
+	 *
65
+	 * @see \ASN1\Element::isConstructed()
66
+	 * @return bool
67
+	 */
68
+	public function isConstructed(): bool
69
+	{
70
+		return true;
71
+	}
72 72
     
73
-    /**
74
-     *
75
-     * @see \ASN1\Element::_encodedContentDER()
76
-     * @return string
77
-     */
78
-    protected function _encodedContentDER(): string
79
-    {
80
-        $data = "";
81
-        foreach ($this->_elements as $element) {
82
-            $data .= $element->toDER();
83
-        }
84
-        return $data;
85
-    }
73
+	/**
74
+	 *
75
+	 * @see \ASN1\Element::_encodedContentDER()
76
+	 * @return string
77
+	 */
78
+	protected function _encodedContentDER(): string
79
+	{
80
+		$data = "";
81
+		foreach ($this->_elements as $element) {
82
+			$data .= $element->toDER();
83
+		}
84
+		return $data;
85
+	}
86 86
     
87
-    /**
88
-     *
89
-     * {@inheritdoc}
90
-     * @see \ASN1\Element::_decodeFromDER()
91
-     * @return self
92
-     */
93
-    protected static function _decodeFromDER(Identifier $identifier, string $data,
94
-        int &$offset): ElementBase
95
-    {
96
-        $idx = $offset;
97
-        if (!$identifier->isConstructed()) {
98
-            throw new DecodeException(
99
-                "Structured element must have constructed bit set.");
100
-        }
101
-        $length = Length::expectFromDER($data, $idx);
102
-        $end = $idx + $length->intLength();
103
-        $elements = [];
104
-        while ($idx < $end) {
105
-            $elements[] = Element::fromDER($data, $idx);
106
-            // check that element didn't overflow length
107
-            if ($idx > $end) {
108
-                throw new DecodeException(
109
-                    "Structure's content overflows length.");
110
-            }
111
-        }
112
-        $offset = $idx;
113
-        // return instance by static late binding
114
-        return new static(...$elements);
115
-    }
87
+	/**
88
+	 *
89
+	 * {@inheritdoc}
90
+	 * @see \ASN1\Element::_decodeFromDER()
91
+	 * @return self
92
+	 */
93
+	protected static function _decodeFromDER(Identifier $identifier, string $data,
94
+		int &$offset): ElementBase
95
+	{
96
+		$idx = $offset;
97
+		if (!$identifier->isConstructed()) {
98
+			throw new DecodeException(
99
+				"Structured element must have constructed bit set.");
100
+		}
101
+		$length = Length::expectFromDER($data, $idx);
102
+		$end = $idx + $length->intLength();
103
+		$elements = [];
104
+		while ($idx < $end) {
105
+			$elements[] = Element::fromDER($data, $idx);
106
+			// check that element didn't overflow length
107
+			if ($idx > $end) {
108
+				throw new DecodeException(
109
+					"Structure's content overflows length.");
110
+			}
111
+		}
112
+		$offset = $idx;
113
+		// return instance by static late binding
114
+		return new static(...$elements);
115
+	}
116 116
     
117
-    /**
118
-     * Explode DER structure to DER encoded components that it contains.
119
-     *
120
-     * @param string $data
121
-     * @throws DecodeException
122
-     * @return string[]
123
-     */
124
-    public static function explodeDER(string $data): array
125
-    {
126
-        $offset = 0;
127
-        $identifier = Identifier::fromDER($data, $offset);
128
-        if (!$identifier->isConstructed()) {
129
-            throw new DecodeException("Element is not constructed.");
130
-        }
131
-        $length = Length::expectFromDER($data, $offset)->intLength();
132
-        $end = $offset + $length;
133
-        $parts = [];
134
-        while ($offset < $end) {
135
-            // start of the element
136
-            $idx = $offset;
137
-            // skip identifier
138
-            Identifier::fromDER($data, $offset);
139
-            // decode element length
140
-            $length = Length::expectFromDER($data, $offset)->intLength();
141
-            // extract der encoding of the element
142
-            $parts[] = substr($data, $idx, $offset - $idx + $length);
143
-            // update offset over content
144
-            $offset += $length;
145
-        }
146
-        return $parts;
147
-    }
117
+	/**
118
+	 * Explode DER structure to DER encoded components that it contains.
119
+	 *
120
+	 * @param string $data
121
+	 * @throws DecodeException
122
+	 * @return string[]
123
+	 */
124
+	public static function explodeDER(string $data): array
125
+	{
126
+		$offset = 0;
127
+		$identifier = Identifier::fromDER($data, $offset);
128
+		if (!$identifier->isConstructed()) {
129
+			throw new DecodeException("Element is not constructed.");
130
+		}
131
+		$length = Length::expectFromDER($data, $offset)->intLength();
132
+		$end = $offset + $length;
133
+		$parts = [];
134
+		while ($offset < $end) {
135
+			// start of the element
136
+			$idx = $offset;
137
+			// skip identifier
138
+			Identifier::fromDER($data, $offset);
139
+			// decode element length
140
+			$length = Length::expectFromDER($data, $offset)->intLength();
141
+			// extract der encoding of the element
142
+			$parts[] = substr($data, $idx, $offset - $idx + $length);
143
+			// update offset over content
144
+			$offset += $length;
145
+		}
146
+		return $parts;
147
+	}
148 148
     
149
-    /**
150
-     * Get self with an element at the given index replaced by another.
151
-     *
152
-     * @param int $idx Element index
153
-     * @param Element $el New element to insert into the structure
154
-     * @throws \OutOfBoundsException
155
-     * @return self
156
-     */
157
-    public function withReplaced(int $idx, Element $el): self
158
-    {
159
-        if (!isset($this->_elements[$idx])) {
160
-            throw new \OutOfBoundsException(
161
-                "Structure doesn't have element at index $idx.");
162
-        }
163
-        $obj = clone $this;
164
-        $obj->_elements[$idx] = $el;
165
-        return $obj;
166
-    }
149
+	/**
150
+	 * Get self with an element at the given index replaced by another.
151
+	 *
152
+	 * @param int $idx Element index
153
+	 * @param Element $el New element to insert into the structure
154
+	 * @throws \OutOfBoundsException
155
+	 * @return self
156
+	 */
157
+	public function withReplaced(int $idx, Element $el): self
158
+	{
159
+		if (!isset($this->_elements[$idx])) {
160
+			throw new \OutOfBoundsException(
161
+				"Structure doesn't have element at index $idx.");
162
+		}
163
+		$obj = clone $this;
164
+		$obj->_elements[$idx] = $el;
165
+		return $obj;
166
+	}
167 167
     
168
-    /**
169
-     * Get self with an element inserted before the given index.
170
-     *
171
-     * @param int $idx Element index
172
-     * @param Element $el New element to insert into the structure
173
-     * @throws \OutOfBoundsException
174
-     * @return self
175
-     */
176
-    public function withInserted(int $idx, Element $el): self
177
-    {
178
-        if (count($this->_elements) < $idx || $idx < 0) {
179
-            throw new \OutOfBoundsException("Index $idx is out of bounds.");
180
-        }
181
-        $obj = clone $this;
182
-        array_splice($obj->_elements, $idx, 0, [$el]);
183
-        return $obj;
184
-    }
168
+	/**
169
+	 * Get self with an element inserted before the given index.
170
+	 *
171
+	 * @param int $idx Element index
172
+	 * @param Element $el New element to insert into the structure
173
+	 * @throws \OutOfBoundsException
174
+	 * @return self
175
+	 */
176
+	public function withInserted(int $idx, Element $el): self
177
+	{
178
+		if (count($this->_elements) < $idx || $idx < 0) {
179
+			throw new \OutOfBoundsException("Index $idx is out of bounds.");
180
+		}
181
+		$obj = clone $this;
182
+		array_splice($obj->_elements, $idx, 0, [$el]);
183
+		return $obj;
184
+	}
185 185
     
186
-    /**
187
-     * Get self with an element appended to the end.
188
-     *
189
-     * @param Element $el Element to insert into the structure
190
-     * @return self
191
-     */
192
-    public function withAppended(Element $el): self
193
-    {
194
-        $obj = clone $this;
195
-        array_push($obj->_elements, $el);
196
-        return $obj;
197
-    }
186
+	/**
187
+	 * Get self with an element appended to the end.
188
+	 *
189
+	 * @param Element $el Element to insert into the structure
190
+	 * @return self
191
+	 */
192
+	public function withAppended(Element $el): self
193
+	{
194
+		$obj = clone $this;
195
+		array_push($obj->_elements, $el);
196
+		return $obj;
197
+	}
198 198
     
199
-    /**
200
-     * Get self with an element prepended in the beginning.
201
-     *
202
-     * @param Element $el Element to insert into the structure
203
-     * @return self
204
-     */
205
-    public function withPrepended(Element $el): self
206
-    {
207
-        $obj = clone $this;
208
-        array_unshift($obj->_elements, $el);
209
-        return $obj;
210
-    }
199
+	/**
200
+	 * Get self with an element prepended in the beginning.
201
+	 *
202
+	 * @param Element $el Element to insert into the structure
203
+	 * @return self
204
+	 */
205
+	public function withPrepended(Element $el): self
206
+	{
207
+		$obj = clone $this;
208
+		array_unshift($obj->_elements, $el);
209
+		return $obj;
210
+	}
211 211
     
212
-    /**
213
-     * Get self with an element at the given index removed.
214
-     *
215
-     * @param int $idx Element index
216
-     * @throws \OutOfBoundsException
217
-     * @return self
218
-     */
219
-    public function withoutElement(int $idx): self
220
-    {
221
-        if (!isset($this->_elements[$idx])) {
222
-            throw new \OutOfBoundsException(
223
-                "Structure doesn't have element at index $idx.");
224
-        }
225
-        $obj = clone $this;
226
-        array_splice($obj->_elements, $idx, 1);
227
-        return $obj;
228
-    }
212
+	/**
213
+	 * Get self with an element at the given index removed.
214
+	 *
215
+	 * @param int $idx Element index
216
+	 * @throws \OutOfBoundsException
217
+	 * @return self
218
+	 */
219
+	public function withoutElement(int $idx): self
220
+	{
221
+		if (!isset($this->_elements[$idx])) {
222
+			throw new \OutOfBoundsException(
223
+				"Structure doesn't have element at index $idx.");
224
+		}
225
+		$obj = clone $this;
226
+		array_splice($obj->_elements, $idx, 1);
227
+		return $obj;
228
+	}
229 229
     
230
-    /**
231
-     * Get elements in the structure.
232
-     *
233
-     * @return UnspecifiedType[]
234
-     */
235
-    public function elements(): array
236
-    {
237
-        if (!isset($this->_unspecifiedTypes)) {
238
-            $this->_unspecifiedTypes = array_map(
239
-                function (Element $el) {
240
-                    return new UnspecifiedType($el);
241
-                }, $this->_elements);
242
-        }
243
-        return $this->_unspecifiedTypes;
244
-    }
230
+	/**
231
+	 * Get elements in the structure.
232
+	 *
233
+	 * @return UnspecifiedType[]
234
+	 */
235
+	public function elements(): array
236
+	{
237
+		if (!isset($this->_unspecifiedTypes)) {
238
+			$this->_unspecifiedTypes = array_map(
239
+				function (Element $el) {
240
+					return new UnspecifiedType($el);
241
+				}, $this->_elements);
242
+		}
243
+		return $this->_unspecifiedTypes;
244
+	}
245 245
     
246
-    /**
247
-     * Check whether the structure has an element at the given index, optionally
248
-     * satisfying given tag expectation.
249
-     *
250
-     * @param int $idx Index 0..n
251
-     * @param int|null $expectedTag Optional type tag expectation
252
-     * @return bool
253
-     */
254
-    public function has(int $idx, $expectedTag = null): bool
255
-    {
256
-        if (!isset($this->_elements[$idx])) {
257
-            return false;
258
-        }
259
-        if (isset($expectedTag)) {
260
-            if (!$this->_elements[$idx]->isType($expectedTag)) {
261
-                return false;
262
-            }
263
-        }
264
-        return true;
265
-    }
246
+	/**
247
+	 * Check whether the structure has an element at the given index, optionally
248
+	 * satisfying given tag expectation.
249
+	 *
250
+	 * @param int $idx Index 0..n
251
+	 * @param int|null $expectedTag Optional type tag expectation
252
+	 * @return bool
253
+	 */
254
+	public function has(int $idx, $expectedTag = null): bool
255
+	{
256
+		if (!isset($this->_elements[$idx])) {
257
+			return false;
258
+		}
259
+		if (isset($expectedTag)) {
260
+			if (!$this->_elements[$idx]->isType($expectedTag)) {
261
+				return false;
262
+			}
263
+		}
264
+		return true;
265
+	}
266 266
     
267
-    /**
268
-     * Get the element at the given index, optionally checking that the element
269
-     * has a given tag.
270
-     *
271
-     * NOTE! Expectation checking is deprecated and should be done
272
-     * with UnspecifiedType.
273
-     *
274
-     * @param int $idx Index 0..n
275
-     * @param int|null $expectedTag Optional type tag expectation
276
-     * @throws \OutOfBoundsException If element doesn't exists
277
-     * @throws \UnexpectedValueException If expectation fails
278
-     * @return UnspecifiedType
279
-     */
280
-    public function at(int $idx, $expectedTag = null): UnspecifiedType
281
-    {
282
-        if (!isset($this->_elements[$idx])) {
283
-            throw new \OutOfBoundsException(
284
-                "Structure doesn't have an element at index $idx.");
285
-        }
286
-        $element = $this->_elements[$idx];
287
-        if (isset($expectedTag)) {
288
-            $element->expectType($expectedTag);
289
-        }
290
-        return new UnspecifiedType($element);
291
-    }
267
+	/**
268
+	 * Get the element at the given index, optionally checking that the element
269
+	 * has a given tag.
270
+	 *
271
+	 * NOTE! Expectation checking is deprecated and should be done
272
+	 * with UnspecifiedType.
273
+	 *
274
+	 * @param int $idx Index 0..n
275
+	 * @param int|null $expectedTag Optional type tag expectation
276
+	 * @throws \OutOfBoundsException If element doesn't exists
277
+	 * @throws \UnexpectedValueException If expectation fails
278
+	 * @return UnspecifiedType
279
+	 */
280
+	public function at(int $idx, $expectedTag = null): UnspecifiedType
281
+	{
282
+		if (!isset($this->_elements[$idx])) {
283
+			throw new \OutOfBoundsException(
284
+				"Structure doesn't have an element at index $idx.");
285
+		}
286
+		$element = $this->_elements[$idx];
287
+		if (isset($expectedTag)) {
288
+			$element->expectType($expectedTag);
289
+		}
290
+		return new UnspecifiedType($element);
291
+	}
292 292
     
293
-    /**
294
-     * Check whether the structure contains a context specific element with a
295
-     * given tag.
296
-     *
297
-     * @param int $tag Tag number
298
-     * @return boolean
299
-     */
300
-    public function hasTagged(int $tag): bool
301
-    {
302
-        // lazily build lookup map
303
-        if (!isset($this->_taggedMap)) {
304
-            $this->_taggedMap = [];
305
-            foreach ($this->_elements as $element) {
306
-                if ($element->isTagged()) {
307
-                    $this->_taggedMap[$element->tag()] = $element;
308
-                }
309
-            }
310
-        }
311
-        return isset($this->_taggedMap[$tag]);
312
-    }
293
+	/**
294
+	 * Check whether the structure contains a context specific element with a
295
+	 * given tag.
296
+	 *
297
+	 * @param int $tag Tag number
298
+	 * @return boolean
299
+	 */
300
+	public function hasTagged(int $tag): bool
301
+	{
302
+		// lazily build lookup map
303
+		if (!isset($this->_taggedMap)) {
304
+			$this->_taggedMap = [];
305
+			foreach ($this->_elements as $element) {
306
+				if ($element->isTagged()) {
307
+					$this->_taggedMap[$element->tag()] = $element;
308
+				}
309
+			}
310
+		}
311
+		return isset($this->_taggedMap[$tag]);
312
+	}
313 313
     
314
-    /**
315
-     * Get a context specific element tagged with a given tag.
316
-     *
317
-     * @param int $tag
318
-     * @throws \LogicException If tag doesn't exists
319
-     * @return TaggedType
320
-     */
321
-    public function getTagged(int $tag): TaggedType
322
-    {
323
-        if (!$this->hasTagged($tag)) {
324
-            throw new \LogicException("No tagged element for tag $tag.");
325
-        }
326
-        return $this->_taggedMap[$tag];
327
-    }
314
+	/**
315
+	 * Get a context specific element tagged with a given tag.
316
+	 *
317
+	 * @param int $tag
318
+	 * @throws \LogicException If tag doesn't exists
319
+	 * @return TaggedType
320
+	 */
321
+	public function getTagged(int $tag): TaggedType
322
+	{
323
+		if (!$this->hasTagged($tag)) {
324
+			throw new \LogicException("No tagged element for tag $tag.");
325
+		}
326
+		return $this->_taggedMap[$tag];
327
+	}
328 328
     
329
-    /**
330
-     *
331
-     * @see \Countable::count()
332
-     * @return int
333
-     */
334
-    public function count(): int
335
-    {
336
-        return count($this->_elements);
337
-    }
329
+	/**
330
+	 *
331
+	 * @see \Countable::count()
332
+	 * @return int
333
+	 */
334
+	public function count(): int
335
+	{
336
+		return count($this->_elements);
337
+	}
338 338
     
339
-    /**
340
-     * Get an iterator for the UnspecifiedElement objects.
341
-     *
342
-     * @see \IteratorAggregate::getIterator()
343
-     * @return \ArrayIterator
344
-     */
345
-    public function getIterator(): \ArrayIterator
346
-    {
347
-        return new \ArrayIterator($this->elements());
348
-    }
339
+	/**
340
+	 * Get an iterator for the UnspecifiedElement objects.
341
+	 *
342
+	 * @see \IteratorAggregate::getIterator()
343
+	 * @return \ArrayIterator
344
+	 */
345
+	public function getIterator(): \ArrayIterator
346
+	{
347
+		return new \ArrayIterator($this->elements());
348
+	}
349 349
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/UnspecifiedType.php 1 patch
Indentation   +630 added lines, -630 removed lines patch added patch discarded remove patch
@@ -17,634 +17,634 @@
 block discarded – undo
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
-     * @return self
42
-     */
43
-    public static function fromDER(string $data): self
44
-    {
45
-        return new self(Element::fromDER($data));
46
-    }
47
-    
48
-    /**
49
-     * Initialize from ElementBase interface.
50
-     *
51
-     * @param ElementBase $el
52
-     * @return self
53
-     */
54
-    public static function fromElementBase(ElementBase $el): self
55
-    {
56
-        // if element is already wrapped
57
-        if ($el instanceof self) {
58
-            return $el;
59
-        }
60
-        return new self($el->asElement());
61
-    }
62
-    
63
-    /**
64
-     * Compatibility method to dispatch calls to the wrapped element.
65
-     *
66
-     * @deprecated Use <code>as*</code> accessor methods to ensure strict type
67
-     * @param string $mtd Method name
68
-     * @param array $args Arguments
69
-     * @return mixed
70
-     */
71
-    public function __call($mtd, array $args)
72
-    {
73
-        return call_user_func_array([$this->_element, $mtd], $args);
74
-    }
75
-    
76
-    /**
77
-     * Get the wrapped element as a context specific tagged type.
78
-     *
79
-     * @throws \UnexpectedValueException If the element is not tagged
80
-     * @return TaggedType
81
-     */
82
-    public function asTagged(): TaggedType
83
-    {
84
-        if (!$this->_element instanceof TaggedType) {
85
-            throw new \UnexpectedValueException(
86
-                "Tagged element expected, got " . $this->_typeDescriptorString());
87
-        }
88
-        return $this->_element;
89
-    }
90
-    
91
-    /**
92
-     * Get the wrapped element as a boolean type.
93
-     *
94
-     * @throws \UnexpectedValueException If the element is not a boolean
95
-     * @return Primitive\Boolean
96
-     */
97
-    public function asBoolean(): Primitive\Boolean
98
-    {
99
-        if (!$this->_element instanceof Primitive\Boolean) {
100
-            throw new \UnexpectedValueException(
101
-                $this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
102
-        }
103
-        return $this->_element;
104
-    }
105
-    
106
-    /**
107
-     * Get the wrapped element as an integer type.
108
-     *
109
-     * @throws \UnexpectedValueException If the element is not an integer
110
-     * @return Primitive\Integer
111
-     */
112
-    public function asInteger(): Primitive\Integer
113
-    {
114
-        if (!$this->_element instanceof Primitive\Integer) {
115
-            throw new \UnexpectedValueException(
116
-                $this->_generateExceptionMessage(Element::TYPE_INTEGER));
117
-        }
118
-        return $this->_element;
119
-    }
120
-    
121
-    /**
122
-     * Get the wrapped element as a bit string type.
123
-     *
124
-     * @throws \UnexpectedValueException If the element is not a bit string
125
-     * @return Primitive\BitString
126
-     */
127
-    public function asBitString(): Primitive\BitString
128
-    {
129
-        if (!$this->_element instanceof Primitive\BitString) {
130
-            throw new \UnexpectedValueException(
131
-                $this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
132
-        }
133
-        return $this->_element;
134
-    }
135
-    
136
-    /**
137
-     * Get the wrapped element as an octet string type.
138
-     *
139
-     * @throws \UnexpectedValueException If the element is not an octet string
140
-     * @return Primitive\OctetString
141
-     */
142
-    public function asOctetString(): Primitive\OctetString
143
-    {
144
-        if (!$this->_element instanceof Primitive\OctetString) {
145
-            throw new \UnexpectedValueException(
146
-                $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
147
-        }
148
-        return $this->_element;
149
-    }
150
-    
151
-    /**
152
-     * Get the wrapped element as a null type.
153
-     *
154
-     * @throws \UnexpectedValueException If the element is not a null
155
-     * @return Primitive\NullType
156
-     */
157
-    public function asNull(): Primitive\NullType
158
-    {
159
-        if (!$this->_element instanceof Primitive\NullType) {
160
-            throw new \UnexpectedValueException(
161
-                $this->_generateExceptionMessage(Element::TYPE_NULL));
162
-        }
163
-        return $this->_element;
164
-    }
165
-    
166
-    /**
167
-     * Get the wrapped element as an object identifier type.
168
-     *
169
-     * @throws \UnexpectedValueException If the element is not an object
170
-     *         identifier
171
-     * @return Primitive\ObjectIdentifier
172
-     */
173
-    public function asObjectIdentifier(): Primitive\ObjectIdentifier
174
-    {
175
-        if (!$this->_element instanceof Primitive\ObjectIdentifier) {
176
-            throw new \UnexpectedValueException(
177
-                $this->_generateExceptionMessage(
178
-                    Element::TYPE_OBJECT_IDENTIFIER));
179
-        }
180
-        return $this->_element;
181
-    }
182
-    
183
-    /**
184
-     * Get the wrapped element as an object descriptor type.
185
-     *
186
-     * @throws \UnexpectedValueException If the element is not an object
187
-     *         descriptor
188
-     * @return Primitive\ObjectDescriptor
189
-     */
190
-    public function asObjectDescriptor(): Primitive\ObjectDescriptor
191
-    {
192
-        if (!$this->_element instanceof Primitive\ObjectDescriptor) {
193
-            throw new \UnexpectedValueException(
194
-                $this->_generateExceptionMessage(
195
-                    Element::TYPE_OBJECT_DESCRIPTOR));
196
-        }
197
-        return $this->_element;
198
-    }
199
-    
200
-    /**
201
-     * Get the wrapped element as a real type.
202
-     *
203
-     * @throws \UnexpectedValueException If the element is not a real
204
-     * @return Primitive\Real
205
-     */
206
-    public function asReal(): Primitive\Real
207
-    {
208
-        if (!$this->_element instanceof Primitive\Real) {
209
-            throw new \UnexpectedValueException(
210
-                $this->_generateExceptionMessage(Element::TYPE_REAL));
211
-        }
212
-        return $this->_element;
213
-    }
214
-    
215
-    /**
216
-     * Get the wrapped element as an enumerated type.
217
-     *
218
-     * @throws \UnexpectedValueException If the element is not an enumerated
219
-     * @return Primitive\Enumerated
220
-     */
221
-    public function asEnumerated(): Primitive\Enumerated
222
-    {
223
-        if (!$this->_element instanceof Primitive\Enumerated) {
224
-            throw new \UnexpectedValueException(
225
-                $this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
226
-        }
227
-        return $this->_element;
228
-    }
229
-    
230
-    /**
231
-     * Get the wrapped element as a UTF8 string type.
232
-     *
233
-     * @throws \UnexpectedValueException If the element is not a UTF8 string
234
-     * @return Primitive\UTF8String
235
-     */
236
-    public function asUTF8String(): Primitive\UTF8String
237
-    {
238
-        if (!$this->_element instanceof Primitive\UTF8String) {
239
-            throw new \UnexpectedValueException(
240
-                $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
241
-        }
242
-        return $this->_element;
243
-    }
244
-    
245
-    /**
246
-     * Get the wrapped element as a relative OID type.
247
-     *
248
-     * @throws \UnexpectedValueException If the element is not a relative OID
249
-     * @return Primitive\RelativeOID
250
-     */
251
-    public function asRelativeOID(): Primitive\RelativeOID
252
-    {
253
-        if (!$this->_element instanceof Primitive\RelativeOID) {
254
-            throw new \UnexpectedValueException(
255
-                $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
256
-        }
257
-        return $this->_element;
258
-    }
259
-    
260
-    /**
261
-     * Get the wrapped element as a sequence type.
262
-     *
263
-     * @throws \UnexpectedValueException If the element is not a sequence
264
-     * @return Constructed\Sequence
265
-     */
266
-    public function asSequence(): Constructed\Sequence
267
-    {
268
-        if (!$this->_element instanceof Constructed\Sequence) {
269
-            throw new \UnexpectedValueException(
270
-                $this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
271
-        }
272
-        return $this->_element;
273
-    }
274
-    
275
-    /**
276
-     * Get the wrapped element as a set type.
277
-     *
278
-     * @throws \UnexpectedValueException If the element is not a set
279
-     * @return Constructed\Set
280
-     */
281
-    public function asSet(): Constructed\Set
282
-    {
283
-        if (!$this->_element instanceof Constructed\Set) {
284
-            throw new \UnexpectedValueException(
285
-                $this->_generateExceptionMessage(Element::TYPE_SET));
286
-        }
287
-        return $this->_element;
288
-    }
289
-    
290
-    /**
291
-     * Get the wrapped element as a numeric string type.
292
-     *
293
-     * @throws \UnexpectedValueException If the element is not a numeric string
294
-     * @return Primitive\NumericString
295
-     */
296
-    public function asNumericString(): Primitive\NumericString
297
-    {
298
-        if (!$this->_element instanceof Primitive\NumericString) {
299
-            throw new \UnexpectedValueException(
300
-                $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
301
-        }
302
-        return $this->_element;
303
-    }
304
-    
305
-    /**
306
-     * Get the wrapped element as a printable string type.
307
-     *
308
-     * @throws \UnexpectedValueException If the element is not a printable
309
-     *         string
310
-     * @return Primitive\PrintableString
311
-     */
312
-    public function asPrintableString(): Primitive\PrintableString
313
-    {
314
-        if (!$this->_element instanceof Primitive\PrintableString) {
315
-            throw new \UnexpectedValueException(
316
-                $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
317
-        }
318
-        return $this->_element;
319
-    }
320
-    
321
-    /**
322
-     * Get the wrapped element as a T61 string type.
323
-     *
324
-     * @throws \UnexpectedValueException If the element is not a T61 string
325
-     * @return Primitive\T61String
326
-     */
327
-    public function asT61String(): Primitive\T61String
328
-    {
329
-        if (!$this->_element instanceof Primitive\T61String) {
330
-            throw new \UnexpectedValueException(
331
-                $this->_generateExceptionMessage(Element::TYPE_T61_STRING));
332
-        }
333
-        return $this->_element;
334
-    }
335
-    
336
-    /**
337
-     * Get the wrapped element as a videotex string type.
338
-     *
339
-     * @throws \UnexpectedValueException If the element is not a videotex string
340
-     * @return Primitive\VideotexString
341
-     */
342
-    public function asVideotexString(): Primitive\VideotexString
343
-    {
344
-        if (!$this->_element instanceof Primitive\VideotexString) {
345
-            throw new \UnexpectedValueException(
346
-                $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
347
-        }
348
-        return $this->_element;
349
-    }
350
-    
351
-    /**
352
-     * Get the wrapped element as a IA5 string type.
353
-     *
354
-     * @throws \UnexpectedValueException If the element is not a IA5 string
355
-     * @return Primitive\IA5String
356
-     */
357
-    public function asIA5String(): Primitive\IA5String
358
-    {
359
-        if (!$this->_element instanceof Primitive\IA5String) {
360
-            throw new \UnexpectedValueException(
361
-                $this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
362
-        }
363
-        return $this->_element;
364
-    }
365
-    
366
-    /**
367
-     * Get the wrapped element as an UTC time type.
368
-     *
369
-     * @throws \UnexpectedValueException If the element is not a UTC time
370
-     * @return Primitive\UTCTime
371
-     */
372
-    public function asUTCTime(): Primitive\UTCTime
373
-    {
374
-        if (!$this->_element instanceof Primitive\UTCTime) {
375
-            throw new \UnexpectedValueException(
376
-                $this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
377
-        }
378
-        return $this->_element;
379
-    }
380
-    
381
-    /**
382
-     * Get the wrapped element as a generalized time type.
383
-     *
384
-     * @throws \UnexpectedValueException If the element is not a generalized
385
-     *         time
386
-     * @return Primitive\GeneralizedTime
387
-     */
388
-    public function asGeneralizedTime(): Primitive\GeneralizedTime
389
-    {
390
-        if (!$this->_element instanceof Primitive\GeneralizedTime) {
391
-            throw new \UnexpectedValueException(
392
-                $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
393
-        }
394
-        return $this->_element;
395
-    }
396
-    
397
-    /**
398
-     * Get the wrapped element as a graphic string type.
399
-     *
400
-     * @throws \UnexpectedValueException If the element is not a graphic string
401
-     * @return Primitive\GraphicString
402
-     */
403
-    public function asGraphicString(): Primitive\GraphicString
404
-    {
405
-        if (!$this->_element instanceof Primitive\GraphicString) {
406
-            throw new \UnexpectedValueException(
407
-                $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
408
-        }
409
-        return $this->_element;
410
-    }
411
-    
412
-    /**
413
-     * Get the wrapped element as a visible string type.
414
-     *
415
-     * @throws \UnexpectedValueException If the element is not a visible string
416
-     * @return Primitive\VisibleString
417
-     */
418
-    public function asVisibleString(): Primitive\VisibleString
419
-    {
420
-        if (!$this->_element instanceof Primitive\VisibleString) {
421
-            throw new \UnexpectedValueException(
422
-                $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
423
-        }
424
-        return $this->_element;
425
-    }
426
-    
427
-    /**
428
-     * Get the wrapped element as a general string type.
429
-     *
430
-     * @throws \UnexpectedValueException If the element is not general string
431
-     * @return Primitive\GeneralString
432
-     */
433
-    public function asGeneralString(): Primitive\GeneralString
434
-    {
435
-        if (!$this->_element instanceof Primitive\GeneralString) {
436
-            throw new \UnexpectedValueException(
437
-                $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
438
-        }
439
-        return $this->_element;
440
-    }
441
-    
442
-    /**
443
-     * Get the wrapped element as a universal string type.
444
-     *
445
-     * @throws \UnexpectedValueException If the element is not a universal
446
-     *         string
447
-     * @return Primitive\UniversalString
448
-     */
449
-    public function asUniversalString(): Primitive\UniversalString
450
-    {
451
-        if (!$this->_element instanceof Primitive\UniversalString) {
452
-            throw new \UnexpectedValueException(
453
-                $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
454
-        }
455
-        return $this->_element;
456
-    }
457
-    
458
-    /**
459
-     * Get the wrapped element as a character string type.
460
-     *
461
-     * @throws \UnexpectedValueException If the element is not a character
462
-     *         string
463
-     * @return Primitive\CharacterString
464
-     */
465
-    public function asCharacterString(): Primitive\CharacterString
466
-    {
467
-        if (!$this->_element instanceof Primitive\CharacterString) {
468
-            throw new \UnexpectedValueException(
469
-                $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
470
-        }
471
-        return $this->_element;
472
-    }
473
-    
474
-    /**
475
-     * Get the wrapped element as a BMP string type.
476
-     *
477
-     * @throws \UnexpectedValueException If the element is not a bmp string
478
-     * @return Primitive\BMPString
479
-     */
480
-    public function asBMPString(): Primitive\BMPString
481
-    {
482
-        if (!$this->_element instanceof Primitive\BMPString) {
483
-            throw new \UnexpectedValueException(
484
-                $this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
485
-        }
486
-        return $this->_element;
487
-    }
488
-    
489
-    /**
490
-     * Get the wrapped element as any string type.
491
-     *
492
-     * @throws \UnexpectedValueException If the element is not a string
493
-     * @return StringType
494
-     */
495
-    public function asString(): StringType
496
-    {
497
-        if (!$this->_element instanceof StringType) {
498
-            throw new \UnexpectedValueException(
499
-                $this->_generateExceptionMessage(Element::TYPE_STRING));
500
-        }
501
-        return $this->_element;
502
-    }
503
-    
504
-    /**
505
-     * Get the wrapped element as any time type.
506
-     *
507
-     * @throws \UnexpectedValueException If the element is not a time
508
-     * @return TimeType
509
-     */
510
-    public function asTime(): TimeType
511
-    {
512
-        if (!$this->_element instanceof TimeType) {
513
-            throw new \UnexpectedValueException(
514
-                $this->_generateExceptionMessage(Element::TYPE_TIME));
515
-        }
516
-        return $this->_element;
517
-    }
518
-    
519
-    /**
520
-     * Generate message for exceptions thrown by <code>as*</code> methods.
521
-     *
522
-     * @param int $tag Type tag of the expected element
523
-     * @return string
524
-     */
525
-    private function _generateExceptionMessage(int $tag): string
526
-    {
527
-        return sprintf("%s expected, got %s.", Element::tagToName($tag),
528
-            $this->_typeDescriptorString());
529
-    }
530
-    
531
-    /**
532
-     * Get textual description of the wrapped element for debugging purposes.
533
-     *
534
-     * @return string
535
-     */
536
-    private function _typeDescriptorString(): string
537
-    {
538
-        $type_cls = $this->_element->typeClass();
539
-        $tag = $this->_element->tag();
540
-        if ($type_cls == Identifier::CLASS_UNIVERSAL) {
541
-            return Element::tagToName($tag);
542
-        }
543
-        return Identifier::classToName($type_cls) . " TAG $tag";
544
-    }
545
-    
546
-    /**
547
-     *
548
-     * @see \ASN1\Feature\Encodable::toDER()
549
-     * @return string
550
-     */
551
-    public function toDER(): string
552
-    {
553
-        return $this->_element->toDER();
554
-    }
555
-    
556
-    /**
557
-     *
558
-     * @see \ASN1\Feature\ElementBase::typeClass()
559
-     * @return int
560
-     */
561
-    public function typeClass(): int
562
-    {
563
-        return $this->_element->typeClass();
564
-    }
565
-    
566
-    /**
567
-     *
568
-     * @see \ASN1\Feature\ElementBase::isConstructed()
569
-     * @return bool
570
-     */
571
-    public function isConstructed(): bool
572
-    {
573
-        return $this->_element->isConstructed();
574
-    }
575
-    
576
-    /**
577
-     *
578
-     * @see \ASN1\Feature\ElementBase::tag()
579
-     * @return int
580
-     */
581
-    public function tag(): int
582
-    {
583
-        return $this->_element->tag();
584
-    }
585
-    
586
-    /**
587
-     *
588
-     * {@inheritdoc}
589
-     * @see \ASN1\Feature\ElementBase::isType()
590
-     * @return bool
591
-     */
592
-    public function isType(int $tag): bool
593
-    {
594
-        return $this->_element->isType($tag);
595
-    }
596
-    
597
-    /**
598
-     *
599
-     * @deprecated Use any <code>as*</code> accessor method first to ensure
600
-     *             type strictness.
601
-     * @see \ASN1\Feature\ElementBase::expectType()
602
-     * @return ElementBase
603
-     */
604
-    public function expectType(int $tag): ElementBase
605
-    {
606
-        return $this->_element->expectType($tag);
607
-    }
608
-    
609
-    /**
610
-     *
611
-     * @see \ASN1\Feature\ElementBase::isTagged()
612
-     * @return bool
613
-     */
614
-    public function isTagged(): bool
615
-    {
616
-        return $this->_element->isTagged();
617
-    }
618
-    
619
-    /**
620
-     *
621
-     * @deprecated Use any <code>as*</code> accessor method first to ensure
622
-     *             type strictness.
623
-     * @see \ASN1\Feature\ElementBase::expectTagged()
624
-     * @return TaggedType
625
-     */
626
-    public function expectTagged($tag = null): TaggedType
627
-    {
628
-        return $this->_element->expectTagged($tag);
629
-    }
630
-    
631
-    /**
632
-     *
633
-     * @see \ASN1\Feature\ElementBase::asElement()
634
-     * @return Element
635
-     */
636
-    public function asElement(): Element
637
-    {
638
-        return $this->_element;
639
-    }
640
-    
641
-    /**
642
-     *
643
-     * {@inheritdoc}
644
-     * @return UnspecifiedType
645
-     */
646
-    public function asUnspecified(): UnspecifiedType
647
-    {
648
-        return $this;
649
-    }
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
+	 * @return self
42
+	 */
43
+	public static function fromDER(string $data): self
44
+	{
45
+		return new self(Element::fromDER($data));
46
+	}
47
+    
48
+	/**
49
+	 * Initialize from ElementBase interface.
50
+	 *
51
+	 * @param ElementBase $el
52
+	 * @return self
53
+	 */
54
+	public static function fromElementBase(ElementBase $el): self
55
+	{
56
+		// if element is already wrapped
57
+		if ($el instanceof self) {
58
+			return $el;
59
+		}
60
+		return new self($el->asElement());
61
+	}
62
+    
63
+	/**
64
+	 * Compatibility method to dispatch calls to the wrapped element.
65
+	 *
66
+	 * @deprecated Use <code>as*</code> accessor methods to ensure strict type
67
+	 * @param string $mtd Method name
68
+	 * @param array $args Arguments
69
+	 * @return mixed
70
+	 */
71
+	public function __call($mtd, array $args)
72
+	{
73
+		return call_user_func_array([$this->_element, $mtd], $args);
74
+	}
75
+    
76
+	/**
77
+	 * Get the wrapped element as a context specific tagged type.
78
+	 *
79
+	 * @throws \UnexpectedValueException If the element is not tagged
80
+	 * @return TaggedType
81
+	 */
82
+	public function asTagged(): TaggedType
83
+	{
84
+		if (!$this->_element instanceof TaggedType) {
85
+			throw new \UnexpectedValueException(
86
+				"Tagged element expected, got " . $this->_typeDescriptorString());
87
+		}
88
+		return $this->_element;
89
+	}
90
+    
91
+	/**
92
+	 * Get the wrapped element as a boolean type.
93
+	 *
94
+	 * @throws \UnexpectedValueException If the element is not a boolean
95
+	 * @return Primitive\Boolean
96
+	 */
97
+	public function asBoolean(): Primitive\Boolean
98
+	{
99
+		if (!$this->_element instanceof Primitive\Boolean) {
100
+			throw new \UnexpectedValueException(
101
+				$this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
102
+		}
103
+		return $this->_element;
104
+	}
105
+    
106
+	/**
107
+	 * Get the wrapped element as an integer type.
108
+	 *
109
+	 * @throws \UnexpectedValueException If the element is not an integer
110
+	 * @return Primitive\Integer
111
+	 */
112
+	public function asInteger(): Primitive\Integer
113
+	{
114
+		if (!$this->_element instanceof Primitive\Integer) {
115
+			throw new \UnexpectedValueException(
116
+				$this->_generateExceptionMessage(Element::TYPE_INTEGER));
117
+		}
118
+		return $this->_element;
119
+	}
120
+    
121
+	/**
122
+	 * Get the wrapped element as a bit string type.
123
+	 *
124
+	 * @throws \UnexpectedValueException If the element is not a bit string
125
+	 * @return Primitive\BitString
126
+	 */
127
+	public function asBitString(): Primitive\BitString
128
+	{
129
+		if (!$this->_element instanceof Primitive\BitString) {
130
+			throw new \UnexpectedValueException(
131
+				$this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
132
+		}
133
+		return $this->_element;
134
+	}
135
+    
136
+	/**
137
+	 * Get the wrapped element as an octet string type.
138
+	 *
139
+	 * @throws \UnexpectedValueException If the element is not an octet string
140
+	 * @return Primitive\OctetString
141
+	 */
142
+	public function asOctetString(): Primitive\OctetString
143
+	{
144
+		if (!$this->_element instanceof Primitive\OctetString) {
145
+			throw new \UnexpectedValueException(
146
+				$this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
147
+		}
148
+		return $this->_element;
149
+	}
150
+    
151
+	/**
152
+	 * Get the wrapped element as a null type.
153
+	 *
154
+	 * @throws \UnexpectedValueException If the element is not a null
155
+	 * @return Primitive\NullType
156
+	 */
157
+	public function asNull(): Primitive\NullType
158
+	{
159
+		if (!$this->_element instanceof Primitive\NullType) {
160
+			throw new \UnexpectedValueException(
161
+				$this->_generateExceptionMessage(Element::TYPE_NULL));
162
+		}
163
+		return $this->_element;
164
+	}
165
+    
166
+	/**
167
+	 * Get the wrapped element as an object identifier type.
168
+	 *
169
+	 * @throws \UnexpectedValueException If the element is not an object
170
+	 *         identifier
171
+	 * @return Primitive\ObjectIdentifier
172
+	 */
173
+	public function asObjectIdentifier(): Primitive\ObjectIdentifier
174
+	{
175
+		if (!$this->_element instanceof Primitive\ObjectIdentifier) {
176
+			throw new \UnexpectedValueException(
177
+				$this->_generateExceptionMessage(
178
+					Element::TYPE_OBJECT_IDENTIFIER));
179
+		}
180
+		return $this->_element;
181
+	}
182
+    
183
+	/**
184
+	 * Get the wrapped element as an object descriptor type.
185
+	 *
186
+	 * @throws \UnexpectedValueException If the element is not an object
187
+	 *         descriptor
188
+	 * @return Primitive\ObjectDescriptor
189
+	 */
190
+	public function asObjectDescriptor(): Primitive\ObjectDescriptor
191
+	{
192
+		if (!$this->_element instanceof Primitive\ObjectDescriptor) {
193
+			throw new \UnexpectedValueException(
194
+				$this->_generateExceptionMessage(
195
+					Element::TYPE_OBJECT_DESCRIPTOR));
196
+		}
197
+		return $this->_element;
198
+	}
199
+    
200
+	/**
201
+	 * Get the wrapped element as a real type.
202
+	 *
203
+	 * @throws \UnexpectedValueException If the element is not a real
204
+	 * @return Primitive\Real
205
+	 */
206
+	public function asReal(): Primitive\Real
207
+	{
208
+		if (!$this->_element instanceof Primitive\Real) {
209
+			throw new \UnexpectedValueException(
210
+				$this->_generateExceptionMessage(Element::TYPE_REAL));
211
+		}
212
+		return $this->_element;
213
+	}
214
+    
215
+	/**
216
+	 * Get the wrapped element as an enumerated type.
217
+	 *
218
+	 * @throws \UnexpectedValueException If the element is not an enumerated
219
+	 * @return Primitive\Enumerated
220
+	 */
221
+	public function asEnumerated(): Primitive\Enumerated
222
+	{
223
+		if (!$this->_element instanceof Primitive\Enumerated) {
224
+			throw new \UnexpectedValueException(
225
+				$this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
226
+		}
227
+		return $this->_element;
228
+	}
229
+    
230
+	/**
231
+	 * Get the wrapped element as a UTF8 string type.
232
+	 *
233
+	 * @throws \UnexpectedValueException If the element is not a UTF8 string
234
+	 * @return Primitive\UTF8String
235
+	 */
236
+	public function asUTF8String(): Primitive\UTF8String
237
+	{
238
+		if (!$this->_element instanceof Primitive\UTF8String) {
239
+			throw new \UnexpectedValueException(
240
+				$this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
241
+		}
242
+		return $this->_element;
243
+	}
244
+    
245
+	/**
246
+	 * Get the wrapped element as a relative OID type.
247
+	 *
248
+	 * @throws \UnexpectedValueException If the element is not a relative OID
249
+	 * @return Primitive\RelativeOID
250
+	 */
251
+	public function asRelativeOID(): Primitive\RelativeOID
252
+	{
253
+		if (!$this->_element instanceof Primitive\RelativeOID) {
254
+			throw new \UnexpectedValueException(
255
+				$this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
256
+		}
257
+		return $this->_element;
258
+	}
259
+    
260
+	/**
261
+	 * Get the wrapped element as a sequence type.
262
+	 *
263
+	 * @throws \UnexpectedValueException If the element is not a sequence
264
+	 * @return Constructed\Sequence
265
+	 */
266
+	public function asSequence(): Constructed\Sequence
267
+	{
268
+		if (!$this->_element instanceof Constructed\Sequence) {
269
+			throw new \UnexpectedValueException(
270
+				$this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
271
+		}
272
+		return $this->_element;
273
+	}
274
+    
275
+	/**
276
+	 * Get the wrapped element as a set type.
277
+	 *
278
+	 * @throws \UnexpectedValueException If the element is not a set
279
+	 * @return Constructed\Set
280
+	 */
281
+	public function asSet(): Constructed\Set
282
+	{
283
+		if (!$this->_element instanceof Constructed\Set) {
284
+			throw new \UnexpectedValueException(
285
+				$this->_generateExceptionMessage(Element::TYPE_SET));
286
+		}
287
+		return $this->_element;
288
+	}
289
+    
290
+	/**
291
+	 * Get the wrapped element as a numeric string type.
292
+	 *
293
+	 * @throws \UnexpectedValueException If the element is not a numeric string
294
+	 * @return Primitive\NumericString
295
+	 */
296
+	public function asNumericString(): Primitive\NumericString
297
+	{
298
+		if (!$this->_element instanceof Primitive\NumericString) {
299
+			throw new \UnexpectedValueException(
300
+				$this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
301
+		}
302
+		return $this->_element;
303
+	}
304
+    
305
+	/**
306
+	 * Get the wrapped element as a printable string type.
307
+	 *
308
+	 * @throws \UnexpectedValueException If the element is not a printable
309
+	 *         string
310
+	 * @return Primitive\PrintableString
311
+	 */
312
+	public function asPrintableString(): Primitive\PrintableString
313
+	{
314
+		if (!$this->_element instanceof Primitive\PrintableString) {
315
+			throw new \UnexpectedValueException(
316
+				$this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
317
+		}
318
+		return $this->_element;
319
+	}
320
+    
321
+	/**
322
+	 * Get the wrapped element as a T61 string type.
323
+	 *
324
+	 * @throws \UnexpectedValueException If the element is not a T61 string
325
+	 * @return Primitive\T61String
326
+	 */
327
+	public function asT61String(): Primitive\T61String
328
+	{
329
+		if (!$this->_element instanceof Primitive\T61String) {
330
+			throw new \UnexpectedValueException(
331
+				$this->_generateExceptionMessage(Element::TYPE_T61_STRING));
332
+		}
333
+		return $this->_element;
334
+	}
335
+    
336
+	/**
337
+	 * Get the wrapped element as a videotex string type.
338
+	 *
339
+	 * @throws \UnexpectedValueException If the element is not a videotex string
340
+	 * @return Primitive\VideotexString
341
+	 */
342
+	public function asVideotexString(): Primitive\VideotexString
343
+	{
344
+		if (!$this->_element instanceof Primitive\VideotexString) {
345
+			throw new \UnexpectedValueException(
346
+				$this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
347
+		}
348
+		return $this->_element;
349
+	}
350
+    
351
+	/**
352
+	 * Get the wrapped element as a IA5 string type.
353
+	 *
354
+	 * @throws \UnexpectedValueException If the element is not a IA5 string
355
+	 * @return Primitive\IA5String
356
+	 */
357
+	public function asIA5String(): Primitive\IA5String
358
+	{
359
+		if (!$this->_element instanceof Primitive\IA5String) {
360
+			throw new \UnexpectedValueException(
361
+				$this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
362
+		}
363
+		return $this->_element;
364
+	}
365
+    
366
+	/**
367
+	 * Get the wrapped element as an UTC time type.
368
+	 *
369
+	 * @throws \UnexpectedValueException If the element is not a UTC time
370
+	 * @return Primitive\UTCTime
371
+	 */
372
+	public function asUTCTime(): Primitive\UTCTime
373
+	{
374
+		if (!$this->_element instanceof Primitive\UTCTime) {
375
+			throw new \UnexpectedValueException(
376
+				$this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
377
+		}
378
+		return $this->_element;
379
+	}
380
+    
381
+	/**
382
+	 * Get the wrapped element as a generalized time type.
383
+	 *
384
+	 * @throws \UnexpectedValueException If the element is not a generalized
385
+	 *         time
386
+	 * @return Primitive\GeneralizedTime
387
+	 */
388
+	public function asGeneralizedTime(): Primitive\GeneralizedTime
389
+	{
390
+		if (!$this->_element instanceof Primitive\GeneralizedTime) {
391
+			throw new \UnexpectedValueException(
392
+				$this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
393
+		}
394
+		return $this->_element;
395
+	}
396
+    
397
+	/**
398
+	 * Get the wrapped element as a graphic string type.
399
+	 *
400
+	 * @throws \UnexpectedValueException If the element is not a graphic string
401
+	 * @return Primitive\GraphicString
402
+	 */
403
+	public function asGraphicString(): Primitive\GraphicString
404
+	{
405
+		if (!$this->_element instanceof Primitive\GraphicString) {
406
+			throw new \UnexpectedValueException(
407
+				$this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
408
+		}
409
+		return $this->_element;
410
+	}
411
+    
412
+	/**
413
+	 * Get the wrapped element as a visible string type.
414
+	 *
415
+	 * @throws \UnexpectedValueException If the element is not a visible string
416
+	 * @return Primitive\VisibleString
417
+	 */
418
+	public function asVisibleString(): Primitive\VisibleString
419
+	{
420
+		if (!$this->_element instanceof Primitive\VisibleString) {
421
+			throw new \UnexpectedValueException(
422
+				$this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
423
+		}
424
+		return $this->_element;
425
+	}
426
+    
427
+	/**
428
+	 * Get the wrapped element as a general string type.
429
+	 *
430
+	 * @throws \UnexpectedValueException If the element is not general string
431
+	 * @return Primitive\GeneralString
432
+	 */
433
+	public function asGeneralString(): Primitive\GeneralString
434
+	{
435
+		if (!$this->_element instanceof Primitive\GeneralString) {
436
+			throw new \UnexpectedValueException(
437
+				$this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
438
+		}
439
+		return $this->_element;
440
+	}
441
+    
442
+	/**
443
+	 * Get the wrapped element as a universal string type.
444
+	 *
445
+	 * @throws \UnexpectedValueException If the element is not a universal
446
+	 *         string
447
+	 * @return Primitive\UniversalString
448
+	 */
449
+	public function asUniversalString(): Primitive\UniversalString
450
+	{
451
+		if (!$this->_element instanceof Primitive\UniversalString) {
452
+			throw new \UnexpectedValueException(
453
+				$this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
454
+		}
455
+		return $this->_element;
456
+	}
457
+    
458
+	/**
459
+	 * Get the wrapped element as a character string type.
460
+	 *
461
+	 * @throws \UnexpectedValueException If the element is not a character
462
+	 *         string
463
+	 * @return Primitive\CharacterString
464
+	 */
465
+	public function asCharacterString(): Primitive\CharacterString
466
+	{
467
+		if (!$this->_element instanceof Primitive\CharacterString) {
468
+			throw new \UnexpectedValueException(
469
+				$this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
470
+		}
471
+		return $this->_element;
472
+	}
473
+    
474
+	/**
475
+	 * Get the wrapped element as a BMP string type.
476
+	 *
477
+	 * @throws \UnexpectedValueException If the element is not a bmp string
478
+	 * @return Primitive\BMPString
479
+	 */
480
+	public function asBMPString(): Primitive\BMPString
481
+	{
482
+		if (!$this->_element instanceof Primitive\BMPString) {
483
+			throw new \UnexpectedValueException(
484
+				$this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
485
+		}
486
+		return $this->_element;
487
+	}
488
+    
489
+	/**
490
+	 * Get the wrapped element as any string type.
491
+	 *
492
+	 * @throws \UnexpectedValueException If the element is not a string
493
+	 * @return StringType
494
+	 */
495
+	public function asString(): StringType
496
+	{
497
+		if (!$this->_element instanceof StringType) {
498
+			throw new \UnexpectedValueException(
499
+				$this->_generateExceptionMessage(Element::TYPE_STRING));
500
+		}
501
+		return $this->_element;
502
+	}
503
+    
504
+	/**
505
+	 * Get the wrapped element as any time type.
506
+	 *
507
+	 * @throws \UnexpectedValueException If the element is not a time
508
+	 * @return TimeType
509
+	 */
510
+	public function asTime(): TimeType
511
+	{
512
+		if (!$this->_element instanceof TimeType) {
513
+			throw new \UnexpectedValueException(
514
+				$this->_generateExceptionMessage(Element::TYPE_TIME));
515
+		}
516
+		return $this->_element;
517
+	}
518
+    
519
+	/**
520
+	 * Generate message for exceptions thrown by <code>as*</code> methods.
521
+	 *
522
+	 * @param int $tag Type tag of the expected element
523
+	 * @return string
524
+	 */
525
+	private function _generateExceptionMessage(int $tag): string
526
+	{
527
+		return sprintf("%s expected, got %s.", Element::tagToName($tag),
528
+			$this->_typeDescriptorString());
529
+	}
530
+    
531
+	/**
532
+	 * Get textual description of the wrapped element for debugging purposes.
533
+	 *
534
+	 * @return string
535
+	 */
536
+	private function _typeDescriptorString(): string
537
+	{
538
+		$type_cls = $this->_element->typeClass();
539
+		$tag = $this->_element->tag();
540
+		if ($type_cls == Identifier::CLASS_UNIVERSAL) {
541
+			return Element::tagToName($tag);
542
+		}
543
+		return Identifier::classToName($type_cls) . " TAG $tag";
544
+	}
545
+    
546
+	/**
547
+	 *
548
+	 * @see \ASN1\Feature\Encodable::toDER()
549
+	 * @return string
550
+	 */
551
+	public function toDER(): string
552
+	{
553
+		return $this->_element->toDER();
554
+	}
555
+    
556
+	/**
557
+	 *
558
+	 * @see \ASN1\Feature\ElementBase::typeClass()
559
+	 * @return int
560
+	 */
561
+	public function typeClass(): int
562
+	{
563
+		return $this->_element->typeClass();
564
+	}
565
+    
566
+	/**
567
+	 *
568
+	 * @see \ASN1\Feature\ElementBase::isConstructed()
569
+	 * @return bool
570
+	 */
571
+	public function isConstructed(): bool
572
+	{
573
+		return $this->_element->isConstructed();
574
+	}
575
+    
576
+	/**
577
+	 *
578
+	 * @see \ASN1\Feature\ElementBase::tag()
579
+	 * @return int
580
+	 */
581
+	public function tag(): int
582
+	{
583
+		return $this->_element->tag();
584
+	}
585
+    
586
+	/**
587
+	 *
588
+	 * {@inheritdoc}
589
+	 * @see \ASN1\Feature\ElementBase::isType()
590
+	 * @return bool
591
+	 */
592
+	public function isType(int $tag): bool
593
+	{
594
+		return $this->_element->isType($tag);
595
+	}
596
+    
597
+	/**
598
+	 *
599
+	 * @deprecated Use any <code>as*</code> accessor method first to ensure
600
+	 *             type strictness.
601
+	 * @see \ASN1\Feature\ElementBase::expectType()
602
+	 * @return ElementBase
603
+	 */
604
+	public function expectType(int $tag): ElementBase
605
+	{
606
+		return $this->_element->expectType($tag);
607
+	}
608
+    
609
+	/**
610
+	 *
611
+	 * @see \ASN1\Feature\ElementBase::isTagged()
612
+	 * @return bool
613
+	 */
614
+	public function isTagged(): bool
615
+	{
616
+		return $this->_element->isTagged();
617
+	}
618
+    
619
+	/**
620
+	 *
621
+	 * @deprecated Use any <code>as*</code> accessor method first to ensure
622
+	 *             type strictness.
623
+	 * @see \ASN1\Feature\ElementBase::expectTagged()
624
+	 * @return TaggedType
625
+	 */
626
+	public function expectTagged($tag = null): TaggedType
627
+	{
628
+		return $this->_element->expectTagged($tag);
629
+	}
630
+    
631
+	/**
632
+	 *
633
+	 * @see \ASN1\Feature\ElementBase::asElement()
634
+	 * @return Element
635
+	 */
636
+	public function asElement(): Element
637
+	{
638
+		return $this->_element;
639
+	}
640
+    
641
+	/**
642
+	 *
643
+	 * {@inheritdoc}
644
+	 * @return UnspecifiedType
645
+	 */
646
+	public function asUnspecified(): UnspecifiedType
647
+	{
648
+		return $this;
649
+	}
650 650
 }
Please login to merge, or discard this patch.
lib/ASN1/Feature/ElementBase.php 1 patch
Indentation   +74 added lines, -74 removed lines patch added patch discarded remove patch
@@ -13,86 +13,86 @@
 block discarded – undo
13 13
  */
14 14
 interface ElementBase extends Encodable
15 15
 {
16
-    /**
17
-     * Get the class of the ASN.1 type.
18
-     *
19
-     * One of <code>Identifier::CLASS_*</code> constants.
20
-     *
21
-     * @return int
22
-     */
23
-    public function typeClass(): int;
16
+	/**
17
+	 * Get the class of the ASN.1 type.
18
+	 *
19
+	 * One of <code>Identifier::CLASS_*</code> constants.
20
+	 *
21
+	 * @return int
22
+	 */
23
+	public function typeClass(): int;
24 24
     
25
-    /**
26
-     * Check whether the element is constructed.
27
-     *
28
-     * Otherwise it's primitive.
29
-     *
30
-     * @return bool
31
-     */
32
-    public function isConstructed(): bool;
25
+	/**
26
+	 * Check whether the element is constructed.
27
+	 *
28
+	 * Otherwise it's primitive.
29
+	 *
30
+	 * @return bool
31
+	 */
32
+	public function isConstructed(): bool;
33 33
     
34
-    /**
35
-     * Get the tag of the element.
36
-     *
37
-     * Interpretation of the tag depends on the context. For example it may
38
-     * represent a universal type tag or a tag of an implicitly or explicitly
39
-     * tagged type.
40
-     *
41
-     * @return int
42
-     */
43
-    public function tag(): int;
34
+	/**
35
+	 * Get the tag of the element.
36
+	 *
37
+	 * Interpretation of the tag depends on the context. For example it may
38
+	 * represent a universal type tag or a tag of an implicitly or explicitly
39
+	 * tagged type.
40
+	 *
41
+	 * @return int
42
+	 */
43
+	public function tag(): int;
44 44
     
45
-    /**
46
-     * Check whether the element is a type of a given tag.
47
-     *
48
-     * @param int $tag Type tag
49
-     * @return boolean
50
-     */
51
-    public function isType(int $tag): bool;
45
+	/**
46
+	 * Check whether the element is a type of a given tag.
47
+	 *
48
+	 * @param int $tag Type tag
49
+	 * @return boolean
50
+	 */
51
+	public function isType(int $tag): bool;
52 52
     
53
-    /**
54
-     * Check whether the element is a type of a given tag.
55
-     *
56
-     * Throws an exception if expectation fails.
57
-     *
58
-     * @param int $tag Type tag
59
-     * @throws \UnexpectedValueException If the element type differs from the
60
-     *         expected
61
-     * @return ElementBase
62
-     */
63
-    public function expectType(int $tag): ElementBase;
53
+	/**
54
+	 * Check whether the element is a type of a given tag.
55
+	 *
56
+	 * Throws an exception if expectation fails.
57
+	 *
58
+	 * @param int $tag Type tag
59
+	 * @throws \UnexpectedValueException If the element type differs from the
60
+	 *         expected
61
+	 * @return ElementBase
62
+	 */
63
+	public function expectType(int $tag): ElementBase;
64 64
     
65
-    /**
66
-     * Check whether the element is tagged (context specific).
67
-     *
68
-     * @return bool
69
-     */
70
-    public function isTagged(): bool;
65
+	/**
66
+	 * Check whether the element is tagged (context specific).
67
+	 *
68
+	 * @return bool
69
+	 */
70
+	public function isTagged(): bool;
71 71
     
72
-    /**
73
-     * Check whether the element is tagged (context specific) and optionally has
74
-     * a given tag.
75
-     *
76
-     * Throws an exception if the element is not tagged or tag differs from
77
-     * the expected.
78
-     *
79
-     * @param int|null $tag Optional type tag
80
-     * @throws \UnexpectedValueException If expectation fails
81
-     * @return \ASN1\Type\TaggedType
82
-     */
83
-    public function expectTagged($tag = null): TaggedType;
72
+	/**
73
+	 * Check whether the element is tagged (context specific) and optionally has
74
+	 * a given tag.
75
+	 *
76
+	 * Throws an exception if the element is not tagged or tag differs from
77
+	 * the expected.
78
+	 *
79
+	 * @param int|null $tag Optional type tag
80
+	 * @throws \UnexpectedValueException If expectation fails
81
+	 * @return \ASN1\Type\TaggedType
82
+	 */
83
+	public function expectTagged($tag = null): TaggedType;
84 84
     
85
-    /**
86
-     * Get the object as an abstract Element instance.
87
-     *
88
-     * @return \ASN1\Element
89
-     */
90
-    public function asElement(): Element;
85
+	/**
86
+	 * Get the object as an abstract Element instance.
87
+	 *
88
+	 * @return \ASN1\Element
89
+	 */
90
+	public function asElement(): Element;
91 91
     
92
-    /**
93
-     * Get the object as an UnspecifiedType instance.
94
-     *
95
-     * @return UnspecifiedType
96
-     */
97
-    public function asUnspecified(): UnspecifiedType;
92
+	/**
93
+	 * Get the object as an UnspecifiedType instance.
94
+	 *
95
+	 * @return UnspecifiedType
96
+	 */
97
+	public function asUnspecified(): UnspecifiedType;
98 98
 }
Please login to merge, or discard this patch.
lib/ASN1/Element.php 1 patch
Indentation   +408 added lines, -408 removed lines patch added patch discarded remove patch
@@ -20,436 +20,436 @@
 block discarded – undo
20 20
  */
21 21
 abstract class Element implements ElementBase
22 22
 {
23
-    // Universal type tags
24
-    const TYPE_EOC = 0x00;
25
-    const TYPE_BOOLEAN = 0x01;
26
-    const TYPE_INTEGER = 0x02;
27
-    const TYPE_BIT_STRING = 0x03;
28
-    const TYPE_OCTET_STRING = 0x04;
29
-    const TYPE_NULL = 0x05;
30
-    const TYPE_OBJECT_IDENTIFIER = 0x06;
31
-    const TYPE_OBJECT_DESCRIPTOR = 0x07;
32
-    const TYPE_EXTERNAL = 0x08;
33
-    const TYPE_REAL = 0x09;
34
-    const TYPE_ENUMERATED = 0x0a;
35
-    const TYPE_EMBEDDED_PDV = 0x0b;
36
-    const TYPE_UTF8_STRING = 0x0c;
37
-    const TYPE_RELATIVE_OID = 0x0d;
38
-    const TYPE_SEQUENCE = 0x10;
39
-    const TYPE_SET = 0x11;
40
-    const TYPE_NUMERIC_STRING = 0x12;
41
-    const TYPE_PRINTABLE_STRING = 0x13;
42
-    const TYPE_T61_STRING = 0x14;
43
-    const TYPE_VIDEOTEX_STRING = 0x15;
44
-    const TYPE_IA5_STRING = 0x16;
45
-    const TYPE_UTC_TIME = 0x17;
46
-    const TYPE_GENERALIZED_TIME = 0x18;
47
-    const TYPE_GRAPHIC_STRING = 0x19;
48
-    const TYPE_VISIBLE_STRING = 0x1a;
49
-    const TYPE_GENERAL_STRING = 0x1b;
50
-    const TYPE_UNIVERSAL_STRING = 0x1c;
51
-    const TYPE_CHARACTER_STRING = 0x1d;
52
-    const TYPE_BMP_STRING = 0x1e;
23
+	// Universal type tags
24
+	const TYPE_EOC = 0x00;
25
+	const TYPE_BOOLEAN = 0x01;
26
+	const TYPE_INTEGER = 0x02;
27
+	const TYPE_BIT_STRING = 0x03;
28
+	const TYPE_OCTET_STRING = 0x04;
29
+	const TYPE_NULL = 0x05;
30
+	const TYPE_OBJECT_IDENTIFIER = 0x06;
31
+	const TYPE_OBJECT_DESCRIPTOR = 0x07;
32
+	const TYPE_EXTERNAL = 0x08;
33
+	const TYPE_REAL = 0x09;
34
+	const TYPE_ENUMERATED = 0x0a;
35
+	const TYPE_EMBEDDED_PDV = 0x0b;
36
+	const TYPE_UTF8_STRING = 0x0c;
37
+	const TYPE_RELATIVE_OID = 0x0d;
38
+	const TYPE_SEQUENCE = 0x10;
39
+	const TYPE_SET = 0x11;
40
+	const TYPE_NUMERIC_STRING = 0x12;
41
+	const TYPE_PRINTABLE_STRING = 0x13;
42
+	const TYPE_T61_STRING = 0x14;
43
+	const TYPE_VIDEOTEX_STRING = 0x15;
44
+	const TYPE_IA5_STRING = 0x16;
45
+	const TYPE_UTC_TIME = 0x17;
46
+	const TYPE_GENERALIZED_TIME = 0x18;
47
+	const TYPE_GRAPHIC_STRING = 0x19;
48
+	const TYPE_VISIBLE_STRING = 0x1a;
49
+	const TYPE_GENERAL_STRING = 0x1b;
50
+	const TYPE_UNIVERSAL_STRING = 0x1c;
51
+	const TYPE_CHARACTER_STRING = 0x1d;
52
+	const TYPE_BMP_STRING = 0x1e;
53 53
     
54
-    /**
55
-     * Mapping from universal type tag to implementation class name.
56
-     *
57
-     * @internal
58
-     *
59
-     * @var array
60
-     */
61
-    const MAP_TAG_TO_CLASS = [ /* @formatter:off */
62
-        self::TYPE_BOOLEAN => Primitive\Boolean::class,
63
-        self::TYPE_INTEGER => Primitive\Integer::class,
64
-        self::TYPE_BIT_STRING => Primitive\BitString::class,
65
-        self::TYPE_OCTET_STRING => Primitive\OctetString::class,
66
-        self::TYPE_NULL => Primitive\NullType::class,
67
-        self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
68
-        self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
69
-        self::TYPE_REAL => Primitive\Real::class,
70
-        self::TYPE_ENUMERATED => Primitive\Enumerated::class,
71
-        self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
72
-        self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
73
-        self::TYPE_SEQUENCE => Constructed\Sequence::class,
74
-        self::TYPE_SET => Constructed\Set::class,
75
-        self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
76
-        self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
77
-        self::TYPE_T61_STRING => Primitive\T61String::class,
78
-        self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
79
-        self::TYPE_IA5_STRING => Primitive\IA5String::class,
80
-        self::TYPE_UTC_TIME => Primitive\UTCTime::class,
81
-        self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
82
-        self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
83
-        self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
84
-        self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
85
-        self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
86
-        self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
87
-        self::TYPE_BMP_STRING => Primitive\BMPString::class
88
-        /* @formatter:on */
89
-    ];
54
+	/**
55
+	 * Mapping from universal type tag to implementation class name.
56
+	 *
57
+	 * @internal
58
+	 *
59
+	 * @var array
60
+	 */
61
+	const MAP_TAG_TO_CLASS = [ /* @formatter:off */
62
+		self::TYPE_BOOLEAN => Primitive\Boolean::class,
63
+		self::TYPE_INTEGER => Primitive\Integer::class,
64
+		self::TYPE_BIT_STRING => Primitive\BitString::class,
65
+		self::TYPE_OCTET_STRING => Primitive\OctetString::class,
66
+		self::TYPE_NULL => Primitive\NullType::class,
67
+		self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
68
+		self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
69
+		self::TYPE_REAL => Primitive\Real::class,
70
+		self::TYPE_ENUMERATED => Primitive\Enumerated::class,
71
+		self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
72
+		self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
73
+		self::TYPE_SEQUENCE => Constructed\Sequence::class,
74
+		self::TYPE_SET => Constructed\Set::class,
75
+		self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
76
+		self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
77
+		self::TYPE_T61_STRING => Primitive\T61String::class,
78
+		self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
79
+		self::TYPE_IA5_STRING => Primitive\IA5String::class,
80
+		self::TYPE_UTC_TIME => Primitive\UTCTime::class,
81
+		self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
82
+		self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
83
+		self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
84
+		self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
85
+		self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
86
+		self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
87
+		self::TYPE_BMP_STRING => Primitive\BMPString::class
88
+		/* @formatter:on */
89
+	];
90 90
     
91
-    /**
92
-     * Pseudotype for all string types.
93
-     *
94
-     * May be used as an expectation parameter.
95
-     *
96
-     * @var int
97
-     */
98
-    const TYPE_STRING = -1;
91
+	/**
92
+	 * Pseudotype for all string types.
93
+	 *
94
+	 * May be used as an expectation parameter.
95
+	 *
96
+	 * @var int
97
+	 */
98
+	const TYPE_STRING = -1;
99 99
     
100
-    /**
101
-     * Pseudotype for all time types.
102
-     *
103
-     * May be used as an expectation parameter.
104
-     *
105
-     * @var int
106
-     */
107
-    const TYPE_TIME = -2;
100
+	/**
101
+	 * Pseudotype for all time types.
102
+	 *
103
+	 * May be used as an expectation parameter.
104
+	 *
105
+	 * @var int
106
+	 */
107
+	const TYPE_TIME = -2;
108 108
     
109
-    /**
110
-     * Mapping from universal type tag to human readable name.
111
-     *
112
-     * @internal
113
-     *
114
-     * @var array
115
-     */
116
-    const MAP_TYPE_TO_NAME = [ /* @formatter:off */
117
-        self::TYPE_EOC => "EOC",
118
-        self::TYPE_BOOLEAN => "BOOLEAN",
119
-        self::TYPE_INTEGER => "INTEGER",
120
-        self::TYPE_BIT_STRING => "BIT STRING",
121
-        self::TYPE_OCTET_STRING => "OCTET STRING",
122
-        self::TYPE_NULL => "NULL",
123
-        self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER",
124
-        self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor",
125
-        self::TYPE_EXTERNAL => "EXTERNAL",
126
-        self::TYPE_REAL => "REAL",
127
-        self::TYPE_ENUMERATED => "ENUMERATED",
128
-        self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV",
129
-        self::TYPE_UTF8_STRING => "UTF8String",
130
-        self::TYPE_RELATIVE_OID => "RELATIVE-OID",
131
-        self::TYPE_SEQUENCE => "SEQUENCE",
132
-        self::TYPE_SET => "SET",
133
-        self::TYPE_NUMERIC_STRING => "NumericString",
134
-        self::TYPE_PRINTABLE_STRING => "PrintableString",
135
-        self::TYPE_T61_STRING => "T61String",
136
-        self::TYPE_VIDEOTEX_STRING => "VideotexString",
137
-        self::TYPE_IA5_STRING => "IA5String",
138
-        self::TYPE_UTC_TIME => "UTCTime",
139
-        self::TYPE_GENERALIZED_TIME => "GeneralizedTime",
140
-        self::TYPE_GRAPHIC_STRING => "GraphicString",
141
-        self::TYPE_VISIBLE_STRING => "VisibleString",
142
-        self::TYPE_GENERAL_STRING => "GeneralString",
143
-        self::TYPE_UNIVERSAL_STRING => "UniversalString",
144
-        self::TYPE_CHARACTER_STRING => "CHARACTER STRING",
145
-        self::TYPE_BMP_STRING => "BMPString",
146
-        self::TYPE_STRING => "Any String",
147
-        self::TYPE_TIME => "Any Time"
148
-        /* @formatter:on */
149
-    ];
109
+	/**
110
+	 * Mapping from universal type tag to human readable name.
111
+	 *
112
+	 * @internal
113
+	 *
114
+	 * @var array
115
+	 */
116
+	const MAP_TYPE_TO_NAME = [ /* @formatter:off */
117
+		self::TYPE_EOC => "EOC",
118
+		self::TYPE_BOOLEAN => "BOOLEAN",
119
+		self::TYPE_INTEGER => "INTEGER",
120
+		self::TYPE_BIT_STRING => "BIT STRING",
121
+		self::TYPE_OCTET_STRING => "OCTET STRING",
122
+		self::TYPE_NULL => "NULL",
123
+		self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER",
124
+		self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor",
125
+		self::TYPE_EXTERNAL => "EXTERNAL",
126
+		self::TYPE_REAL => "REAL",
127
+		self::TYPE_ENUMERATED => "ENUMERATED",
128
+		self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV",
129
+		self::TYPE_UTF8_STRING => "UTF8String",
130
+		self::TYPE_RELATIVE_OID => "RELATIVE-OID",
131
+		self::TYPE_SEQUENCE => "SEQUENCE",
132
+		self::TYPE_SET => "SET",
133
+		self::TYPE_NUMERIC_STRING => "NumericString",
134
+		self::TYPE_PRINTABLE_STRING => "PrintableString",
135
+		self::TYPE_T61_STRING => "T61String",
136
+		self::TYPE_VIDEOTEX_STRING => "VideotexString",
137
+		self::TYPE_IA5_STRING => "IA5String",
138
+		self::TYPE_UTC_TIME => "UTCTime",
139
+		self::TYPE_GENERALIZED_TIME => "GeneralizedTime",
140
+		self::TYPE_GRAPHIC_STRING => "GraphicString",
141
+		self::TYPE_VISIBLE_STRING => "VisibleString",
142
+		self::TYPE_GENERAL_STRING => "GeneralString",
143
+		self::TYPE_UNIVERSAL_STRING => "UniversalString",
144
+		self::TYPE_CHARACTER_STRING => "CHARACTER STRING",
145
+		self::TYPE_BMP_STRING => "BMPString",
146
+		self::TYPE_STRING => "Any String",
147
+		self::TYPE_TIME => "Any Time"
148
+		/* @formatter:on */
149
+	];
150 150
     
151
-    /**
152
-     * Element's type tag.
153
-     *
154
-     * @var int
155
-     */
156
-    protected $_typeTag;
151
+	/**
152
+	 * Element's type tag.
153
+	 *
154
+	 * @var int
155
+	 */
156
+	protected $_typeTag;
157 157
     
158
-    /**
159
-     *
160
-     * @see \ASN1\Feature\ElementBase::typeClass()
161
-     * @return int
162
-     */
163
-    abstract public function typeClass(): int;
158
+	/**
159
+	 *
160
+	 * @see \ASN1\Feature\ElementBase::typeClass()
161
+	 * @return int
162
+	 */
163
+	abstract public function typeClass(): int;
164 164
     
165
-    /**
166
-     *
167
-     * @see \ASN1\Feature\ElementBase::isConstructed()
168
-     * @return bool
169
-     */
170
-    abstract public function isConstructed(): bool;
165
+	/**
166
+	 *
167
+	 * @see \ASN1\Feature\ElementBase::isConstructed()
168
+	 * @return bool
169
+	 */
170
+	abstract public function isConstructed(): bool;
171 171
     
172
-    /**
173
-     * Get the content encoded in DER.
174
-     *
175
-     * Returns the DER encoded content without identifier and length header
176
-     * octets.
177
-     *
178
-     * @return string
179
-     */
180
-    abstract protected function _encodedContentDER(): string;
172
+	/**
173
+	 * Get the content encoded in DER.
174
+	 *
175
+	 * Returns the DER encoded content without identifier and length header
176
+	 * octets.
177
+	 *
178
+	 * @return string
179
+	 */
180
+	abstract protected function _encodedContentDER(): string;
181 181
     
182
-    /**
183
-     * Decode type-specific element from DER.
184
-     *
185
-     * @param Identifier $identifier Pre-parsed identifier
186
-     * @param string $data DER data
187
-     * @param int $offset Offset in data to the next byte after identifier
188
-     * @throws DecodeException If decoding fails
189
-     * @return self
190
-     */
191
-    protected static function _decodeFromDER(Identifier $identifier, string $data,
192
-        int &$offset): ElementBase
193
-    {
194
-        throw new \BadMethodCallException(
195
-            __METHOD__ . " must be implemented in derived class.");
196
-    }
182
+	/**
183
+	 * Decode type-specific element from DER.
184
+	 *
185
+	 * @param Identifier $identifier Pre-parsed identifier
186
+	 * @param string $data DER data
187
+	 * @param int $offset Offset in data to the next byte after identifier
188
+	 * @throws DecodeException If decoding fails
189
+	 * @return self
190
+	 */
191
+	protected static function _decodeFromDER(Identifier $identifier, string $data,
192
+		int &$offset): ElementBase
193
+	{
194
+		throw new \BadMethodCallException(
195
+			__METHOD__ . " must be implemented in derived class.");
196
+	}
197 197
     
198
-    /**
199
-     * Decode element from DER data.
200
-     *
201
-     * @param string $data DER encoded data
202
-     * @param int|null $offset Reference to the variable that contains offset
203
-     *        into the data where to start parsing. Variable is updated to
204
-     *        the offset next to the parsed element. If null, start from offset
205
-     *        0.
206
-     * @throws DecodeException If decoding fails
207
-     * @throws \UnexpectedValueException If called in the context of an expected
208
-     *         type, but decoding yields another type
209
-     * @return ElementBase
210
-     */
211
-    public static function fromDER(string $data, int &$offset = null): ElementBase
212
-    {
213
-        // decode identifier
214
-        $idx = $offset ? $offset : 0;
215
-        $identifier = Identifier::fromDER($data, $idx);
216
-        // determine class that implements type specific decoding
217
-        $cls = self::_determineImplClass($identifier);
218
-        try {
219
-            // decode remaining element
220
-            $element = $cls::_decodeFromDER($identifier, $data, $idx);
221
-        } catch (\LogicException $e) {
222
-            // rethrow as a RuntimeException for unified exception handling
223
-            throw new DecodeException(
224
-                sprintf("Error while decoding %s.",
225
-                    self::tagToName($identifier->intTag())), 0, $e);
226
-        }
227
-        // if called in the context of a concrete class, check
228
-        // that decoded type matches the type of a calling class
229
-        $called_class = get_called_class();
230
-        if (self::class != $called_class) {
231
-            if (!$element instanceof $called_class) {
232
-                throw new \UnexpectedValueException(
233
-                    sprintf("%s expected, got %s.", $called_class,
234
-                        get_class($element)));
235
-            }
236
-        }
237
-        // update offset for the caller
238
-        if (isset($offset)) {
239
-            $offset = $idx;
240
-        }
241
-        return $element;
242
-    }
198
+	/**
199
+	 * Decode element from DER data.
200
+	 *
201
+	 * @param string $data DER encoded data
202
+	 * @param int|null $offset Reference to the variable that contains offset
203
+	 *        into the data where to start parsing. Variable is updated to
204
+	 *        the offset next to the parsed element. If null, start from offset
205
+	 *        0.
206
+	 * @throws DecodeException If decoding fails
207
+	 * @throws \UnexpectedValueException If called in the context of an expected
208
+	 *         type, but decoding yields another type
209
+	 * @return ElementBase
210
+	 */
211
+	public static function fromDER(string $data, int &$offset = null): ElementBase
212
+	{
213
+		// decode identifier
214
+		$idx = $offset ? $offset : 0;
215
+		$identifier = Identifier::fromDER($data, $idx);
216
+		// determine class that implements type specific decoding
217
+		$cls = self::_determineImplClass($identifier);
218
+		try {
219
+			// decode remaining element
220
+			$element = $cls::_decodeFromDER($identifier, $data, $idx);
221
+		} catch (\LogicException $e) {
222
+			// rethrow as a RuntimeException for unified exception handling
223
+			throw new DecodeException(
224
+				sprintf("Error while decoding %s.",
225
+					self::tagToName($identifier->intTag())), 0, $e);
226
+		}
227
+		// if called in the context of a concrete class, check
228
+		// that decoded type matches the type of a calling class
229
+		$called_class = get_called_class();
230
+		if (self::class != $called_class) {
231
+			if (!$element instanceof $called_class) {
232
+				throw new \UnexpectedValueException(
233
+					sprintf("%s expected, got %s.", $called_class,
234
+						get_class($element)));
235
+			}
236
+		}
237
+		// update offset for the caller
238
+		if (isset($offset)) {
239
+			$offset = $idx;
240
+		}
241
+		return $element;
242
+	}
243 243
     
244
-    /**
245
-     *
246
-     * @see \ASN1\Feature\Encodable::toDER()
247
-     * @return string
248
-     */
249
-    public function toDER(): string
250
-    {
251
-        $identifier = new Identifier($this->typeClass(),
252
-            $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
253
-            $this->_typeTag);
254
-        $content = $this->_encodedContentDER();
255
-        $length = new Length(strlen($content));
256
-        return $identifier->toDER() . $length->toDER() . $content;
257
-    }
244
+	/**
245
+	 *
246
+	 * @see \ASN1\Feature\Encodable::toDER()
247
+	 * @return string
248
+	 */
249
+	public function toDER(): string
250
+	{
251
+		$identifier = new Identifier($this->typeClass(),
252
+			$this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
253
+			$this->_typeTag);
254
+		$content = $this->_encodedContentDER();
255
+		$length = new Length(strlen($content));
256
+		return $identifier->toDER() . $length->toDER() . $content;
257
+	}
258 258
     
259
-    /**
260
-     *
261
-     * @see \ASN1\Feature\ElementBase::tag()
262
-     * @return int
263
-     */
264
-    public function tag(): int
265
-    {
266
-        return $this->_typeTag;
267
-    }
259
+	/**
260
+	 *
261
+	 * @see \ASN1\Feature\ElementBase::tag()
262
+	 * @return int
263
+	 */
264
+	public function tag(): int
265
+	{
266
+		return $this->_typeTag;
267
+	}
268 268
     
269
-    /**
270
-     *
271
-     * @see \ASN1\Feature\ElementBase::isType()
272
-     * @return bool
273
-     */
274
-    public function isType(int $tag): bool
275
-    {
276
-        // if element is context specific
277
-        if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) {
278
-            return false;
279
-        }
280
-        // negative tags identify an abstract pseudotype
281
-        if ($tag < 0) {
282
-            return $this->_isPseudoType($tag);
283
-        }
284
-        return $this->_isConcreteType($tag);
285
-    }
269
+	/**
270
+	 *
271
+	 * @see \ASN1\Feature\ElementBase::isType()
272
+	 * @return bool
273
+	 */
274
+	public function isType(int $tag): bool
275
+	{
276
+		// if element is context specific
277
+		if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) {
278
+			return false;
279
+		}
280
+		// negative tags identify an abstract pseudotype
281
+		if ($tag < 0) {
282
+			return $this->_isPseudoType($tag);
283
+		}
284
+		return $this->_isConcreteType($tag);
285
+	}
286 286
     
287
-    /**
288
-     *
289
-     * @see \ASN1\Feature\ElementBase::expectType()
290
-     * @return ElementBase
291
-     */
292
-    public function expectType(int $tag): ElementBase
293
-    {
294
-        if (!$this->isType($tag)) {
295
-            throw new \UnexpectedValueException(
296
-                sprintf("%s expected, got %s.", self::tagToName($tag),
297
-                    $this->_typeDescriptorString()));
298
-        }
299
-        return $this;
300
-    }
287
+	/**
288
+	 *
289
+	 * @see \ASN1\Feature\ElementBase::expectType()
290
+	 * @return ElementBase
291
+	 */
292
+	public function expectType(int $tag): ElementBase
293
+	{
294
+		if (!$this->isType($tag)) {
295
+			throw new \UnexpectedValueException(
296
+				sprintf("%s expected, got %s.", self::tagToName($tag),
297
+					$this->_typeDescriptorString()));
298
+		}
299
+		return $this;
300
+	}
301 301
     
302
-    /**
303
-     * Check whether the element is a concrete type of a given tag.
304
-     *
305
-     * @param int $tag
306
-     * @return bool
307
-     */
308
-    private function _isConcreteType(int $tag): bool
309
-    {
310
-        // if tag doesn't match
311
-        if ($this->tag() != $tag) {
312
-            return false;
313
-        }
314
-        // if type is universal check that instance is of a correct class
315
-        if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
316
-            $cls = self::_determineUniversalImplClass($tag);
317
-            if (!$this instanceof $cls) {
318
-                return false;
319
-            }
320
-        }
321
-        return true;
322
-    }
302
+	/**
303
+	 * Check whether the element is a concrete type of a given tag.
304
+	 *
305
+	 * @param int $tag
306
+	 * @return bool
307
+	 */
308
+	private function _isConcreteType(int $tag): bool
309
+	{
310
+		// if tag doesn't match
311
+		if ($this->tag() != $tag) {
312
+			return false;
313
+		}
314
+		// if type is universal check that instance is of a correct class
315
+		if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
316
+			$cls = self::_determineUniversalImplClass($tag);
317
+			if (!$this instanceof $cls) {
318
+				return false;
319
+			}
320
+		}
321
+		return true;
322
+	}
323 323
     
324
-    /**
325
-     * Check whether the element is a pseudotype.
326
-     *
327
-     * @param int $tag
328
-     * @return bool
329
-     */
330
-    private function _isPseudoType(int $tag): bool
331
-    {
332
-        switch ($tag) {
333
-            case self::TYPE_STRING:
334
-                return $this instanceof StringType;
335
-            case self::TYPE_TIME:
336
-                return $this instanceof TimeType;
337
-        }
338
-        return false;
339
-    }
324
+	/**
325
+	 * Check whether the element is a pseudotype.
326
+	 *
327
+	 * @param int $tag
328
+	 * @return bool
329
+	 */
330
+	private function _isPseudoType(int $tag): bool
331
+	{
332
+		switch ($tag) {
333
+			case self::TYPE_STRING:
334
+				return $this instanceof StringType;
335
+			case self::TYPE_TIME:
336
+				return $this instanceof TimeType;
337
+		}
338
+		return false;
339
+	}
340 340
     
341
-    /**
342
-     *
343
-     * @see \ASN1\Feature\ElementBase::isTagged()
344
-     * @return bool
345
-     */
346
-    public function isTagged(): bool
347
-    {
348
-        return $this instanceof TaggedType;
349
-    }
341
+	/**
342
+	 *
343
+	 * @see \ASN1\Feature\ElementBase::isTagged()
344
+	 * @return bool
345
+	 */
346
+	public function isTagged(): bool
347
+	{
348
+		return $this instanceof TaggedType;
349
+	}
350 350
     
351
-    /**
352
-     *
353
-     * @see \ASN1\Feature\ElementBase::expectTagged()
354
-     * @return TaggedType
355
-     */
356
-    public function expectTagged($tag = null): TaggedType
357
-    {
358
-        if (!$this->isTagged()) {
359
-            throw new \UnexpectedValueException(
360
-                sprintf("Context specific element expected, got %s.",
361
-                    Identifier::classToName($this->typeClass())));
362
-        }
363
-        if (isset($tag) && $this->tag() != $tag) {
364
-            throw new \UnexpectedValueException(
365
-                sprintf("Tag %d expected, got %d.", $tag, $this->tag()));
366
-        }
367
-        return $this;
368
-    }
351
+	/**
352
+	 *
353
+	 * @see \ASN1\Feature\ElementBase::expectTagged()
354
+	 * @return TaggedType
355
+	 */
356
+	public function expectTagged($tag = null): TaggedType
357
+	{
358
+		if (!$this->isTagged()) {
359
+			throw new \UnexpectedValueException(
360
+				sprintf("Context specific element expected, got %s.",
361
+					Identifier::classToName($this->typeClass())));
362
+		}
363
+		if (isset($tag) && $this->tag() != $tag) {
364
+			throw new \UnexpectedValueException(
365
+				sprintf("Tag %d expected, got %d.", $tag, $this->tag()));
366
+		}
367
+		return $this;
368
+	}
369 369
     
370
-    /**
371
-     *
372
-     * @see \ASN1\Feature\ElementBase::asElement()
373
-     * @return Element
374
-     */
375
-    final public function asElement(): Element
376
-    {
377
-        return $this;
378
-    }
370
+	/**
371
+	 *
372
+	 * @see \ASN1\Feature\ElementBase::asElement()
373
+	 * @return Element
374
+	 */
375
+	final public function asElement(): Element
376
+	{
377
+		return $this;
378
+	}
379 379
     
380
-    /**
381
-     * Get element decorated with UnspecifiedType object.
382
-     *
383
-     * @return UnspecifiedType
384
-     */
385
-    public function asUnspecified(): UnspecifiedType
386
-    {
387
-        return new UnspecifiedType($this);
388
-    }
380
+	/**
381
+	 * Get element decorated with UnspecifiedType object.
382
+	 *
383
+	 * @return UnspecifiedType
384
+	 */
385
+	public function asUnspecified(): UnspecifiedType
386
+	{
387
+		return new UnspecifiedType($this);
388
+	}
389 389
     
390
-    /**
391
-     * Determine the class that implements the type.
392
-     *
393
-     * @param Identifier $identifier
394
-     * @return string Class name
395
-     */
396
-    protected static function _determineImplClass(Identifier $identifier): string
397
-    {
398
-        // tagged type
399
-        if ($identifier->isContextSpecific()) {
400
-            return TaggedType::class;
401
-        }
402
-        // universal class
403
-        if ($identifier->isUniversal()) {
404
-            return self::_determineUniversalImplClass($identifier->intTag());
405
-        }
406
-        throw new \UnexpectedValueException(
407
-            sprintf("%s %d not implemented.",
408
-                Identifier::classToName($identifier->typeClass()),
409
-                $identifier->tag()));
410
-    }
390
+	/**
391
+	 * Determine the class that implements the type.
392
+	 *
393
+	 * @param Identifier $identifier
394
+	 * @return string Class name
395
+	 */
396
+	protected static function _determineImplClass(Identifier $identifier): string
397
+	{
398
+		// tagged type
399
+		if ($identifier->isContextSpecific()) {
400
+			return TaggedType::class;
401
+		}
402
+		// universal class
403
+		if ($identifier->isUniversal()) {
404
+			return self::_determineUniversalImplClass($identifier->intTag());
405
+		}
406
+		throw new \UnexpectedValueException(
407
+			sprintf("%s %d not implemented.",
408
+				Identifier::classToName($identifier->typeClass()),
409
+				$identifier->tag()));
410
+	}
411 411
     
412
-    /**
413
-     * Determine the class that implements an universal type of the given tag.
414
-     *
415
-     * @param int $tag
416
-     * @throws \UnexpectedValueException
417
-     * @return string Class name
418
-     */
419
-    protected static function _determineUniversalImplClass(int $tag): string
420
-    {
421
-        if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
422
-            throw new \UnexpectedValueException(
423
-                "Universal tag $tag not implemented.");
424
-        }
425
-        return self::MAP_TAG_TO_CLASS[$tag];
426
-    }
412
+	/**
413
+	 * Determine the class that implements an universal type of the given tag.
414
+	 *
415
+	 * @param int $tag
416
+	 * @throws \UnexpectedValueException
417
+	 * @return string Class name
418
+	 */
419
+	protected static function _determineUniversalImplClass(int $tag): string
420
+	{
421
+		if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
422
+			throw new \UnexpectedValueException(
423
+				"Universal tag $tag not implemented.");
424
+		}
425
+		return self::MAP_TAG_TO_CLASS[$tag];
426
+	}
427 427
     
428
-    /**
429
-     * Get textual description of the type for debugging purposes.
430
-     *
431
-     * @return string
432
-     */
433
-    protected function _typeDescriptorString(): string
434
-    {
435
-        if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
436
-            return self::tagToName($this->_typeTag);
437
-        }
438
-        return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()),
439
-            $this->_typeTag);
440
-    }
428
+	/**
429
+	 * Get textual description of the type for debugging purposes.
430
+	 *
431
+	 * @return string
432
+	 */
433
+	protected function _typeDescriptorString(): string
434
+	{
435
+		if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
436
+			return self::tagToName($this->_typeTag);
437
+		}
438
+		return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()),
439
+			$this->_typeTag);
440
+	}
441 441
     
442
-    /**
443
-     * Get human readable name for an universal tag.
444
-     *
445
-     * @param int $tag
446
-     * @return string
447
-     */
448
-    public static function tagToName(int $tag): string
449
-    {
450
-        if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
451
-            return "TAG $tag";
452
-        }
453
-        return self::MAP_TYPE_TO_NAME[$tag];
454
-    }
442
+	/**
443
+	 * Get human readable name for an universal tag.
444
+	 *
445
+	 * @param int $tag
446
+	 * @return string
447
+	 */
448
+	public static function tagToName(int $tag): string
449
+	{
450
+		if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
451
+			return "TAG $tag";
452
+		}
453
+		return self::MAP_TYPE_TO_NAME[$tag];
454
+	}
455 455
 }
Please login to merge, or discard this patch.