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.
Passed
Push — master ( 662af5...44febf )
by Joni
02:17
created
lib/ASN1/Type/Primitive/Enumerated.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -7,14 +7,14 @@
 block discarded – undo
7 7
  */
8 8
 class Enumerated extends Integer
9 9
 {
10
-    /**
11
-     * Constructor.
12
-     *
13
-     * @param int|string $number
14
-     */
15
-    public function __construct($number)
16
-    {
17
-        parent::__construct($number);
18
-        $this->_typeTag = self::TYPE_ENUMERATED;
19
-    }
10
+	/**
11
+	 * Constructor.
12
+	 *
13
+	 * @param int|string $number
14
+	 */
15
+	public function __construct($number)
16
+	{
17
+		parent::__construct($number);
18
+		$this->_typeTag = self::TYPE_ENUMERATED;
19
+	}
20 20
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type\Primitive;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Feature/Encodable.php 2 patches
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -9,10 +9,10 @@
 block discarded – undo
9 9
  */
10 10
 interface Encodable
11 11
 {
12
-    /**
13
-     * Encode object to DER.
14
-     *
15
-     * @return string
16
-     */
17
-    public function toDER(): string;
12
+	/**
13
+	 * Encode object to DER.
14
+	 *
15
+	 * @return string
16
+	 */
17
+	public function toDER(): string;
18 18
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Feature;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/OctetString.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -12,16 +12,16 @@
 block discarded – undo
12 12
  */
13 13
 class OctetString extends PrimitiveString
14 14
 {
15
-    use UniversalClass;
15
+	use UniversalClass;
16 16
     
17
-    /**
18
-     * Constructor.
19
-     *
20
-     * @param string $string
21
-     */
22
-    public function __construct(string $string)
23
-    {
24
-        $this->_typeTag = self::TYPE_OCTET_STRING;
25
-        parent::__construct($string);
26
-    }
17
+	/**
18
+	 * Constructor.
19
+	 *
20
+	 * @param string $string
21
+	 */
22
+	public function __construct(string $string)
23
+	{
24
+		$this->_typeTag = self::TYPE_OCTET_STRING;
25
+		parent::__construct($string);
26
+	}
27 27
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type\Primitive;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/ObjectDescriptor.php 2 patches
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -12,26 +12,26 @@
 block discarded – undo
12 12
  */
13 13
 class ObjectDescriptor extends PrimitiveString
14 14
 {
15
-    use UniversalClass;
15
+	use UniversalClass;
16 16
     
17
-    /**
18
-     * Constructor.
19
-     *
20
-     * @param string $descriptor
21
-     */
22
-    public function __construct(string $descriptor)
23
-    {
24
-        $this->_string = $descriptor;
25
-        $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR;
26
-    }
17
+	/**
18
+	 * Constructor.
19
+	 *
20
+	 * @param string $descriptor
21
+	 */
22
+	public function __construct(string $descriptor)
23
+	{
24
+		$this->_string = $descriptor;
25
+		$this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR;
26
+	}
27 27
     
28
-    /**
29
-     * Get the object descriptor.
30
-     *
31
-     * @return string
32
-     */
33
-    public function descriptor(): string
34
-    {
35
-        return $this->_string;
36
-    }
28
+	/**
29
+	 * Get the object descriptor.
30
+	 *
31
+	 * @return string
32
+	 */
33
+	public function descriptor(): string
34
+	{
35
+		return $this->_string;
36
+	}
37 37
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type\Primitive;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/Integer.php 3 patches
Switch Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -71,11 +71,11 @@
 block discarded – undo
71 71
         $num = $this->_number->gmpObj();
72 72
         switch (gmp_sign($num)) {
73 73
             // positive
74
-            case 1:
75
-                return self::_encodePositiveInteger($num);
76
-            // negative
77
-            case -1:
78
-                return self::_encodeNegativeInteger($num);
74
+        case 1:
75
+            return self::_encodePositiveInteger($num);
76
+        // negative
77
+        case -1:
78
+            return self::_encodeNegativeInteger($num);
79 79
         }
80 80
         // zero
81 81
         return "\0";
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type\Primitive;
6 6
 
Please login to merge, or discard this patch.
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -17,161 +17,161 @@
 block discarded – undo
17 17
  */
18 18
 class Integer extends Element
19 19
 {
20
-    use UniversalClass;
21
-    use PrimitiveType;
20
+	use UniversalClass;
21
+	use PrimitiveType;
22 22
 
23
-    /**
24
-     * The number.
25
-     *
26
-     * @var BigInt
27
-     */
28
-    private $_number;
23
+	/**
24
+	 * The number.
25
+	 *
26
+	 * @var BigInt
27
+	 */
28
+	private $_number;
29 29
 
30
-    /**
31
-     * Constructor.
32
-     *
33
-     * @param int|string $number Base 10 integer
34
-     */
35
-    public function __construct($number)
36
-    {
37
-        $this->_typeTag = self::TYPE_INTEGER;
38
-        if (!self::_validateNumber($number)) {
39
-            $var = is_scalar($number) ? strval($number) : gettype($number);
40
-            throw new \InvalidArgumentException("'{$var}' is not a valid number.");
41
-        }
42
-        $this->_number = new BigInt($number);
43
-    }
30
+	/**
31
+	 * Constructor.
32
+	 *
33
+	 * @param int|string $number Base 10 integer
34
+	 */
35
+	public function __construct($number)
36
+	{
37
+		$this->_typeTag = self::TYPE_INTEGER;
38
+		if (!self::_validateNumber($number)) {
39
+			$var = is_scalar($number) ? strval($number) : gettype($number);
40
+			throw new \InvalidArgumentException("'{$var}' is not a valid number.");
41
+		}
42
+		$this->_number = new BigInt($number);
43
+	}
44 44
 
45
-    /**
46
-     * Get the number as a base 10.
47
-     *
48
-     * @return string Integer as a string
49
-     */
50
-    public function number(): string
51
-    {
52
-        return $this->_number->base10();
53
-    }
45
+	/**
46
+	 * Get the number as a base 10.
47
+	 *
48
+	 * @return string Integer as a string
49
+	 */
50
+	public function number(): string
51
+	{
52
+		return $this->_number->base10();
53
+	}
54 54
 
55
-    /**
56
-     * Get the number as an integer type.
57
-     *
58
-     * @return int
59
-     */
60
-    public function intNumber(): int
61
-    {
62
-        return $this->_number->intVal();
63
-    }
55
+	/**
56
+	 * Get the number as an integer type.
57
+	 *
58
+	 * @return int
59
+	 */
60
+	public function intNumber(): int
61
+	{
62
+		return $this->_number->intVal();
63
+	}
64 64
 
65
-    /**
66
-     * {@inheritdoc}
67
-     */
68
-    protected function _encodedContentDER(): string
69
-    {
70
-        $num = $this->_number->gmpObj();
71
-        switch (gmp_sign($num)) {
72
-            // positive
73
-            case 1:
74
-                return self::_encodePositiveInteger($num);
75
-            // negative
76
-            case -1:
77
-                return self::_encodeNegativeInteger($num);
78
-        }
79
-        // zero
80
-        return "\0";
81
-    }
65
+	/**
66
+	 * {@inheritdoc}
67
+	 */
68
+	protected function _encodedContentDER(): string
69
+	{
70
+		$num = $this->_number->gmpObj();
71
+		switch (gmp_sign($num)) {
72
+			// positive
73
+			case 1:
74
+				return self::_encodePositiveInteger($num);
75
+			// negative
76
+			case -1:
77
+				return self::_encodeNegativeInteger($num);
78
+		}
79
+		// zero
80
+		return "\0";
81
+	}
82 82
 
83
-    /**
84
-     * {@inheritdoc}
85
-     */
86
-    protected static function _decodeFromDER(Identifier $identifier,
87
-        string $data, int &$offset): ElementBase
88
-    {
89
-        $idx = $offset;
90
-        $length = Length::expectFromDER($data, $idx)->intLength();
91
-        $bytes = substr($data, $idx, $length);
92
-        $idx += $length;
93
-        $neg = ord($bytes[0]) & 0x80;
94
-        // negative, apply inversion of two's complement
95
-        if ($neg) {
96
-            $len = strlen($bytes);
97
-            for ($i = 0; $i < $len; ++$i) {
98
-                $bytes[$i] = ~$bytes[$i];
99
-            }
100
-        }
101
-        $num = gmp_init(bin2hex($bytes), 16);
102
-        // negative, apply addition of two's complement
103
-        // and produce negative result
104
-        if ($neg) {
105
-            $num = gmp_neg($num + 1);
106
-        }
107
-        $offset = $idx;
108
-        // late static binding since enumerated extends integer type
109
-        return new static(gmp_strval($num, 10));
110
-    }
83
+	/**
84
+	 * {@inheritdoc}
85
+	 */
86
+	protected static function _decodeFromDER(Identifier $identifier,
87
+		string $data, int &$offset): ElementBase
88
+	{
89
+		$idx = $offset;
90
+		$length = Length::expectFromDER($data, $idx)->intLength();
91
+		$bytes = substr($data, $idx, $length);
92
+		$idx += $length;
93
+		$neg = ord($bytes[0]) & 0x80;
94
+		// negative, apply inversion of two's complement
95
+		if ($neg) {
96
+			$len = strlen($bytes);
97
+			for ($i = 0; $i < $len; ++$i) {
98
+				$bytes[$i] = ~$bytes[$i];
99
+			}
100
+		}
101
+		$num = gmp_init(bin2hex($bytes), 16);
102
+		// negative, apply addition of two's complement
103
+		// and produce negative result
104
+		if ($neg) {
105
+			$num = gmp_neg($num + 1);
106
+		}
107
+		$offset = $idx;
108
+		// late static binding since enumerated extends integer type
109
+		return new static(gmp_strval($num, 10));
110
+	}
111 111
 
112
-    /**
113
-     * Encode positive integer to DER content.
114
-     *
115
-     * @param \GMP $num
116
-     *
117
-     * @return string
118
-     */
119
-    private static function _encodePositiveInteger(\GMP $num): string
120
-    {
121
-        $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
122
-        // if first bit is 1, prepend full zero byte
123
-        // to represent positive two's complement
124
-        if (ord($bin[0]) & 0x80) {
125
-            $bin = chr(0x00) . $bin;
126
-        }
127
-        return $bin;
128
-    }
112
+	/**
113
+	 * Encode positive integer to DER content.
114
+	 *
115
+	 * @param \GMP $num
116
+	 *
117
+	 * @return string
118
+	 */
119
+	private static function _encodePositiveInteger(\GMP $num): string
120
+	{
121
+		$bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
122
+		// if first bit is 1, prepend full zero byte
123
+		// to represent positive two's complement
124
+		if (ord($bin[0]) & 0x80) {
125
+			$bin = chr(0x00) . $bin;
126
+		}
127
+		return $bin;
128
+	}
129 129
 
130
-    /**
131
-     * Encode negative integer to DER content.
132
-     *
133
-     * @param \GMP $num
134
-     *
135
-     * @return string
136
-     */
137
-    private static function _encodeNegativeInteger(\GMP $num): string
138
-    {
139
-        $num = gmp_abs($num);
140
-        // compute number of bytes required
141
-        $width = 1;
142
-        if ($num > 128) {
143
-            $tmp = $num;
144
-            do {
145
-                ++$width;
146
-                $tmp >>= 8;
147
-            } while ($tmp > 128);
148
-        }
149
-        // compute two's complement 2^n - x
150
-        $num = gmp_pow('2', 8 * $width) - $num;
151
-        $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
152
-        // if first bit is 0, prepend full inverted byte
153
-        // to represent negative two's complement
154
-        if (!(ord($bin[0]) & 0x80)) {
155
-            $bin = chr(0xff) . $bin;
156
-        }
157
-        return $bin;
158
-    }
130
+	/**
131
+	 * Encode negative integer to DER content.
132
+	 *
133
+	 * @param \GMP $num
134
+	 *
135
+	 * @return string
136
+	 */
137
+	private static function _encodeNegativeInteger(\GMP $num): string
138
+	{
139
+		$num = gmp_abs($num);
140
+		// compute number of bytes required
141
+		$width = 1;
142
+		if ($num > 128) {
143
+			$tmp = $num;
144
+			do {
145
+				++$width;
146
+				$tmp >>= 8;
147
+			} while ($tmp > 128);
148
+		}
149
+		// compute two's complement 2^n - x
150
+		$num = gmp_pow('2', 8 * $width) - $num;
151
+		$bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
152
+		// if first bit is 0, prepend full inverted byte
153
+		// to represent negative two's complement
154
+		if (!(ord($bin[0]) & 0x80)) {
155
+			$bin = chr(0xff) . $bin;
156
+		}
157
+		return $bin;
158
+	}
159 159
 
160
-    /**
161
-     * Test that number is valid for this context.
162
-     *
163
-     * @param mixed $num
164
-     *
165
-     * @return bool
166
-     */
167
-    private static function _validateNumber($num): bool
168
-    {
169
-        if (is_int($num)) {
170
-            return true;
171
-        }
172
-        if (is_string($num) && preg_match('/-?\d+/', $num)) {
173
-            return true;
174
-        }
175
-        return false;
176
-    }
160
+	/**
161
+	 * Test that number is valid for this context.
162
+	 *
163
+	 * @param mixed $num
164
+	 *
165
+	 * @return bool
166
+	 */
167
+	private static function _validateNumber($num): bool
168
+	{
169
+		if (is_int($num)) {
170
+			return true;
171
+		}
172
+		if (is_string($num) && preg_match('/-?\d+/', $num)) {
173
+			return true;
174
+		}
175
+		return false;
176
+	}
177 177
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Constructed/Sequence.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -12,14 +12,14 @@
 block discarded – undo
12 12
  */
13 13
 class Sequence extends Structure
14 14
 {
15
-    /**
16
-     * Constructor.
17
-     *
18
-     * @param Element ...$elements Any number of elements
19
-     */
20
-    public function __construct(Element ...$elements)
21
-    {
22
-        $this->_typeTag = self::TYPE_SEQUENCE;
23
-        parent::__construct(...$elements);
24
-    }
15
+	/**
16
+	 * Constructor.
17
+	 *
18
+	 * @param Element ...$elements Any number of elements
19
+	 */
20
+	public function __construct(Element ...$elements)
21
+	{
22
+		$this->_typeTag = self::TYPE_SEQUENCE;
23
+		parent::__construct(...$elements);
24
+	}
25 25
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type\Constructed;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Element.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1;
6 6
 
Please login to merge, or discard this patch.
Switch Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -407,20 +407,20 @@  discard block
 block discarded – undo
407 407
     protected static function _determineImplClass(Identifier $identifier): string
408 408
     {
409 409
         switch ($identifier->typeClass()) {
410
-            case Identifier::CLASS_UNIVERSAL:
411
-                $cls = self::_determineUniversalImplClass($identifier->intTag());
412
-                // constructed strings may be present in BER
413
-                if ($identifier->isConstructed() &&
414
-                    is_subclass_of($cls, StringType::class)) {
415
-                    $cls = ConstructedString::class;
416
-                }
417
-                return $cls;
418
-            case Identifier::CLASS_CONTEXT_SPECIFIC:
419
-                return ContextSpecificType::class;
420
-            case Identifier::CLASS_APPLICATION:
421
-                return ApplicationType::class;
422
-            case Identifier::CLASS_PRIVATE:
423
-                return PrivateType::class;
410
+        case Identifier::CLASS_UNIVERSAL:
411
+            $cls = self::_determineUniversalImplClass($identifier->intTag());
412
+            // constructed strings may be present in BER
413
+            if ($identifier->isConstructed() &&
414
+                is_subclass_of($cls, StringType::class)) {
415
+                $cls = ConstructedString::class;
416
+            }
417
+            return $cls;
418
+        case Identifier::CLASS_CONTEXT_SPECIFIC:
419
+            return ContextSpecificType::class;
420
+        case Identifier::CLASS_APPLICATION:
421
+            return ApplicationType::class;
422
+        case Identifier::CLASS_PRIVATE:
423
+            return PrivateType::class;
424 424
         }
425 425
         throw new \UnexpectedValueException(
426 426
             sprintf('%s %d not implemented.',
@@ -493,12 +493,12 @@  discard block
 block discarded – undo
493 493
     private function _isPseudoType(int $tag): bool
494 494
     {
495 495
         switch ($tag) {
496
-            case self::TYPE_STRING:
497
-                return $this instanceof StringType;
498
-            case self::TYPE_TIME:
499
-                return $this instanceof TimeType;
500
-            case self::TYPE_CONSTRUCTED_STRING:
501
-                return $this instanceof ConstructedString;
496
+        case self::TYPE_STRING:
497
+            return $this instanceof StringType;
498
+        case self::TYPE_TIME:
499
+            return $this instanceof TimeType;
500
+        case self::TYPE_CONSTRUCTED_STRING:
501
+            return $this instanceof ConstructedString;
502 502
         }
503 503
         return false;
504 504
     }
Please login to merge, or discard this patch.
Indentation   +478 added lines, -478 removed lines patch added patch discarded remove patch
@@ -24,482 +24,482 @@
 block discarded – undo
24 24
  */
25 25
 abstract class Element implements ElementBase
26 26
 {
27
-    // Universal type tags
28
-    const TYPE_EOC = 0x00;
29
-    const TYPE_BOOLEAN = 0x01;
30
-    const TYPE_INTEGER = 0x02;
31
-    const TYPE_BIT_STRING = 0x03;
32
-    const TYPE_OCTET_STRING = 0x04;
33
-    const TYPE_NULL = 0x05;
34
-    const TYPE_OBJECT_IDENTIFIER = 0x06;
35
-    const TYPE_OBJECT_DESCRIPTOR = 0x07;
36
-    const TYPE_EXTERNAL = 0x08;
37
-    const TYPE_REAL = 0x09;
38
-    const TYPE_ENUMERATED = 0x0a;
39
-    const TYPE_EMBEDDED_PDV = 0x0b;
40
-    const TYPE_UTF8_STRING = 0x0c;
41
-    const TYPE_RELATIVE_OID = 0x0d;
42
-    const TYPE_SEQUENCE = 0x10;
43
-    const TYPE_SET = 0x11;
44
-    const TYPE_NUMERIC_STRING = 0x12;
45
-    const TYPE_PRINTABLE_STRING = 0x13;
46
-    const TYPE_T61_STRING = 0x14;
47
-    const TYPE_VIDEOTEX_STRING = 0x15;
48
-    const TYPE_IA5_STRING = 0x16;
49
-    const TYPE_UTC_TIME = 0x17;
50
-    const TYPE_GENERALIZED_TIME = 0x18;
51
-    const TYPE_GRAPHIC_STRING = 0x19;
52
-    const TYPE_VISIBLE_STRING = 0x1a;
53
-    const TYPE_GENERAL_STRING = 0x1b;
54
-    const TYPE_UNIVERSAL_STRING = 0x1c;
55
-    const TYPE_CHARACTER_STRING = 0x1d;
56
-    const TYPE_BMP_STRING = 0x1e;
57
-
58
-    /**
59
-     * Mapping from universal type tag to implementation class name.
60
-     *
61
-     * @internal
62
-     *
63
-     * @var array
64
-     */
65
-    const MAP_TAG_TO_CLASS = [
66
-        self::TYPE_EOC => Primitive\EOC::class,
67
-        self::TYPE_BOOLEAN => Primitive\Boolean::class,
68
-        self::TYPE_INTEGER => Primitive\Integer::class,
69
-        self::TYPE_BIT_STRING => Primitive\BitString::class,
70
-        self::TYPE_OCTET_STRING => Primitive\OctetString::class,
71
-        self::TYPE_NULL => Primitive\NullType::class,
72
-        self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
73
-        self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
74
-        self::TYPE_REAL => Primitive\Real::class,
75
-        self::TYPE_ENUMERATED => Primitive\Enumerated::class,
76
-        self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
77
-        self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
78
-        self::TYPE_SEQUENCE => Constructed\Sequence::class,
79
-        self::TYPE_SET => Constructed\Set::class,
80
-        self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
81
-        self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
82
-        self::TYPE_T61_STRING => Primitive\T61String::class,
83
-        self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
84
-        self::TYPE_IA5_STRING => Primitive\IA5String::class,
85
-        self::TYPE_UTC_TIME => Primitive\UTCTime::class,
86
-        self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
87
-        self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
88
-        self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
89
-        self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
90
-        self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
91
-        self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
92
-        self::TYPE_BMP_STRING => Primitive\BMPString::class,
93
-    ];
94
-
95
-    /**
96
-     * Pseudotype for all string types.
97
-     *
98
-     * May be used as an expectation parameter.
99
-     *
100
-     * @var int
101
-     */
102
-    const TYPE_STRING = -1;
103
-
104
-    /**
105
-     * Pseudotype for all time types.
106
-     *
107
-     * May be used as an expectation parameter.
108
-     *
109
-     * @var int
110
-     */
111
-    const TYPE_TIME = -2;
112
-
113
-    /**
114
-     * Pseudotype for constructed strings.
115
-     *
116
-     * May be used as an expectation parameter.
117
-     *
118
-     * @var int
119
-     */
120
-    const TYPE_CONSTRUCTED_STRING = -3;
121
-
122
-    /**
123
-     * Mapping from universal type tag to human readable name.
124
-     *
125
-     * @internal
126
-     *
127
-     * @var array
128
-     */
129
-    const MAP_TYPE_TO_NAME = [
130
-        self::TYPE_EOC => 'EOC',
131
-        self::TYPE_BOOLEAN => 'BOOLEAN',
132
-        self::TYPE_INTEGER => 'INTEGER',
133
-        self::TYPE_BIT_STRING => 'BIT STRING',
134
-        self::TYPE_OCTET_STRING => 'OCTET STRING',
135
-        self::TYPE_NULL => 'NULL',
136
-        self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER',
137
-        self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor',
138
-        self::TYPE_EXTERNAL => 'EXTERNAL',
139
-        self::TYPE_REAL => 'REAL',
140
-        self::TYPE_ENUMERATED => 'ENUMERATED',
141
-        self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV',
142
-        self::TYPE_UTF8_STRING => 'UTF8String',
143
-        self::TYPE_RELATIVE_OID => 'RELATIVE-OID',
144
-        self::TYPE_SEQUENCE => 'SEQUENCE',
145
-        self::TYPE_SET => 'SET',
146
-        self::TYPE_NUMERIC_STRING => 'NumericString',
147
-        self::TYPE_PRINTABLE_STRING => 'PrintableString',
148
-        self::TYPE_T61_STRING => 'T61String',
149
-        self::TYPE_VIDEOTEX_STRING => 'VideotexString',
150
-        self::TYPE_IA5_STRING => 'IA5String',
151
-        self::TYPE_UTC_TIME => 'UTCTime',
152
-        self::TYPE_GENERALIZED_TIME => 'GeneralizedTime',
153
-        self::TYPE_GRAPHIC_STRING => 'GraphicString',
154
-        self::TYPE_VISIBLE_STRING => 'VisibleString',
155
-        self::TYPE_GENERAL_STRING => 'GeneralString',
156
-        self::TYPE_UNIVERSAL_STRING => 'UniversalString',
157
-        self::TYPE_CHARACTER_STRING => 'CHARACTER STRING',
158
-        self::TYPE_BMP_STRING => 'BMPString',
159
-        self::TYPE_STRING => 'Any String',
160
-        self::TYPE_TIME => 'Any Time',
161
-        self::TYPE_CONSTRUCTED_STRING => 'Constructed String',
162
-    ];
163
-
164
-    /**
165
-     * Element's type tag.
166
-     *
167
-     * @var int
168
-     */
169
-    protected $_typeTag;
170
-
171
-    /**
172
-     * Whether type shall be encoded with indefinite length.
173
-     *
174
-     * @var bool
175
-     */
176
-    protected $_indefiniteLength = false;
177
-
178
-    /**
179
-     * {@inheritdoc}
180
-     */
181
-    abstract public function typeClass(): int;
182
-
183
-    /**
184
-     * {@inheritdoc}
185
-     */
186
-    abstract public function isConstructed(): bool;
187
-
188
-    /**
189
-     * Decode element from DER data.
190
-     *
191
-     * @param string   $data   DER encoded data
192
-     * @param null|int $offset Reference to the variable that contains offset
193
-     *                         into the data where to start parsing.
194
-     *                         Variable is updated to the offset next to the
195
-     *                         parsed element. If null, start from offset 0.
196
-     *
197
-     * @throws DecodeException           If decoding fails
198
-     * @throws \UnexpectedValueException If called in the context of an expected
199
-     *                                   type, but decoding yields another type
200
-     *
201
-     * @return ElementBase
202
-     */
203
-    public static function fromDER(string $data, int &$offset = null): ElementBase
204
-    {
205
-        $idx = $offset ?? 0;
206
-        // decode identifier
207
-        $identifier = Identifier::fromDER($data, $idx);
208
-        // determine class that implements type specific decoding
209
-        $cls = self::_determineImplClass($identifier);
210
-        try {
211
-            // decode remaining element
212
-            $element = $cls::_decodeFromDER($identifier, $data, $idx);
213
-        } catch (\LogicException $e) {
214
-            // rethrow as a RuntimeException for unified exception handling
215
-            throw new DecodeException(
216
-                sprintf('Error while decoding %s.',
217
-                    self::tagToName($identifier->intTag())), 0, $e);
218
-        }
219
-        // if called in the context of a concrete class, check
220
-        // that decoded type matches the type of a calling class
221
-        $called_class = get_called_class();
222
-        if (self::class !== $called_class) {
223
-            if (!$element instanceof $called_class) {
224
-                throw new \UnexpectedValueException(
225
-                    sprintf('%s expected, got %s.', $called_class,
226
-                        get_class($element)));
227
-            }
228
-        }
229
-        // update offset for the caller
230
-        if (isset($offset)) {
231
-            $offset = $idx;
232
-        }
233
-        return $element;
234
-    }
235
-
236
-    /**
237
-     * {@inheritdoc}
238
-     */
239
-    public function toDER(): string
240
-    {
241
-        $identifier = new Identifier($this->typeClass(),
242
-            $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
243
-            $this->_typeTag);
244
-        $content = $this->_encodedContentDER();
245
-        if ($this->_indefiniteLength) {
246
-            $length = new Length(0, true);
247
-            $eoc = new Primitive\EOC();
248
-            return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER();
249
-        }
250
-        $length = new Length(strlen($content));
251
-        return $identifier->toDER() . $length->toDER() . $content;
252
-    }
253
-
254
-    /**
255
-     * {@inheritdoc}
256
-     */
257
-    public function tag(): int
258
-    {
259
-        return $this->_typeTag;
260
-    }
261
-
262
-    /**
263
-     * {@inheritdoc}
264
-     */
265
-    public function isType(int $tag): bool
266
-    {
267
-        // if element is context specific
268
-        if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) {
269
-            return false;
270
-        }
271
-        // negative tags identify an abstract pseudotype
272
-        if ($tag < 0) {
273
-            return $this->_isPseudoType($tag);
274
-        }
275
-        return $this->_isConcreteType($tag);
276
-    }
277
-
278
-    /**
279
-     * {@inheritdoc}
280
-     */
281
-    public function expectType(int $tag): ElementBase
282
-    {
283
-        if (!$this->isType($tag)) {
284
-            throw new \UnexpectedValueException(
285
-                sprintf('%s expected, got %s.', self::tagToName($tag),
286
-                    $this->_typeDescriptorString()));
287
-        }
288
-        return $this;
289
-    }
290
-
291
-    /**
292
-     * {@inheritdoc}
293
-     */
294
-    public function isTagged(): bool
295
-    {
296
-        return $this instanceof TaggedType;
297
-    }
298
-
299
-    /**
300
-     * {@inheritdoc}
301
-     */
302
-    public function expectTagged(?int $tag = null): TaggedType
303
-    {
304
-        if (!$this->isTagged()) {
305
-            throw new \UnexpectedValueException(
306
-                sprintf('Context specific element expected, got %s.',
307
-                    Identifier::classToName($this->typeClass())));
308
-        }
309
-        if (isset($tag) && $this->tag() !== $tag) {
310
-            throw new \UnexpectedValueException(
311
-                sprintf('Tag %d expected, got %d.', $tag, $this->tag()));
312
-        }
313
-        return $this;
314
-    }
315
-
316
-    /**
317
-     * Whether element has indefinite length.
318
-     *
319
-     * @return bool
320
-     */
321
-    public function hasIndefiniteLength(): bool
322
-    {
323
-        return $this->_indefiniteLength;
324
-    }
325
-
326
-    /**
327
-     * Get self with indefinite length encoding set.
328
-     *
329
-     * @param bool $indefinite True for indefinite length, false for definite length
330
-     *
331
-     * @return self
332
-     */
333
-    public function withIndefiniteLength(bool $indefinite = true): self
334
-    {
335
-        $obj = clone $this;
336
-        $obj->_indefiniteLength = $indefinite;
337
-        return $obj;
338
-    }
339
-
340
-    /**
341
-     * {@inheritdoc}
342
-     */
343
-    final public function asElement(): Element
344
-    {
345
-        return $this;
346
-    }
347
-
348
-    /**
349
-     * Get element decorated with `UnspecifiedType` object.
350
-     *
351
-     * @return UnspecifiedType
352
-     */
353
-    public function asUnspecified(): UnspecifiedType
354
-    {
355
-        return new UnspecifiedType($this);
356
-    }
357
-
358
-    /**
359
-     * Get human readable name for an universal tag.
360
-     *
361
-     * @param int $tag
362
-     *
363
-     * @return string
364
-     */
365
-    public static function tagToName(int $tag): string
366
-    {
367
-        if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
368
-            return "TAG {$tag}";
369
-        }
370
-        return self::MAP_TYPE_TO_NAME[$tag];
371
-    }
372
-
373
-    /**
374
-     * Get the content encoded in DER.
375
-     *
376
-     * Returns the DER encoded content without identifier and length header octets.
377
-     *
378
-     * @return string
379
-     */
380
-    abstract protected function _encodedContentDER(): string;
381
-
382
-    /**
383
-     * Decode type-specific element from DER.
384
-     *
385
-     * @param Identifier $identifier Pre-parsed identifier
386
-     * @param string     $data       DER data
387
-     * @param int        $offset     Offset in data to the next byte after identifier
388
-     *
389
-     * @throws DecodeException If decoding fails
390
-     *
391
-     * @return ElementBase
392
-     */
393
-    protected static function _decodeFromDER(Identifier $identifier,
394
-        string $data, int &$offset): ElementBase
395
-    {
396
-        throw new \BadMethodCallException(
397
-            __METHOD__ . ' must be implemented in derived class.');
398
-    }
399
-
400
-    /**
401
-     * Determine the class that implements the type.
402
-     *
403
-     * @param Identifier $identifier
404
-     *
405
-     * @return string Class name
406
-     */
407
-    protected static function _determineImplClass(Identifier $identifier): string
408
-    {
409
-        switch ($identifier->typeClass()) {
410
-            case Identifier::CLASS_UNIVERSAL:
411
-                $cls = self::_determineUniversalImplClass($identifier->intTag());
412
-                // constructed strings may be present in BER
413
-                if ($identifier->isConstructed() &&
414
-                    is_subclass_of($cls, StringType::class)) {
415
-                    $cls = ConstructedString::class;
416
-                }
417
-                return $cls;
418
-            case Identifier::CLASS_CONTEXT_SPECIFIC:
419
-                return ContextSpecificType::class;
420
-            case Identifier::CLASS_APPLICATION:
421
-                return ApplicationType::class;
422
-            case Identifier::CLASS_PRIVATE:
423
-                return PrivateType::class;
424
-        }
425
-        throw new \UnexpectedValueException(
426
-            sprintf('%s %d not implemented.',
427
-                Identifier::classToName($identifier->typeClass()),
428
-                $identifier->tag()));
429
-    }
430
-
431
-    /**
432
-     * Determine the class that implements an universal type of the given tag.
433
-     *
434
-     * @param int $tag
435
-     *
436
-     * @throws \UnexpectedValueException
437
-     *
438
-     * @return string Class name
439
-     */
440
-    protected static function _determineUniversalImplClass(int $tag): string
441
-    {
442
-        if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
443
-            throw new \UnexpectedValueException(
444
-                "Universal tag {$tag} not implemented.");
445
-        }
446
-        return self::MAP_TAG_TO_CLASS[$tag];
447
-    }
448
-
449
-    /**
450
-     * Get textual description of the type for debugging purposes.
451
-     *
452
-     * @return string
453
-     */
454
-    protected function _typeDescriptorString(): string
455
-    {
456
-        if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) {
457
-            return self::tagToName($this->_typeTag);
458
-        }
459
-        return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()),
460
-            $this->_typeTag);
461
-    }
462
-
463
-    /**
464
-     * Check whether the element is a concrete type of a given tag.
465
-     *
466
-     * @param int $tag
467
-     *
468
-     * @return bool
469
-     */
470
-    private function _isConcreteType(int $tag): bool
471
-    {
472
-        // if tag doesn't match
473
-        if ($this->tag() !== $tag) {
474
-            return false;
475
-        }
476
-        // if type is universal check that instance is of a correct class
477
-        if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) {
478
-            $cls = self::_determineUniversalImplClass($tag);
479
-            if (!$this instanceof $cls) {
480
-                return false;
481
-            }
482
-        }
483
-        return true;
484
-    }
485
-
486
-    /**
487
-     * Check whether the element is a pseudotype.
488
-     *
489
-     * @param int $tag
490
-     *
491
-     * @return bool
492
-     */
493
-    private function _isPseudoType(int $tag): bool
494
-    {
495
-        switch ($tag) {
496
-            case self::TYPE_STRING:
497
-                return $this instanceof StringType;
498
-            case self::TYPE_TIME:
499
-                return $this instanceof TimeType;
500
-            case self::TYPE_CONSTRUCTED_STRING:
501
-                return $this instanceof ConstructedString;
502
-        }
503
-        return false;
504
-    }
27
+	// Universal type tags
28
+	const TYPE_EOC = 0x00;
29
+	const TYPE_BOOLEAN = 0x01;
30
+	const TYPE_INTEGER = 0x02;
31
+	const TYPE_BIT_STRING = 0x03;
32
+	const TYPE_OCTET_STRING = 0x04;
33
+	const TYPE_NULL = 0x05;
34
+	const TYPE_OBJECT_IDENTIFIER = 0x06;
35
+	const TYPE_OBJECT_DESCRIPTOR = 0x07;
36
+	const TYPE_EXTERNAL = 0x08;
37
+	const TYPE_REAL = 0x09;
38
+	const TYPE_ENUMERATED = 0x0a;
39
+	const TYPE_EMBEDDED_PDV = 0x0b;
40
+	const TYPE_UTF8_STRING = 0x0c;
41
+	const TYPE_RELATIVE_OID = 0x0d;
42
+	const TYPE_SEQUENCE = 0x10;
43
+	const TYPE_SET = 0x11;
44
+	const TYPE_NUMERIC_STRING = 0x12;
45
+	const TYPE_PRINTABLE_STRING = 0x13;
46
+	const TYPE_T61_STRING = 0x14;
47
+	const TYPE_VIDEOTEX_STRING = 0x15;
48
+	const TYPE_IA5_STRING = 0x16;
49
+	const TYPE_UTC_TIME = 0x17;
50
+	const TYPE_GENERALIZED_TIME = 0x18;
51
+	const TYPE_GRAPHIC_STRING = 0x19;
52
+	const TYPE_VISIBLE_STRING = 0x1a;
53
+	const TYPE_GENERAL_STRING = 0x1b;
54
+	const TYPE_UNIVERSAL_STRING = 0x1c;
55
+	const TYPE_CHARACTER_STRING = 0x1d;
56
+	const TYPE_BMP_STRING = 0x1e;
57
+
58
+	/**
59
+	 * Mapping from universal type tag to implementation class name.
60
+	 *
61
+	 * @internal
62
+	 *
63
+	 * @var array
64
+	 */
65
+	const MAP_TAG_TO_CLASS = [
66
+		self::TYPE_EOC => Primitive\EOC::class,
67
+		self::TYPE_BOOLEAN => Primitive\Boolean::class,
68
+		self::TYPE_INTEGER => Primitive\Integer::class,
69
+		self::TYPE_BIT_STRING => Primitive\BitString::class,
70
+		self::TYPE_OCTET_STRING => Primitive\OctetString::class,
71
+		self::TYPE_NULL => Primitive\NullType::class,
72
+		self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
73
+		self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
74
+		self::TYPE_REAL => Primitive\Real::class,
75
+		self::TYPE_ENUMERATED => Primitive\Enumerated::class,
76
+		self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
77
+		self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
78
+		self::TYPE_SEQUENCE => Constructed\Sequence::class,
79
+		self::TYPE_SET => Constructed\Set::class,
80
+		self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
81
+		self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
82
+		self::TYPE_T61_STRING => Primitive\T61String::class,
83
+		self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
84
+		self::TYPE_IA5_STRING => Primitive\IA5String::class,
85
+		self::TYPE_UTC_TIME => Primitive\UTCTime::class,
86
+		self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
87
+		self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
88
+		self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
89
+		self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
90
+		self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
91
+		self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
92
+		self::TYPE_BMP_STRING => Primitive\BMPString::class,
93
+	];
94
+
95
+	/**
96
+	 * Pseudotype for all string types.
97
+	 *
98
+	 * May be used as an expectation parameter.
99
+	 *
100
+	 * @var int
101
+	 */
102
+	const TYPE_STRING = -1;
103
+
104
+	/**
105
+	 * Pseudotype for all time types.
106
+	 *
107
+	 * May be used as an expectation parameter.
108
+	 *
109
+	 * @var int
110
+	 */
111
+	const TYPE_TIME = -2;
112
+
113
+	/**
114
+	 * Pseudotype for constructed strings.
115
+	 *
116
+	 * May be used as an expectation parameter.
117
+	 *
118
+	 * @var int
119
+	 */
120
+	const TYPE_CONSTRUCTED_STRING = -3;
121
+
122
+	/**
123
+	 * Mapping from universal type tag to human readable name.
124
+	 *
125
+	 * @internal
126
+	 *
127
+	 * @var array
128
+	 */
129
+	const MAP_TYPE_TO_NAME = [
130
+		self::TYPE_EOC => 'EOC',
131
+		self::TYPE_BOOLEAN => 'BOOLEAN',
132
+		self::TYPE_INTEGER => 'INTEGER',
133
+		self::TYPE_BIT_STRING => 'BIT STRING',
134
+		self::TYPE_OCTET_STRING => 'OCTET STRING',
135
+		self::TYPE_NULL => 'NULL',
136
+		self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER',
137
+		self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor',
138
+		self::TYPE_EXTERNAL => 'EXTERNAL',
139
+		self::TYPE_REAL => 'REAL',
140
+		self::TYPE_ENUMERATED => 'ENUMERATED',
141
+		self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV',
142
+		self::TYPE_UTF8_STRING => 'UTF8String',
143
+		self::TYPE_RELATIVE_OID => 'RELATIVE-OID',
144
+		self::TYPE_SEQUENCE => 'SEQUENCE',
145
+		self::TYPE_SET => 'SET',
146
+		self::TYPE_NUMERIC_STRING => 'NumericString',
147
+		self::TYPE_PRINTABLE_STRING => 'PrintableString',
148
+		self::TYPE_T61_STRING => 'T61String',
149
+		self::TYPE_VIDEOTEX_STRING => 'VideotexString',
150
+		self::TYPE_IA5_STRING => 'IA5String',
151
+		self::TYPE_UTC_TIME => 'UTCTime',
152
+		self::TYPE_GENERALIZED_TIME => 'GeneralizedTime',
153
+		self::TYPE_GRAPHIC_STRING => 'GraphicString',
154
+		self::TYPE_VISIBLE_STRING => 'VisibleString',
155
+		self::TYPE_GENERAL_STRING => 'GeneralString',
156
+		self::TYPE_UNIVERSAL_STRING => 'UniversalString',
157
+		self::TYPE_CHARACTER_STRING => 'CHARACTER STRING',
158
+		self::TYPE_BMP_STRING => 'BMPString',
159
+		self::TYPE_STRING => 'Any String',
160
+		self::TYPE_TIME => 'Any Time',
161
+		self::TYPE_CONSTRUCTED_STRING => 'Constructed String',
162
+	];
163
+
164
+	/**
165
+	 * Element's type tag.
166
+	 *
167
+	 * @var int
168
+	 */
169
+	protected $_typeTag;
170
+
171
+	/**
172
+	 * Whether type shall be encoded with indefinite length.
173
+	 *
174
+	 * @var bool
175
+	 */
176
+	protected $_indefiniteLength = false;
177
+
178
+	/**
179
+	 * {@inheritdoc}
180
+	 */
181
+	abstract public function typeClass(): int;
182
+
183
+	/**
184
+	 * {@inheritdoc}
185
+	 */
186
+	abstract public function isConstructed(): bool;
187
+
188
+	/**
189
+	 * Decode element from DER data.
190
+	 *
191
+	 * @param string   $data   DER encoded data
192
+	 * @param null|int $offset Reference to the variable that contains offset
193
+	 *                         into the data where to start parsing.
194
+	 *                         Variable is updated to the offset next to the
195
+	 *                         parsed element. If null, start from offset 0.
196
+	 *
197
+	 * @throws DecodeException           If decoding fails
198
+	 * @throws \UnexpectedValueException If called in the context of an expected
199
+	 *                                   type, but decoding yields another type
200
+	 *
201
+	 * @return ElementBase
202
+	 */
203
+	public static function fromDER(string $data, int &$offset = null): ElementBase
204
+	{
205
+		$idx = $offset ?? 0;
206
+		// decode identifier
207
+		$identifier = Identifier::fromDER($data, $idx);
208
+		// determine class that implements type specific decoding
209
+		$cls = self::_determineImplClass($identifier);
210
+		try {
211
+			// decode remaining element
212
+			$element = $cls::_decodeFromDER($identifier, $data, $idx);
213
+		} catch (\LogicException $e) {
214
+			// rethrow as a RuntimeException for unified exception handling
215
+			throw new DecodeException(
216
+				sprintf('Error while decoding %s.',
217
+					self::tagToName($identifier->intTag())), 0, $e);
218
+		}
219
+		// if called in the context of a concrete class, check
220
+		// that decoded type matches the type of a calling class
221
+		$called_class = get_called_class();
222
+		if (self::class !== $called_class) {
223
+			if (!$element instanceof $called_class) {
224
+				throw new \UnexpectedValueException(
225
+					sprintf('%s expected, got %s.', $called_class,
226
+						get_class($element)));
227
+			}
228
+		}
229
+		// update offset for the caller
230
+		if (isset($offset)) {
231
+			$offset = $idx;
232
+		}
233
+		return $element;
234
+	}
235
+
236
+	/**
237
+	 * {@inheritdoc}
238
+	 */
239
+	public function toDER(): string
240
+	{
241
+		$identifier = new Identifier($this->typeClass(),
242
+			$this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
243
+			$this->_typeTag);
244
+		$content = $this->_encodedContentDER();
245
+		if ($this->_indefiniteLength) {
246
+			$length = new Length(0, true);
247
+			$eoc = new Primitive\EOC();
248
+			return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER();
249
+		}
250
+		$length = new Length(strlen($content));
251
+		return $identifier->toDER() . $length->toDER() . $content;
252
+	}
253
+
254
+	/**
255
+	 * {@inheritdoc}
256
+	 */
257
+	public function tag(): int
258
+	{
259
+		return $this->_typeTag;
260
+	}
261
+
262
+	/**
263
+	 * {@inheritdoc}
264
+	 */
265
+	public function isType(int $tag): bool
266
+	{
267
+		// if element is context specific
268
+		if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) {
269
+			return false;
270
+		}
271
+		// negative tags identify an abstract pseudotype
272
+		if ($tag < 0) {
273
+			return $this->_isPseudoType($tag);
274
+		}
275
+		return $this->_isConcreteType($tag);
276
+	}
277
+
278
+	/**
279
+	 * {@inheritdoc}
280
+	 */
281
+	public function expectType(int $tag): ElementBase
282
+	{
283
+		if (!$this->isType($tag)) {
284
+			throw new \UnexpectedValueException(
285
+				sprintf('%s expected, got %s.', self::tagToName($tag),
286
+					$this->_typeDescriptorString()));
287
+		}
288
+		return $this;
289
+	}
290
+
291
+	/**
292
+	 * {@inheritdoc}
293
+	 */
294
+	public function isTagged(): bool
295
+	{
296
+		return $this instanceof TaggedType;
297
+	}
298
+
299
+	/**
300
+	 * {@inheritdoc}
301
+	 */
302
+	public function expectTagged(?int $tag = null): TaggedType
303
+	{
304
+		if (!$this->isTagged()) {
305
+			throw new \UnexpectedValueException(
306
+				sprintf('Context specific element expected, got %s.',
307
+					Identifier::classToName($this->typeClass())));
308
+		}
309
+		if (isset($tag) && $this->tag() !== $tag) {
310
+			throw new \UnexpectedValueException(
311
+				sprintf('Tag %d expected, got %d.', $tag, $this->tag()));
312
+		}
313
+		return $this;
314
+	}
315
+
316
+	/**
317
+	 * Whether element has indefinite length.
318
+	 *
319
+	 * @return bool
320
+	 */
321
+	public function hasIndefiniteLength(): bool
322
+	{
323
+		return $this->_indefiniteLength;
324
+	}
325
+
326
+	/**
327
+	 * Get self with indefinite length encoding set.
328
+	 *
329
+	 * @param bool $indefinite True for indefinite length, false for definite length
330
+	 *
331
+	 * @return self
332
+	 */
333
+	public function withIndefiniteLength(bool $indefinite = true): self
334
+	{
335
+		$obj = clone $this;
336
+		$obj->_indefiniteLength = $indefinite;
337
+		return $obj;
338
+	}
339
+
340
+	/**
341
+	 * {@inheritdoc}
342
+	 */
343
+	final public function asElement(): Element
344
+	{
345
+		return $this;
346
+	}
347
+
348
+	/**
349
+	 * Get element decorated with `UnspecifiedType` object.
350
+	 *
351
+	 * @return UnspecifiedType
352
+	 */
353
+	public function asUnspecified(): UnspecifiedType
354
+	{
355
+		return new UnspecifiedType($this);
356
+	}
357
+
358
+	/**
359
+	 * Get human readable name for an universal tag.
360
+	 *
361
+	 * @param int $tag
362
+	 *
363
+	 * @return string
364
+	 */
365
+	public static function tagToName(int $tag): string
366
+	{
367
+		if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
368
+			return "TAG {$tag}";
369
+		}
370
+		return self::MAP_TYPE_TO_NAME[$tag];
371
+	}
372
+
373
+	/**
374
+	 * Get the content encoded in DER.
375
+	 *
376
+	 * Returns the DER encoded content without identifier and length header octets.
377
+	 *
378
+	 * @return string
379
+	 */
380
+	abstract protected function _encodedContentDER(): string;
381
+
382
+	/**
383
+	 * Decode type-specific element from DER.
384
+	 *
385
+	 * @param Identifier $identifier Pre-parsed identifier
386
+	 * @param string     $data       DER data
387
+	 * @param int        $offset     Offset in data to the next byte after identifier
388
+	 *
389
+	 * @throws DecodeException If decoding fails
390
+	 *
391
+	 * @return ElementBase
392
+	 */
393
+	protected static function _decodeFromDER(Identifier $identifier,
394
+		string $data, int &$offset): ElementBase
395
+	{
396
+		throw new \BadMethodCallException(
397
+			__METHOD__ . ' must be implemented in derived class.');
398
+	}
399
+
400
+	/**
401
+	 * Determine the class that implements the type.
402
+	 *
403
+	 * @param Identifier $identifier
404
+	 *
405
+	 * @return string Class name
406
+	 */
407
+	protected static function _determineImplClass(Identifier $identifier): string
408
+	{
409
+		switch ($identifier->typeClass()) {
410
+			case Identifier::CLASS_UNIVERSAL:
411
+				$cls = self::_determineUniversalImplClass($identifier->intTag());
412
+				// constructed strings may be present in BER
413
+				if ($identifier->isConstructed() &&
414
+					is_subclass_of($cls, StringType::class)) {
415
+					$cls = ConstructedString::class;
416
+				}
417
+				return $cls;
418
+			case Identifier::CLASS_CONTEXT_SPECIFIC:
419
+				return ContextSpecificType::class;
420
+			case Identifier::CLASS_APPLICATION:
421
+				return ApplicationType::class;
422
+			case Identifier::CLASS_PRIVATE:
423
+				return PrivateType::class;
424
+		}
425
+		throw new \UnexpectedValueException(
426
+			sprintf('%s %d not implemented.',
427
+				Identifier::classToName($identifier->typeClass()),
428
+				$identifier->tag()));
429
+	}
430
+
431
+	/**
432
+	 * Determine the class that implements an universal type of the given tag.
433
+	 *
434
+	 * @param int $tag
435
+	 *
436
+	 * @throws \UnexpectedValueException
437
+	 *
438
+	 * @return string Class name
439
+	 */
440
+	protected static function _determineUniversalImplClass(int $tag): string
441
+	{
442
+		if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
443
+			throw new \UnexpectedValueException(
444
+				"Universal tag {$tag} not implemented.");
445
+		}
446
+		return self::MAP_TAG_TO_CLASS[$tag];
447
+	}
448
+
449
+	/**
450
+	 * Get textual description of the type for debugging purposes.
451
+	 *
452
+	 * @return string
453
+	 */
454
+	protected function _typeDescriptorString(): string
455
+	{
456
+		if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) {
457
+			return self::tagToName($this->_typeTag);
458
+		}
459
+		return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()),
460
+			$this->_typeTag);
461
+	}
462
+
463
+	/**
464
+	 * Check whether the element is a concrete type of a given tag.
465
+	 *
466
+	 * @param int $tag
467
+	 *
468
+	 * @return bool
469
+	 */
470
+	private function _isConcreteType(int $tag): bool
471
+	{
472
+		// if tag doesn't match
473
+		if ($this->tag() !== $tag) {
474
+			return false;
475
+		}
476
+		// if type is universal check that instance is of a correct class
477
+		if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) {
478
+			$cls = self::_determineUniversalImplClass($tag);
479
+			if (!$this instanceof $cls) {
480
+				return false;
481
+			}
482
+		}
483
+		return true;
484
+	}
485
+
486
+	/**
487
+	 * Check whether the element is a pseudotype.
488
+	 *
489
+	 * @param int $tag
490
+	 *
491
+	 * @return bool
492
+	 */
493
+	private function _isPseudoType(int $tag): bool
494
+	{
495
+		switch ($tag) {
496
+			case self::TYPE_STRING:
497
+				return $this instanceof StringType;
498
+			case self::TYPE_TIME:
499
+				return $this instanceof TimeType;
500
+			case self::TYPE_CONSTRUCTED_STRING:
501
+				return $this instanceof ConstructedString;
502
+		}
503
+		return false;
504
+	}
505 505
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/UniversalClass.php 2 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -11,13 +11,13 @@
 block discarded – undo
11 11
  */
12 12
 trait UniversalClass
13 13
 {
14
-    /**
15
-     * @see \Sop\ASN1\Feature\ElementBase::typeClass()
16
-     *
17
-     * @return int
18
-     */
19
-    public function typeClass(): int
20
-    {
21
-        return Identifier::CLASS_UNIVERSAL;
22
-    }
14
+	/**
15
+	 * @see \Sop\ASN1\Feature\ElementBase::typeClass()
16
+	 *
17
+	 * @return int
18
+	 */
19
+	public function typeClass(): int
20
+	{
21
+		return Identifier::CLASS_UNIVERSAL;
22
+	}
23 23
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Type/Structure.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\ASN1\Type;
6 6
 
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
     public function __construct(ElementBase ...$elements)
47 47
     {
48 48
         $this->_elements = array_map(
49
-            function (ElementBase $el) {
49
+            function(ElementBase $el) {
50 50
                 return $el->asElement();
51 51
             }, $elements);
52 52
     }
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
     {
206 206
         if (!isset($this->_unspecifiedTypes)) {
207 207
             $this->_unspecifiedTypes = array_map(
208
-                function (Element $el) {
208
+                function(Element $el) {
209 209
                     return new UnspecifiedType($el);
210 210
                 }, $this->_elements);
211 211
         }
Please login to merge, or discard this patch.
Indentation   +373 added lines, -373 removed lines patch added patch discarded remove patch
@@ -15,400 +15,400 @@
 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
-     * {@inheritdoc}
320
-     */
321
-    protected function _encodedContentDER(): string
322
-    {
323
-        $data = '';
324
-        foreach ($this->_elements as $element) {
325
-            $data .= $element->toDER();
326
-        }
327
-        return $data;
328
-    }
318
+	/**
319
+	 * {@inheritdoc}
320
+	 */
321
+	protected function _encodedContentDER(): string
322
+	{
323
+		$data = '';
324
+		foreach ($this->_elements as $element) {
325
+			$data .= $element->toDER();
326
+		}
327
+		return $data;
328
+	}
329 329
 
330
-    /**
331
-     * {@inheritdoc}
332
-     *
333
-     * @return self
334
-     */
335
-    protected static function _decodeFromDER(Identifier $identifier,
336
-        string $data, int &$offset): ElementBase
337
-    {
338
-        if (!$identifier->isConstructed()) {
339
-            throw new DecodeException(
340
-                'Structured element must have constructed bit set.');
341
-        }
342
-        $idx = $offset;
343
-        $length = Length::expectFromDER($data, $idx);
344
-        if ($length->isIndefinite()) {
345
-            $type = self::_decodeIndefiniteLength($data, $idx);
346
-        } else {
347
-            $type = self::_decodeDefiniteLength($data, $idx,
348
-                $length->intLength());
349
-        }
350
-        $offset = $idx;
351
-        return $type;
352
-    }
330
+	/**
331
+	 * {@inheritdoc}
332
+	 *
333
+	 * @return self
334
+	 */
335
+	protected static function _decodeFromDER(Identifier $identifier,
336
+		string $data, int &$offset): ElementBase
337
+	{
338
+		if (!$identifier->isConstructed()) {
339
+			throw new DecodeException(
340
+				'Structured element must have constructed bit set.');
341
+		}
342
+		$idx = $offset;
343
+		$length = Length::expectFromDER($data, $idx);
344
+		if ($length->isIndefinite()) {
345
+			$type = self::_decodeIndefiniteLength($data, $idx);
346
+		} else {
347
+			$type = self::_decodeDefiniteLength($data, $idx,
348
+				$length->intLength());
349
+		}
350
+		$offset = $idx;
351
+		return $type;
352
+	}
353 353
 
354
-    /**
355
-     * Decode elements for a definite length.
356
-     *
357
-     * @param string $data   DER data
358
-     * @param int    $offset Offset to data
359
-     * @param int    $length Number of bytes to decode
360
-     *
361
-     * @throws DecodeException
362
-     *
363
-     * @return ElementBase
364
-     */
365
-    private static function _decodeDefiniteLength(string $data, int &$offset,
366
-        int $length): ElementBase
367
-    {
368
-        $idx = $offset;
369
-        $end = $idx + $length;
370
-        $elements = [];
371
-        while ($idx < $end) {
372
-            $elements[] = Element::fromDER($data, $idx);
373
-            // check that element didn't overflow length
374
-            if ($idx > $end) {
375
-                throw new DecodeException("Structure's content overflows length.");
376
-            }
377
-        }
378
-        $offset = $idx;
379
-        // return instance by static late binding
380
-        return new static(...$elements);
381
-    }
354
+	/**
355
+	 * Decode elements for a definite length.
356
+	 *
357
+	 * @param string $data   DER data
358
+	 * @param int    $offset Offset to data
359
+	 * @param int    $length Number of bytes to decode
360
+	 *
361
+	 * @throws DecodeException
362
+	 *
363
+	 * @return ElementBase
364
+	 */
365
+	private static function _decodeDefiniteLength(string $data, int &$offset,
366
+		int $length): ElementBase
367
+	{
368
+		$idx = $offset;
369
+		$end = $idx + $length;
370
+		$elements = [];
371
+		while ($idx < $end) {
372
+			$elements[] = Element::fromDER($data, $idx);
373
+			// check that element didn't overflow length
374
+			if ($idx > $end) {
375
+				throw new DecodeException("Structure's content overflows length.");
376
+			}
377
+		}
378
+		$offset = $idx;
379
+		// return instance by static late binding
380
+		return new static(...$elements);
381
+	}
382 382
 
383
-    /**
384
-     * Decode elements for an indefinite length.
385
-     *
386
-     * @param string $data   DER data
387
-     * @param int    $offset Offset to data
388
-     *
389
-     * @throws DecodeException
390
-     *
391
-     * @return ElementBase
392
-     */
393
-    private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase
394
-    {
395
-        $idx = $offset;
396
-        $elements = [];
397
-        $end = strlen($data);
398
-        while (true) {
399
-            if ($idx >= $end) {
400
-                throw new DecodeException(
401
-                    'Unexpected end of data while decoding indefinite length structure.');
402
-            }
403
-            $el = Element::fromDER($data, $idx);
404
-            if ($el->isType(self::TYPE_EOC)) {
405
-                break;
406
-            }
407
-            $elements[] = $el;
408
-        }
409
-        $offset = $idx;
410
-        $type = new static(...$elements);
411
-        $type->_indefiniteLength = true;
412
-        return $type;
413
-    }
383
+	/**
384
+	 * Decode elements for an indefinite length.
385
+	 *
386
+	 * @param string $data   DER data
387
+	 * @param int    $offset Offset to data
388
+	 *
389
+	 * @throws DecodeException
390
+	 *
391
+	 * @return ElementBase
392
+	 */
393
+	private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase
394
+	{
395
+		$idx = $offset;
396
+		$elements = [];
397
+		$end = strlen($data);
398
+		while (true) {
399
+			if ($idx >= $end) {
400
+				throw new DecodeException(
401
+					'Unexpected end of data while decoding indefinite length structure.');
402
+			}
403
+			$el = Element::fromDER($data, $idx);
404
+			if ($el->isType(self::TYPE_EOC)) {
405
+				break;
406
+			}
407
+			$elements[] = $el;
408
+		}
409
+		$offset = $idx;
410
+		$type = new static(...$elements);
411
+		$type->_indefiniteLength = true;
412
+		return $type;
413
+	}
414 414
 }
Please login to merge, or discard this patch.