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