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
Branch indefinite (96169e)
by Joni
03:55
created
lib/ASN1/Type/Constructed/Set.php 2 patches
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -12,54 +12,54 @@
 block discarded – undo
12 12
  */
13 13
 class Set extends Structure
14 14
 {
15
-    /**
16
-     * Constructor.
17
-     *
18
-     * @param Element ...$elements Any number of elements
19
-     */
20
-    public function __construct(Element ...$elements)
21
-    {
22
-        $this->_typeTag = self::TYPE_SET;
23
-        parent::__construct(...$elements);
24
-    }
15
+	/**
16
+	 * Constructor.
17
+	 *
18
+	 * @param Element ...$elements Any number of elements
19
+	 */
20
+	public function __construct(Element ...$elements)
21
+	{
22
+		$this->_typeTag = self::TYPE_SET;
23
+		parent::__construct(...$elements);
24
+	}
25 25
     
26
-    /**
27
-     * Sort by canonical ascending order.
28
-     * Used for DER encoding of SET type.
29
-     *
30
-     * @return self
31
-     */
32
-    public function sortedSet(): self
33
-    {
34
-        $obj = clone $this;
35
-        usort($obj->_elements,
36
-            function (Element $a, Element $b) {
37
-                if ($a->typeClass() != $b->typeClass()) {
38
-                    return $a->typeClass() < $b->typeClass() ? -1 : 1;
39
-                }
40
-                if ($a->tag() == $b->tag()) {
41
-                    return 0;
42
-                }
43
-                return $a->tag() < $b->tag() ? -1 : 1;
44
-            });
45
-        return $obj;
46
-    }
26
+	/**
27
+	 * Sort by canonical ascending order.
28
+	 * Used for DER encoding of SET type.
29
+	 *
30
+	 * @return self
31
+	 */
32
+	public function sortedSet(): self
33
+	{
34
+		$obj = clone $this;
35
+		usort($obj->_elements,
36
+			function (Element $a, Element $b) {
37
+				if ($a->typeClass() != $b->typeClass()) {
38
+					return $a->typeClass() < $b->typeClass() ? -1 : 1;
39
+				}
40
+				if ($a->tag() == $b->tag()) {
41
+					return 0;
42
+				}
43
+				return $a->tag() < $b->tag() ? -1 : 1;
44
+			});
45
+		return $obj;
46
+	}
47 47
     
48
-    /**
49
-     * Sort by encoding ascending order.
50
-     * Used for DER encoding of SET OF type.
51
-     *
52
-     * @return self
53
-     */
54
-    public function sortedSetOf(): self
55
-    {
56
-        $obj = clone $this;
57
-        usort($obj->_elements,
58
-            function (Element $a, Element $b) {
59
-                $a_der = $a->toDER();
60
-                $b_der = $b->toDER();
61
-                return strcmp($a_der, $b_der);
62
-            });
63
-        return $obj;
64
-    }
48
+	/**
49
+	 * Sort by encoding ascending order.
50
+	 * Used for DER encoding of SET OF type.
51
+	 *
52
+	 * @return self
53
+	 */
54
+	public function sortedSetOf(): self
55
+	{
56
+		$obj = clone $this;
57
+		usort($obj->_elements,
58
+			function (Element $a, Element $b) {
59
+				$a_der = $a->toDER();
60
+				$b_der = $b->toDER();
61
+				return strcmp($a_der, $b_der);
62
+			});
63
+		return $obj;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
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 ASN1\Type\Constructed;
6 6
 
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
     {
34 34
         $obj = clone $this;
35 35
         usort($obj->_elements,
36
-            function (Element $a, Element $b) {
36
+            function(Element $a, Element $b) {
37 37
                 if ($a->typeClass() != $b->typeClass()) {
38 38
                     return $a->typeClass() < $b->typeClass() ? -1 : 1;
39 39
                 }
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
     {
56 56
         $obj = clone $this;
57 57
         usort($obj->_elements,
58
-            function (Element $a, Element $b) {
58
+            function(Element $a, Element $b) {
59 59
                 $a_der = $a->toDER();
60 60
                 $b_der = $b->toDER();
61 61
                 return strcmp($a_der, $b_der);
Please login to merge, or discard this patch.
lib/ASN1/Element.php 3 patches
Switch Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -332,10 +332,10 @@  discard block
 block discarded – undo
332 332
     private function _isPseudoType(int $tag): bool
333 333
     {
334 334
         switch ($tag) {
335
-            case self::TYPE_STRING:
336
-                return $this instanceof StringType;
337
-            case self::TYPE_TIME:
338
-                return $this instanceof TimeType;
335
+        case self::TYPE_STRING:
336
+            return $this instanceof StringType;
337
+        case self::TYPE_TIME:
338
+            return $this instanceof TimeType;
339 339
         }
340 340
         return false;
341 341
     }
@@ -398,14 +398,14 @@  discard block
 block discarded – undo
398 398
     protected static function _determineImplClass(Identifier $identifier): string
399 399
     {
400 400
         switch ($identifier->typeClass()) {
401
-            case Identifier::CLASS_UNIVERSAL:
402
-                return self::_determineUniversalImplClass($identifier->intTag());
403
-            case Identifier::CLASS_CONTEXT_SPECIFIC:
404
-                return TaggedType::class;
405
-            case Identifier::CLASS_APPLICATION:
406
-                return ApplicationType::class;
407
-            case Identifier::CLASS_PRIVATE:
408
-                return PrivateType::class;
401
+        case Identifier::CLASS_UNIVERSAL:
402
+            return self::_determineUniversalImplClass($identifier->intTag());
403
+        case Identifier::CLASS_CONTEXT_SPECIFIC:
404
+            return TaggedType::class;
405
+        case Identifier::CLASS_APPLICATION:
406
+            return ApplicationType::class;
407
+        case Identifier::CLASS_PRIVATE:
408
+            return PrivateType::class;
409 409
         }
410 410
         throw new \UnexpectedValueException(
411 411
             sprintf("%s %d not implemented.",
Please login to merge, or discard this patch.
Indentation   +446 added lines, -446 removed lines patch added patch discarded remove patch
@@ -21,477 +21,477 @@
 block discarded – undo
21 21
  */
22 22
 abstract class Element implements ElementBase
23 23
 {
24
-    // Universal type tags
25
-    const TYPE_EOC = 0x00;
26
-    const TYPE_BOOLEAN = 0x01;
27
-    const TYPE_INTEGER = 0x02;
28
-    const TYPE_BIT_STRING = 0x03;
29
-    const TYPE_OCTET_STRING = 0x04;
30
-    const TYPE_NULL = 0x05;
31
-    const TYPE_OBJECT_IDENTIFIER = 0x06;
32
-    const TYPE_OBJECT_DESCRIPTOR = 0x07;
33
-    const TYPE_EXTERNAL = 0x08;
34
-    const TYPE_REAL = 0x09;
35
-    const TYPE_ENUMERATED = 0x0a;
36
-    const TYPE_EMBEDDED_PDV = 0x0b;
37
-    const TYPE_UTF8_STRING = 0x0c;
38
-    const TYPE_RELATIVE_OID = 0x0d;
39
-    const TYPE_SEQUENCE = 0x10;
40
-    const TYPE_SET = 0x11;
41
-    const TYPE_NUMERIC_STRING = 0x12;
42
-    const TYPE_PRINTABLE_STRING = 0x13;
43
-    const TYPE_T61_STRING = 0x14;
44
-    const TYPE_VIDEOTEX_STRING = 0x15;
45
-    const TYPE_IA5_STRING = 0x16;
46
-    const TYPE_UTC_TIME = 0x17;
47
-    const TYPE_GENERALIZED_TIME = 0x18;
48
-    const TYPE_GRAPHIC_STRING = 0x19;
49
-    const TYPE_VISIBLE_STRING = 0x1a;
50
-    const TYPE_GENERAL_STRING = 0x1b;
51
-    const TYPE_UNIVERSAL_STRING = 0x1c;
52
-    const TYPE_CHARACTER_STRING = 0x1d;
53
-    const TYPE_BMP_STRING = 0x1e;
24
+	// Universal type tags
25
+	const TYPE_EOC = 0x00;
26
+	const TYPE_BOOLEAN = 0x01;
27
+	const TYPE_INTEGER = 0x02;
28
+	const TYPE_BIT_STRING = 0x03;
29
+	const TYPE_OCTET_STRING = 0x04;
30
+	const TYPE_NULL = 0x05;
31
+	const TYPE_OBJECT_IDENTIFIER = 0x06;
32
+	const TYPE_OBJECT_DESCRIPTOR = 0x07;
33
+	const TYPE_EXTERNAL = 0x08;
34
+	const TYPE_REAL = 0x09;
35
+	const TYPE_ENUMERATED = 0x0a;
36
+	const TYPE_EMBEDDED_PDV = 0x0b;
37
+	const TYPE_UTF8_STRING = 0x0c;
38
+	const TYPE_RELATIVE_OID = 0x0d;
39
+	const TYPE_SEQUENCE = 0x10;
40
+	const TYPE_SET = 0x11;
41
+	const TYPE_NUMERIC_STRING = 0x12;
42
+	const TYPE_PRINTABLE_STRING = 0x13;
43
+	const TYPE_T61_STRING = 0x14;
44
+	const TYPE_VIDEOTEX_STRING = 0x15;
45
+	const TYPE_IA5_STRING = 0x16;
46
+	const TYPE_UTC_TIME = 0x17;
47
+	const TYPE_GENERALIZED_TIME = 0x18;
48
+	const TYPE_GRAPHIC_STRING = 0x19;
49
+	const TYPE_VISIBLE_STRING = 0x1a;
50
+	const TYPE_GENERAL_STRING = 0x1b;
51
+	const TYPE_UNIVERSAL_STRING = 0x1c;
52
+	const TYPE_CHARACTER_STRING = 0x1d;
53
+	const TYPE_BMP_STRING = 0x1e;
54 54
     
55
-    /**
56
-     * Mapping from universal type tag to implementation class name.
57
-     *
58
-     * @internal
59
-     *
60
-     * @var array
61
-     */
62
-    const MAP_TAG_TO_CLASS = [ /* @formatter:off */
63
-        self::TYPE_EOC => Primitive\EOC::class,
64
-        self::TYPE_BOOLEAN => Primitive\Boolean::class,
65
-        self::TYPE_INTEGER => Primitive\Integer::class,
66
-        self::TYPE_BIT_STRING => Primitive\BitString::class,
67
-        self::TYPE_OCTET_STRING => Primitive\OctetString::class,
68
-        self::TYPE_NULL => Primitive\NullType::class,
69
-        self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
70
-        self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
71
-        self::TYPE_REAL => Primitive\Real::class,
72
-        self::TYPE_ENUMERATED => Primitive\Enumerated::class,
73
-        self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
74
-        self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
75
-        self::TYPE_SEQUENCE => Constructed\Sequence::class,
76
-        self::TYPE_SET => Constructed\Set::class,
77
-        self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
78
-        self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
79
-        self::TYPE_T61_STRING => Primitive\T61String::class,
80
-        self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
81
-        self::TYPE_IA5_STRING => Primitive\IA5String::class,
82
-        self::TYPE_UTC_TIME => Primitive\UTCTime::class,
83
-        self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
84
-        self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
85
-        self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
86
-        self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
87
-        self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
88
-        self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
89
-        self::TYPE_BMP_STRING => Primitive\BMPString::class
90
-        /* @formatter:on */
91
-    ];
55
+	/**
56
+	 * Mapping from universal type tag to implementation class name.
57
+	 *
58
+	 * @internal
59
+	 *
60
+	 * @var array
61
+	 */
62
+	const MAP_TAG_TO_CLASS = [ /* @formatter:off */
63
+		self::TYPE_EOC => Primitive\EOC::class,
64
+		self::TYPE_BOOLEAN => Primitive\Boolean::class,
65
+		self::TYPE_INTEGER => Primitive\Integer::class,
66
+		self::TYPE_BIT_STRING => Primitive\BitString::class,
67
+		self::TYPE_OCTET_STRING => Primitive\OctetString::class,
68
+		self::TYPE_NULL => Primitive\NullType::class,
69
+		self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class,
70
+		self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class,
71
+		self::TYPE_REAL => Primitive\Real::class,
72
+		self::TYPE_ENUMERATED => Primitive\Enumerated::class,
73
+		self::TYPE_UTF8_STRING => Primitive\UTF8String::class,
74
+		self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class,
75
+		self::TYPE_SEQUENCE => Constructed\Sequence::class,
76
+		self::TYPE_SET => Constructed\Set::class,
77
+		self::TYPE_NUMERIC_STRING => Primitive\NumericString::class,
78
+		self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class,
79
+		self::TYPE_T61_STRING => Primitive\T61String::class,
80
+		self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class,
81
+		self::TYPE_IA5_STRING => Primitive\IA5String::class,
82
+		self::TYPE_UTC_TIME => Primitive\UTCTime::class,
83
+		self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class,
84
+		self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class,
85
+		self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class,
86
+		self::TYPE_GENERAL_STRING => Primitive\GeneralString::class,
87
+		self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class,
88
+		self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class,
89
+		self::TYPE_BMP_STRING => Primitive\BMPString::class
90
+		/* @formatter:on */
91
+	];
92 92
     
93
-    /**
94
-     * Pseudotype for all string types.
95
-     *
96
-     * May be used as an expectation parameter.
97
-     *
98
-     * @var int
99
-     */
100
-    const TYPE_STRING = -1;
93
+	/**
94
+	 * Pseudotype for all string types.
95
+	 *
96
+	 * May be used as an expectation parameter.
97
+	 *
98
+	 * @var int
99
+	 */
100
+	const TYPE_STRING = -1;
101 101
     
102
-    /**
103
-     * Pseudotype for all time types.
104
-     *
105
-     * May be used as an expectation parameter.
106
-     *
107
-     * @var int
108
-     */
109
-    const TYPE_TIME = -2;
102
+	/**
103
+	 * Pseudotype for all time types.
104
+	 *
105
+	 * May be used as an expectation parameter.
106
+	 *
107
+	 * @var int
108
+	 */
109
+	const TYPE_TIME = -2;
110 110
     
111
-    /**
112
-     * Mapping from universal type tag to human readable name.
113
-     *
114
-     * @internal
115
-     *
116
-     * @var array
117
-     */
118
-    const MAP_TYPE_TO_NAME = [ /* @formatter:off */
119
-        self::TYPE_EOC => "EOC",
120
-        self::TYPE_BOOLEAN => "BOOLEAN",
121
-        self::TYPE_INTEGER => "INTEGER",
122
-        self::TYPE_BIT_STRING => "BIT STRING",
123
-        self::TYPE_OCTET_STRING => "OCTET STRING",
124
-        self::TYPE_NULL => "NULL",
125
-        self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER",
126
-        self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor",
127
-        self::TYPE_EXTERNAL => "EXTERNAL",
128
-        self::TYPE_REAL => "REAL",
129
-        self::TYPE_ENUMERATED => "ENUMERATED",
130
-        self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV",
131
-        self::TYPE_UTF8_STRING => "UTF8String",
132
-        self::TYPE_RELATIVE_OID => "RELATIVE-OID",
133
-        self::TYPE_SEQUENCE => "SEQUENCE",
134
-        self::TYPE_SET => "SET",
135
-        self::TYPE_NUMERIC_STRING => "NumericString",
136
-        self::TYPE_PRINTABLE_STRING => "PrintableString",
137
-        self::TYPE_T61_STRING => "T61String",
138
-        self::TYPE_VIDEOTEX_STRING => "VideotexString",
139
-        self::TYPE_IA5_STRING => "IA5String",
140
-        self::TYPE_UTC_TIME => "UTCTime",
141
-        self::TYPE_GENERALIZED_TIME => "GeneralizedTime",
142
-        self::TYPE_GRAPHIC_STRING => "GraphicString",
143
-        self::TYPE_VISIBLE_STRING => "VisibleString",
144
-        self::TYPE_GENERAL_STRING => "GeneralString",
145
-        self::TYPE_UNIVERSAL_STRING => "UniversalString",
146
-        self::TYPE_CHARACTER_STRING => "CHARACTER STRING",
147
-        self::TYPE_BMP_STRING => "BMPString",
148
-        self::TYPE_STRING => "Any String",
149
-        self::TYPE_TIME => "Any Time"
150
-        /* @formatter:on */
151
-    ];
111
+	/**
112
+	 * Mapping from universal type tag to human readable name.
113
+	 *
114
+	 * @internal
115
+	 *
116
+	 * @var array
117
+	 */
118
+	const MAP_TYPE_TO_NAME = [ /* @formatter:off */
119
+		self::TYPE_EOC => "EOC",
120
+		self::TYPE_BOOLEAN => "BOOLEAN",
121
+		self::TYPE_INTEGER => "INTEGER",
122
+		self::TYPE_BIT_STRING => "BIT STRING",
123
+		self::TYPE_OCTET_STRING => "OCTET STRING",
124
+		self::TYPE_NULL => "NULL",
125
+		self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER",
126
+		self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor",
127
+		self::TYPE_EXTERNAL => "EXTERNAL",
128
+		self::TYPE_REAL => "REAL",
129
+		self::TYPE_ENUMERATED => "ENUMERATED",
130
+		self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV",
131
+		self::TYPE_UTF8_STRING => "UTF8String",
132
+		self::TYPE_RELATIVE_OID => "RELATIVE-OID",
133
+		self::TYPE_SEQUENCE => "SEQUENCE",
134
+		self::TYPE_SET => "SET",
135
+		self::TYPE_NUMERIC_STRING => "NumericString",
136
+		self::TYPE_PRINTABLE_STRING => "PrintableString",
137
+		self::TYPE_T61_STRING => "T61String",
138
+		self::TYPE_VIDEOTEX_STRING => "VideotexString",
139
+		self::TYPE_IA5_STRING => "IA5String",
140
+		self::TYPE_UTC_TIME => "UTCTime",
141
+		self::TYPE_GENERALIZED_TIME => "GeneralizedTime",
142
+		self::TYPE_GRAPHIC_STRING => "GraphicString",
143
+		self::TYPE_VISIBLE_STRING => "VisibleString",
144
+		self::TYPE_GENERAL_STRING => "GeneralString",
145
+		self::TYPE_UNIVERSAL_STRING => "UniversalString",
146
+		self::TYPE_CHARACTER_STRING => "CHARACTER STRING",
147
+		self::TYPE_BMP_STRING => "BMPString",
148
+		self::TYPE_STRING => "Any String",
149
+		self::TYPE_TIME => "Any Time"
150
+		/* @formatter:on */
151
+	];
152 152
     
153
-    /**
154
-     * Element's type tag.
155
-     *
156
-     * @var int
157
-     */
158
-    protected $_typeTag;
153
+	/**
154
+	 * Element's type tag.
155
+	 *
156
+	 * @var int
157
+	 */
158
+	protected $_typeTag;
159 159
     
160
-    /**
161
-     * Whether type shall be encoded with indefinite length.
162
-     *
163
-     * @var bool
164
-     */
165
-    protected $_indefiniteLength = false;
160
+	/**
161
+	 * Whether type shall be encoded with indefinite length.
162
+	 *
163
+	 * @var bool
164
+	 */
165
+	protected $_indefiniteLength = false;
166 166
     
167
-    /**
168
-     *
169
-     * @see \ASN1\Feature\ElementBase::typeClass()
170
-     * @return int
171
-     */
172
-    abstract public function typeClass(): int;
167
+	/**
168
+	 *
169
+	 * @see \ASN1\Feature\ElementBase::typeClass()
170
+	 * @return int
171
+	 */
172
+	abstract public function typeClass(): int;
173 173
     
174
-    /**
175
-     *
176
-     * @see \ASN1\Feature\ElementBase::isConstructed()
177
-     * @return bool
178
-     */
179
-    abstract public function isConstructed(): bool;
174
+	/**
175
+	 *
176
+	 * @see \ASN1\Feature\ElementBase::isConstructed()
177
+	 * @return bool
178
+	 */
179
+	abstract public function isConstructed(): bool;
180 180
     
181
-    /**
182
-     * Get the content encoded in DER.
183
-     *
184
-     * Returns the DER encoded content without identifier and length header
185
-     * octets.
186
-     *
187
-     * @return string
188
-     */
189
-    abstract protected function _encodedContentDER(): string;
181
+	/**
182
+	 * Get the content encoded in DER.
183
+	 *
184
+	 * Returns the DER encoded content without identifier and length header
185
+	 * octets.
186
+	 *
187
+	 * @return string
188
+	 */
189
+	abstract protected function _encodedContentDER(): string;
190 190
     
191
-    /**
192
-     * Decode type-specific element from DER.
193
-     *
194
-     * @param Identifier $identifier Pre-parsed identifier
195
-     * @param string $data DER data
196
-     * @param int $offset Offset in data to the next byte after identifier
197
-     * @throws DecodeException If decoding fails
198
-     * @return ElementBase
199
-     */
200
-    protected static function _decodeFromDER(Identifier $identifier,
201
-        string $data, int &$offset): ElementBase
202
-    {
203
-        throw new \BadMethodCallException(
204
-            __METHOD__ . " must be implemented in derived class.");
205
-    }
191
+	/**
192
+	 * Decode type-specific element from DER.
193
+	 *
194
+	 * @param Identifier $identifier Pre-parsed identifier
195
+	 * @param string $data DER data
196
+	 * @param int $offset Offset in data to the next byte after identifier
197
+	 * @throws DecodeException If decoding fails
198
+	 * @return ElementBase
199
+	 */
200
+	protected static function _decodeFromDER(Identifier $identifier,
201
+		string $data, int &$offset): ElementBase
202
+	{
203
+		throw new \BadMethodCallException(
204
+			__METHOD__ . " must be implemented in derived class.");
205
+	}
206 206
     
207
-    /**
208
-     * Decode element from DER data.
209
-     *
210
-     * @param string $data DER encoded data
211
-     * @param int|null $offset Reference to the variable that contains offset
212
-     *        into the data where to start parsing. Variable is updated to
213
-     *        the offset next to the parsed element. If null, start from offset
214
-     *        0.
215
-     * @throws DecodeException If decoding fails
216
-     * @throws \UnexpectedValueException If called in the context of an expected
217
-     *         type, but decoding yields another type
218
-     * @return ElementBase
219
-     */
220
-    public static function fromDER(string $data, int &$offset = null): ElementBase
221
-    {
222
-        $idx = $offset ?? 0;
223
-        // decode identifier
224
-        $identifier = Identifier::fromDER($data, $idx);
225
-        // determine class that implements type specific decoding
226
-        $cls = self::_determineImplClass($identifier);
227
-        try {
228
-            // decode remaining element
229
-            $element = $cls::_decodeFromDER($identifier, $data, $idx);
230
-        } catch (\LogicException $e) {
231
-            // rethrow as a RuntimeException for unified exception handling
232
-            throw new DecodeException(
233
-                sprintf("Error while decoding %s.",
234
-                    self::tagToName($identifier->intTag())), 0, $e);
235
-        }
236
-        // if called in the context of a concrete class, check
237
-        // that decoded type matches the type of a calling class
238
-        $called_class = get_called_class();
239
-        if (self::class != $called_class) {
240
-            if (!$element instanceof $called_class) {
241
-                throw new \UnexpectedValueException(
242
-                    sprintf("%s expected, got %s.", $called_class,
243
-                        get_class($element)));
244
-            }
245
-        }
246
-        // update offset for the caller
247
-        if (isset($offset)) {
248
-            $offset = $idx;
249
-        }
250
-        return $element;
251
-    }
207
+	/**
208
+	 * Decode element from DER data.
209
+	 *
210
+	 * @param string $data DER encoded data
211
+	 * @param int|null $offset Reference to the variable that contains offset
212
+	 *        into the data where to start parsing. Variable is updated to
213
+	 *        the offset next to the parsed element. If null, start from offset
214
+	 *        0.
215
+	 * @throws DecodeException If decoding fails
216
+	 * @throws \UnexpectedValueException If called in the context of an expected
217
+	 *         type, but decoding yields another type
218
+	 * @return ElementBase
219
+	 */
220
+	public static function fromDER(string $data, int &$offset = null): ElementBase
221
+	{
222
+		$idx = $offset ?? 0;
223
+		// decode identifier
224
+		$identifier = Identifier::fromDER($data, $idx);
225
+		// determine class that implements type specific decoding
226
+		$cls = self::_determineImplClass($identifier);
227
+		try {
228
+			// decode remaining element
229
+			$element = $cls::_decodeFromDER($identifier, $data, $idx);
230
+		} catch (\LogicException $e) {
231
+			// rethrow as a RuntimeException for unified exception handling
232
+			throw new DecodeException(
233
+				sprintf("Error while decoding %s.",
234
+					self::tagToName($identifier->intTag())), 0, $e);
235
+		}
236
+		// if called in the context of a concrete class, check
237
+		// that decoded type matches the type of a calling class
238
+		$called_class = get_called_class();
239
+		if (self::class != $called_class) {
240
+			if (!$element instanceof $called_class) {
241
+				throw new \UnexpectedValueException(
242
+					sprintf("%s expected, got %s.", $called_class,
243
+						get_class($element)));
244
+			}
245
+		}
246
+		// update offset for the caller
247
+		if (isset($offset)) {
248
+			$offset = $idx;
249
+		}
250
+		return $element;
251
+	}
252 252
     
253
-    /**
254
-     *
255
-     * @see \ASN1\Feature\Encodable::toDER()
256
-     * @return string
257
-     */
258
-    public function toDER(): string
259
-    {
260
-        $identifier = new Identifier($this->typeClass(),
261
-            $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
262
-            $this->_typeTag);
263
-        $content = $this->_encodedContentDER();
264
-        if ($this->_indefiniteLength) {
265
-            $length = new Length(0, true);
266
-            $eoc = new Primitive\EOC();
267
-            return $identifier->toDER() . $length->toDER() . $content .
268
-                $eoc->toDER();
269
-        } else {
270
-            $length = new Length(strlen($content));
271
-            return $identifier->toDER() . $length->toDER() . $content;
272
-        }
273
-    }
253
+	/**
254
+	 *
255
+	 * @see \ASN1\Feature\Encodable::toDER()
256
+	 * @return string
257
+	 */
258
+	public function toDER(): string
259
+	{
260
+		$identifier = new Identifier($this->typeClass(),
261
+			$this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE,
262
+			$this->_typeTag);
263
+		$content = $this->_encodedContentDER();
264
+		if ($this->_indefiniteLength) {
265
+			$length = new Length(0, true);
266
+			$eoc = new Primitive\EOC();
267
+			return $identifier->toDER() . $length->toDER() . $content .
268
+				$eoc->toDER();
269
+		} else {
270
+			$length = new Length(strlen($content));
271
+			return $identifier->toDER() . $length->toDER() . $content;
272
+		}
273
+	}
274 274
     
275
-    /**
276
-     *
277
-     * @see \ASN1\Feature\ElementBase::tag()
278
-     * @return int
279
-     */
280
-    public function tag(): int
281
-    {
282
-        return $this->_typeTag;
283
-    }
275
+	/**
276
+	 *
277
+	 * @see \ASN1\Feature\ElementBase::tag()
278
+	 * @return int
279
+	 */
280
+	public function tag(): int
281
+	{
282
+		return $this->_typeTag;
283
+	}
284 284
     
285
-    /**
286
-     *
287
-     * @see \ASN1\Feature\ElementBase::isType()
288
-     * @return bool
289
-     */
290
-    public function isType(int $tag): bool
291
-    {
292
-        // if element is context specific
293
-        if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) {
294
-            return false;
295
-        }
296
-        // negative tags identify an abstract pseudotype
297
-        if ($tag < 0) {
298
-            return $this->_isPseudoType($tag);
299
-        }
300
-        return $this->_isConcreteType($tag);
301
-    }
285
+	/**
286
+	 *
287
+	 * @see \ASN1\Feature\ElementBase::isType()
288
+	 * @return bool
289
+	 */
290
+	public function isType(int $tag): bool
291
+	{
292
+		// if element is context specific
293
+		if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) {
294
+			return false;
295
+		}
296
+		// negative tags identify an abstract pseudotype
297
+		if ($tag < 0) {
298
+			return $this->_isPseudoType($tag);
299
+		}
300
+		return $this->_isConcreteType($tag);
301
+	}
302 302
     
303
-    /**
304
-     *
305
-     * @see \ASN1\Feature\ElementBase::expectType()
306
-     * @return ElementBase
307
-     */
308
-    public function expectType(int $tag): ElementBase
309
-    {
310
-        if (!$this->isType($tag)) {
311
-            throw new \UnexpectedValueException(
312
-                sprintf("%s expected, got %s.", self::tagToName($tag),
313
-                    $this->_typeDescriptorString()));
314
-        }
315
-        return $this;
316
-    }
303
+	/**
304
+	 *
305
+	 * @see \ASN1\Feature\ElementBase::expectType()
306
+	 * @return ElementBase
307
+	 */
308
+	public function expectType(int $tag): ElementBase
309
+	{
310
+		if (!$this->isType($tag)) {
311
+			throw new \UnexpectedValueException(
312
+				sprintf("%s expected, got %s.", self::tagToName($tag),
313
+					$this->_typeDescriptorString()));
314
+		}
315
+		return $this;
316
+	}
317 317
     
318
-    /**
319
-     * Check whether the element is a concrete type of a given tag.
320
-     *
321
-     * @param int $tag
322
-     * @return bool
323
-     */
324
-    private function _isConcreteType(int $tag): bool
325
-    {
326
-        // if tag doesn't match
327
-        if ($this->tag() != $tag) {
328
-            return false;
329
-        }
330
-        // if type is universal check that instance is of a correct class
331
-        if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
332
-            $cls = self::_determineUniversalImplClass($tag);
333
-            if (!$this instanceof $cls) {
334
-                return false;
335
-            }
336
-        }
337
-        return true;
338
-    }
318
+	/**
319
+	 * Check whether the element is a concrete type of a given tag.
320
+	 *
321
+	 * @param int $tag
322
+	 * @return bool
323
+	 */
324
+	private function _isConcreteType(int $tag): bool
325
+	{
326
+		// if tag doesn't match
327
+		if ($this->tag() != $tag) {
328
+			return false;
329
+		}
330
+		// if type is universal check that instance is of a correct class
331
+		if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
332
+			$cls = self::_determineUniversalImplClass($tag);
333
+			if (!$this instanceof $cls) {
334
+				return false;
335
+			}
336
+		}
337
+		return true;
338
+	}
339 339
     
340
-    /**
341
-     * Check whether the element is a pseudotype.
342
-     *
343
-     * @param int $tag
344
-     * @return bool
345
-     */
346
-    private function _isPseudoType(int $tag): bool
347
-    {
348
-        switch ($tag) {
349
-            case self::TYPE_STRING:
350
-                return $this instanceof StringType;
351
-            case self::TYPE_TIME:
352
-                return $this instanceof TimeType;
353
-        }
354
-        return false;
355
-    }
340
+	/**
341
+	 * Check whether the element is a pseudotype.
342
+	 *
343
+	 * @param int $tag
344
+	 * @return bool
345
+	 */
346
+	private function _isPseudoType(int $tag): bool
347
+	{
348
+		switch ($tag) {
349
+			case self::TYPE_STRING:
350
+				return $this instanceof StringType;
351
+			case self::TYPE_TIME:
352
+				return $this instanceof TimeType;
353
+		}
354
+		return false;
355
+	}
356 356
     
357
-    /**
358
-     *
359
-     * @see \ASN1\Feature\ElementBase::isTagged()
360
-     * @return bool
361
-     */
362
-    public function isTagged(): bool
363
-    {
364
-        return $this instanceof TaggedType;
365
-    }
357
+	/**
358
+	 *
359
+	 * @see \ASN1\Feature\ElementBase::isTagged()
360
+	 * @return bool
361
+	 */
362
+	public function isTagged(): bool
363
+	{
364
+		return $this instanceof TaggedType;
365
+	}
366 366
     
367
-    /**
368
-     *
369
-     * @see \ASN1\Feature\ElementBase::expectTagged()
370
-     * @return TaggedType
371
-     */
372
-    public function expectTagged($tag = null): TaggedType
373
-    {
374
-        if (!$this->isTagged()) {
375
-            throw new \UnexpectedValueException(
376
-                sprintf("Context specific element expected, got %s.",
377
-                    Identifier::classToName($this->typeClass())));
378
-        }
379
-        if (isset($tag) && $this->tag() != $tag) {
380
-            throw new \UnexpectedValueException(
381
-                sprintf("Tag %d expected, got %d.", $tag, $this->tag()));
382
-        }
383
-        return $this;
384
-    }
367
+	/**
368
+	 *
369
+	 * @see \ASN1\Feature\ElementBase::expectTagged()
370
+	 * @return TaggedType
371
+	 */
372
+	public function expectTagged($tag = null): TaggedType
373
+	{
374
+		if (!$this->isTagged()) {
375
+			throw new \UnexpectedValueException(
376
+				sprintf("Context specific element expected, got %s.",
377
+					Identifier::classToName($this->typeClass())));
378
+		}
379
+		if (isset($tag) && $this->tag() != $tag) {
380
+			throw new \UnexpectedValueException(
381
+				sprintf("Tag %d expected, got %d.", $tag, $this->tag()));
382
+		}
383
+		return $this;
384
+	}
385 385
     
386
-    /**
387
-     * Whether element has indefinite length.
388
-     *
389
-     * @return bool
390
-     */
391
-    public function hasIndefiniteLength(): bool
392
-    {
393
-        return $this->_indefiniteLength;
394
-    }
386
+	/**
387
+	 * Whether element has indefinite length.
388
+	 *
389
+	 * @return bool
390
+	 */
391
+	public function hasIndefiniteLength(): bool
392
+	{
393
+		return $this->_indefiniteLength;
394
+	}
395 395
     
396
-    /**
397
-     * Get self with indefinite length encoding set.
398
-     *
399
-     * @param bool $indefinite True for indefinite length, false for definite
400
-     *        length
401
-     * @return self
402
-     */
403
-    public function withIndefiniteLength(bool $indefinite = true): self
404
-    {
405
-        $obj = clone $this;
406
-        $obj->_indefiniteLength = $indefinite;
407
-        return $obj;
408
-    }
396
+	/**
397
+	 * Get self with indefinite length encoding set.
398
+	 *
399
+	 * @param bool $indefinite True for indefinite length, false for definite
400
+	 *        length
401
+	 * @return self
402
+	 */
403
+	public function withIndefiniteLength(bool $indefinite = true): self
404
+	{
405
+		$obj = clone $this;
406
+		$obj->_indefiniteLength = $indefinite;
407
+		return $obj;
408
+	}
409 409
     
410
-    /**
411
-     *
412
-     * @see \ASN1\Feature\ElementBase::asElement()
413
-     * @return Element
414
-     */
415
-    final public function asElement(): Element
416
-    {
417
-        return $this;
418
-    }
410
+	/**
411
+	 *
412
+	 * @see \ASN1\Feature\ElementBase::asElement()
413
+	 * @return Element
414
+	 */
415
+	final public function asElement(): Element
416
+	{
417
+		return $this;
418
+	}
419 419
     
420
-    /**
421
-     * Get element decorated with UnspecifiedType object.
422
-     *
423
-     * @return UnspecifiedType
424
-     */
425
-    public function asUnspecified(): UnspecifiedType
426
-    {
427
-        return new UnspecifiedType($this);
428
-    }
420
+	/**
421
+	 * Get element decorated with UnspecifiedType object.
422
+	 *
423
+	 * @return UnspecifiedType
424
+	 */
425
+	public function asUnspecified(): UnspecifiedType
426
+	{
427
+		return new UnspecifiedType($this);
428
+	}
429 429
     
430
-    /**
431
-     * Determine the class that implements the type.
432
-     *
433
-     * @param Identifier $identifier
434
-     * @return string Class name
435
-     */
436
-    protected static function _determineImplClass(Identifier $identifier): string
437
-    {
438
-        switch ($identifier->typeClass()) {
439
-            case Identifier::CLASS_UNIVERSAL:
440
-                return self::_determineUniversalImplClass($identifier->intTag());
441
-            case Identifier::CLASS_CONTEXT_SPECIFIC:
442
-                return TaggedType::class;
443
-            case Identifier::CLASS_APPLICATION:
444
-                return ApplicationType::class;
445
-            case Identifier::CLASS_PRIVATE:
446
-                return PrivateType::class;
447
-        }
448
-        throw new \UnexpectedValueException(
449
-            sprintf("%s %d not implemented.",
450
-                Identifier::classToName($identifier->typeClass()),
451
-                $identifier->tag()));
452
-    }
430
+	/**
431
+	 * Determine the class that implements the type.
432
+	 *
433
+	 * @param Identifier $identifier
434
+	 * @return string Class name
435
+	 */
436
+	protected static function _determineImplClass(Identifier $identifier): string
437
+	{
438
+		switch ($identifier->typeClass()) {
439
+			case Identifier::CLASS_UNIVERSAL:
440
+				return self::_determineUniversalImplClass($identifier->intTag());
441
+			case Identifier::CLASS_CONTEXT_SPECIFIC:
442
+				return TaggedType::class;
443
+			case Identifier::CLASS_APPLICATION:
444
+				return ApplicationType::class;
445
+			case Identifier::CLASS_PRIVATE:
446
+				return PrivateType::class;
447
+		}
448
+		throw new \UnexpectedValueException(
449
+			sprintf("%s %d not implemented.",
450
+				Identifier::classToName($identifier->typeClass()),
451
+				$identifier->tag()));
452
+	}
453 453
     
454
-    /**
455
-     * Determine the class that implements an universal type of the given tag.
456
-     *
457
-     * @param int $tag
458
-     * @throws \UnexpectedValueException
459
-     * @return string Class name
460
-     */
461
-    protected static function _determineUniversalImplClass(int $tag): string
462
-    {
463
-        if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
464
-            throw new \UnexpectedValueException(
465
-                "Universal tag $tag not implemented.");
466
-        }
467
-        return self::MAP_TAG_TO_CLASS[$tag];
468
-    }
454
+	/**
455
+	 * Determine the class that implements an universal type of the given tag.
456
+	 *
457
+	 * @param int $tag
458
+	 * @throws \UnexpectedValueException
459
+	 * @return string Class name
460
+	 */
461
+	protected static function _determineUniversalImplClass(int $tag): string
462
+	{
463
+		if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
464
+			throw new \UnexpectedValueException(
465
+				"Universal tag $tag not implemented.");
466
+		}
467
+		return self::MAP_TAG_TO_CLASS[$tag];
468
+	}
469 469
     
470
-    /**
471
-     * Get textual description of the type for debugging purposes.
472
-     *
473
-     * @return string
474
-     */
475
-    protected function _typeDescriptorString(): string
476
-    {
477
-        if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
478
-            return self::tagToName($this->_typeTag);
479
-        }
480
-        return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()),
481
-            $this->_typeTag);
482
-    }
470
+	/**
471
+	 * Get textual description of the type for debugging purposes.
472
+	 *
473
+	 * @return string
474
+	 */
475
+	protected function _typeDescriptorString(): string
476
+	{
477
+		if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) {
478
+			return self::tagToName($this->_typeTag);
479
+		}
480
+		return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()),
481
+			$this->_typeTag);
482
+	}
483 483
     
484
-    /**
485
-     * Get human readable name for an universal tag.
486
-     *
487
-     * @param int $tag
488
-     * @return string
489
-     */
490
-    public static function tagToName(int $tag): string
491
-    {
492
-        if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
493
-            return "TAG $tag";
494
-        }
495
-        return self::MAP_TYPE_TO_NAME[$tag];
496
-    }
484
+	/**
485
+	 * Get human readable name for an universal tag.
486
+	 *
487
+	 * @param int $tag
488
+	 * @return string
489
+	 */
490
+	public static function tagToName(int $tag): string
491
+	{
492
+		if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) {
493
+			return "TAG $tag";
494
+		}
495
+		return self::MAP_TYPE_TO_NAME[$tag];
496
+	}
497 497
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types = 1);
2
+declare(strict_types=1);
3 3
 
4 4
 namespace ASN1;
5 5
 
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
      *
60 60
      * @var array
61 61
      */
62
-    const MAP_TAG_TO_CLASS = [ /* @formatter:off */
62
+    const MAP_TAG_TO_CLASS = [/* @formatter:off */
63 63
         self::TYPE_EOC => Primitive\EOC::class,
64 64
         self::TYPE_BOOLEAN => Primitive\Boolean::class,
65 65
         self::TYPE_INTEGER => Primitive\Integer::class,
@@ -115,7 +115,7 @@  discard block
 block discarded – undo
115 115
      *
116 116
      * @var array
117 117
      */
118
-    const MAP_TYPE_TO_NAME = [ /* @formatter:off */
118
+    const MAP_TYPE_TO_NAME = [/* @formatter:off */
119 119
         self::TYPE_EOC => "EOC",
120 120
         self::TYPE_BOOLEAN => "BOOLEAN",
121 121
         self::TYPE_INTEGER => "INTEGER",
Please login to merge, or discard this patch.
lib/ASN1/Feature/ElementBase.php 2 patches
Indentation   +74 added lines, -74 removed lines patch added patch discarded remove patch
@@ -13,86 +13,86 @@
 block discarded – undo
13 13
  */
14 14
 interface ElementBase extends Encodable
15 15
 {
16
-    /**
17
-     * Get the class of the ASN.1 type.
18
-     *
19
-     * One of <code>Identifier::CLASS_*</code> constants.
20
-     *
21
-     * @return int
22
-     */
23
-    public function typeClass(): int;
16
+	/**
17
+	 * Get the class of the ASN.1 type.
18
+	 *
19
+	 * One of <code>Identifier::CLASS_*</code> constants.
20
+	 *
21
+	 * @return int
22
+	 */
23
+	public function typeClass(): int;
24 24
     
25
-    /**
26
-     * Check whether the element is constructed.
27
-     *
28
-     * Otherwise it's primitive.
29
-     *
30
-     * @return bool
31
-     */
32
-    public function isConstructed(): bool;
25
+	/**
26
+	 * Check whether the element is constructed.
27
+	 *
28
+	 * Otherwise it's primitive.
29
+	 *
30
+	 * @return bool
31
+	 */
32
+	public function isConstructed(): bool;
33 33
     
34
-    /**
35
-     * Get the tag of the element.
36
-     *
37
-     * Interpretation of the tag depends on the context. For example it may
38
-     * represent a universal type tag or a tag of an implicitly or explicitly
39
-     * tagged type.
40
-     *
41
-     * @return int
42
-     */
43
-    public function tag(): int;
34
+	/**
35
+	 * Get the tag of the element.
36
+	 *
37
+	 * Interpretation of the tag depends on the context. For example it may
38
+	 * represent a universal type tag or a tag of an implicitly or explicitly
39
+	 * tagged type.
40
+	 *
41
+	 * @return int
42
+	 */
43
+	public function tag(): int;
44 44
     
45
-    /**
46
-     * Check whether the element is a type of a given tag.
47
-     *
48
-     * @param int $tag Type tag
49
-     * @return bool
50
-     */
51
-    public function isType(int $tag): bool;
45
+	/**
46
+	 * Check whether the element is a type of a given tag.
47
+	 *
48
+	 * @param int $tag Type tag
49
+	 * @return bool
50
+	 */
51
+	public function isType(int $tag): bool;
52 52
     
53
-    /**
54
-     * Check whether the element is a type of a given tag.
55
-     *
56
-     * Throws an exception if expectation fails.
57
-     *
58
-     * @param int $tag Type tag
59
-     * @throws \UnexpectedValueException If the element type differs from the
60
-     *         expected
61
-     * @return ElementBase
62
-     */
63
-    public function expectType(int $tag): ElementBase;
53
+	/**
54
+	 * Check whether the element is a type of a given tag.
55
+	 *
56
+	 * Throws an exception if expectation fails.
57
+	 *
58
+	 * @param int $tag Type tag
59
+	 * @throws \UnexpectedValueException If the element type differs from the
60
+	 *         expected
61
+	 * @return ElementBase
62
+	 */
63
+	public function expectType(int $tag): ElementBase;
64 64
     
65
-    /**
66
-     * Check whether the element is tagged (context specific).
67
-     *
68
-     * @return bool
69
-     */
70
-    public function isTagged(): bool;
65
+	/**
66
+	 * Check whether the element is tagged (context specific).
67
+	 *
68
+	 * @return bool
69
+	 */
70
+	public function isTagged(): bool;
71 71
     
72
-    /**
73
-     * Check whether the element is tagged (context specific) and optionally has
74
-     * a given tag.
75
-     *
76
-     * Throws an exception if the element is not tagged or tag differs from
77
-     * the expected.
78
-     *
79
-     * @param int|null $tag Optional type tag
80
-     * @throws \UnexpectedValueException If expectation fails
81
-     * @return \ASN1\Type\TaggedType
82
-     */
83
-    public function expectTagged($tag = null): TaggedType;
72
+	/**
73
+	 * Check whether the element is tagged (context specific) and optionally has
74
+	 * a given tag.
75
+	 *
76
+	 * Throws an exception if the element is not tagged or tag differs from
77
+	 * the expected.
78
+	 *
79
+	 * @param int|null $tag Optional type tag
80
+	 * @throws \UnexpectedValueException If expectation fails
81
+	 * @return \ASN1\Type\TaggedType
82
+	 */
83
+	public function expectTagged($tag = null): TaggedType;
84 84
     
85
-    /**
86
-     * Get the object as an abstract Element instance.
87
-     *
88
-     * @return \ASN1\Element
89
-     */
90
-    public function asElement(): Element;
85
+	/**
86
+	 * Get the object as an abstract Element instance.
87
+	 *
88
+	 * @return \ASN1\Element
89
+	 */
90
+	public function asElement(): Element;
91 91
     
92
-    /**
93
-     * Get the object as an UnspecifiedType instance.
94
-     *
95
-     * @return UnspecifiedType
96
-     */
97
-    public function asUnspecified(): UnspecifiedType;
92
+	/**
93
+	 * Get the object as an UnspecifiedType instance.
94
+	 *
95
+	 * @return UnspecifiedType
96
+	 */
97
+	public function asUnspecified(): UnspecifiedType;
98 98
 }
Please login to merge, or discard this patch.
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 ASN1\Feature;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Exception/DecodeException.php 1 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 ASN1\Exception;
6 6
 
Please login to merge, or discard this patch.
lib/ASN1/Component/Identifier.php 2 patches
Spacing   +2 added lines, -2 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 ASN1\Component;
6 6
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
      *
27 27
      * @var array
28 28
      */
29
-    const MAP_CLASS_TO_NAME = [ /* @formatter:off */
29
+    const MAP_CLASS_TO_NAME = [/* @formatter:off */
30 30
         self::CLASS_UNIVERSAL => "UNIVERSAL", 
31 31
         self::CLASS_APPLICATION => "APPLICATION", 
32 32
         self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", 
Please login to merge, or discard this patch.
Indentation   +269 added lines, -269 removed lines patch added patch discarded remove patch
@@ -12,295 +12,295 @@
 block discarded – undo
12 12
  */
13 13
 class Identifier implements Encodable
14 14
 {
15
-    // Type class enumerations
16
-    const CLASS_UNIVERSAL = 0b00;
17
-    const CLASS_APPLICATION = 0b01;
18
-    const CLASS_CONTEXT_SPECIFIC = 0b10;
19
-    const CLASS_PRIVATE = 0b11;
15
+	// Type class enumerations
16
+	const CLASS_UNIVERSAL = 0b00;
17
+	const CLASS_APPLICATION = 0b01;
18
+	const CLASS_CONTEXT_SPECIFIC = 0b10;
19
+	const CLASS_PRIVATE = 0b11;
20 20
     
21
-    /**
22
-     * Mapping from type class to human readable name.
23
-     *
24
-     * @internal
25
-     *
26
-     * @var array
27
-     */
28
-    const MAP_CLASS_TO_NAME = [ /* @formatter:off */
29
-        self::CLASS_UNIVERSAL => "UNIVERSAL", 
30
-        self::CLASS_APPLICATION => "APPLICATION", 
31
-        self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", 
32
-        self::CLASS_PRIVATE => "PRIVATE",
33
-        /* @formatter:on */
34
-    ];
21
+	/**
22
+	 * Mapping from type class to human readable name.
23
+	 *
24
+	 * @internal
25
+	 *
26
+	 * @var array
27
+	 */
28
+	const MAP_CLASS_TO_NAME = [ /* @formatter:off */
29
+		self::CLASS_UNIVERSAL => "UNIVERSAL", 
30
+		self::CLASS_APPLICATION => "APPLICATION", 
31
+		self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", 
32
+		self::CLASS_PRIVATE => "PRIVATE",
33
+		/* @formatter:on */
34
+	];
35 35
     
36
-    // P/C enumerations
37
-    const PRIMITIVE = 0b0;
38
-    const CONSTRUCTED = 0b1;
36
+	// P/C enumerations
37
+	const PRIMITIVE = 0b0;
38
+	const CONSTRUCTED = 0b1;
39 39
     
40
-    /**
41
-     * Type class.
42
-     *
43
-     * @var int
44
-     */
45
-    private $_class;
40
+	/**
41
+	 * Type class.
42
+	 *
43
+	 * @var int
44
+	 */
45
+	private $_class;
46 46
     
47
-    /**
48
-     * Primitive or Constructed.
49
-     *
50
-     * @var int
51
-     */
52
-    private $_pc;
47
+	/**
48
+	 * Primitive or Constructed.
49
+	 *
50
+	 * @var int
51
+	 */
52
+	private $_pc;
53 53
     
54
-    /**
55
-     * Content type tag.
56
-     *
57
-     * @var BigInt
58
-     */
59
-    private $_tag;
54
+	/**
55
+	 * Content type tag.
56
+	 *
57
+	 * @var BigInt
58
+	 */
59
+	private $_tag;
60 60
     
61
-    /**
62
-     * Constructor.
63
-     *
64
-     * @param int $class Type class
65
-     * @param int $pc Primitive / Constructed
66
-     * @param int|string $tag Type tag number
67
-     */
68
-    public function __construct(int $class, int $pc, $tag)
69
-    {
70
-        $this->_class = 0b11 & $class;
71
-        $this->_pc = 0b1 & $pc;
72
-        $this->_tag = new BigInt($tag);
73
-    }
61
+	/**
62
+	 * Constructor.
63
+	 *
64
+	 * @param int $class Type class
65
+	 * @param int $pc Primitive / Constructed
66
+	 * @param int|string $tag Type tag number
67
+	 */
68
+	public function __construct(int $class, int $pc, $tag)
69
+	{
70
+		$this->_class = 0b11 & $class;
71
+		$this->_pc = 0b1 & $pc;
72
+		$this->_tag = new BigInt($tag);
73
+	}
74 74
     
75
-    /**
76
-     * Decode identifier component from DER data.
77
-     *
78
-     * @param string $data DER encoded data
79
-     * @param int|null $offset Reference to the variable that contains offset
80
-     *        into the data where to start parsing. Variable is updated to
81
-     *        the offset next to the parsed identifier. If null, start from
82
-     *        offset 0.
83
-     * @throws DecodeException If decoding fails
84
-     * @return self
85
-     */
86
-    public static function fromDER(string $data, int &$offset = null): Identifier
87
-    {
88
-        $idx = $offset ?? 0;
89
-        $datalen = strlen($data);
90
-        if ($idx >= $datalen) {
91
-            throw new DecodeException("Invalid offset.");
92
-        }
93
-        $byte = ord($data[$idx++]);
94
-        // bits 8 and 7 (class)
95
-        // 0 = universal, 1 = application, 2 = context-specific, 3 = private
96
-        $class = (0b11000000 & $byte) >> 6;
97
-        // bit 6 (0 = primitive / 1 = constructed)
98
-        $pc = (0b00100000 & $byte) >> 5;
99
-        // bits 5 to 1 (tag number)
100
-        $tag = (0b00011111 & $byte);
101
-        // long-form identifier
102
-        if (0x1f == $tag) {
103
-            $tag = self::_decodeLongFormTag($data, $idx);
104
-        }
105
-        if (isset($offset)) {
106
-            $offset = $idx;
107
-        }
108
-        return new self($class, $pc, $tag);
109
-    }
75
+	/**
76
+	 * Decode identifier component from DER data.
77
+	 *
78
+	 * @param string $data DER encoded data
79
+	 * @param int|null $offset Reference to the variable that contains offset
80
+	 *        into the data where to start parsing. Variable is updated to
81
+	 *        the offset next to the parsed identifier. If null, start from
82
+	 *        offset 0.
83
+	 * @throws DecodeException If decoding fails
84
+	 * @return self
85
+	 */
86
+	public static function fromDER(string $data, int &$offset = null): Identifier
87
+	{
88
+		$idx = $offset ?? 0;
89
+		$datalen = strlen($data);
90
+		if ($idx >= $datalen) {
91
+			throw new DecodeException("Invalid offset.");
92
+		}
93
+		$byte = ord($data[$idx++]);
94
+		// bits 8 and 7 (class)
95
+		// 0 = universal, 1 = application, 2 = context-specific, 3 = private
96
+		$class = (0b11000000 & $byte) >> 6;
97
+		// bit 6 (0 = primitive / 1 = constructed)
98
+		$pc = (0b00100000 & $byte) >> 5;
99
+		// bits 5 to 1 (tag number)
100
+		$tag = (0b00011111 & $byte);
101
+		// long-form identifier
102
+		if (0x1f == $tag) {
103
+			$tag = self::_decodeLongFormTag($data, $idx);
104
+		}
105
+		if (isset($offset)) {
106
+			$offset = $idx;
107
+		}
108
+		return new self($class, $pc, $tag);
109
+	}
110 110
     
111
-    /**
112
-     * Parse long form tag.
113
-     *
114
-     * @param string $data DER data
115
-     * @param int $offset Reference to the variable containing offset to data
116
-     * @throws DecodeException If decoding fails
117
-     * @return string Tag number
118
-     */
119
-    private static function _decodeLongFormTag(string $data, int &$offset): string
120
-    {
121
-        $datalen = strlen($data);
122
-        $tag = gmp_init(0, 10);
123
-        while (true) {
124
-            if ($offset >= $datalen) {
125
-                throw new DecodeException(
126
-                    "Unexpected end of data while decoding" .
127
-                    " long form identifier.");
128
-            }
129
-            $byte = ord($data[$offset++]);
130
-            $tag <<= 7;
131
-            $tag |= 0x7f & $byte;
132
-            // last byte has bit 8 set to zero
133
-            if (!(0x80 & $byte)) {
134
-                break;
135
-            }
136
-        }
137
-        return gmp_strval($tag, 10);
138
-    }
111
+	/**
112
+	 * Parse long form tag.
113
+	 *
114
+	 * @param string $data DER data
115
+	 * @param int $offset Reference to the variable containing offset to data
116
+	 * @throws DecodeException If decoding fails
117
+	 * @return string Tag number
118
+	 */
119
+	private static function _decodeLongFormTag(string $data, int &$offset): string
120
+	{
121
+		$datalen = strlen($data);
122
+		$tag = gmp_init(0, 10);
123
+		while (true) {
124
+			if ($offset >= $datalen) {
125
+				throw new DecodeException(
126
+					"Unexpected end of data while decoding" .
127
+					" long form identifier.");
128
+			}
129
+			$byte = ord($data[$offset++]);
130
+			$tag <<= 7;
131
+			$tag |= 0x7f & $byte;
132
+			// last byte has bit 8 set to zero
133
+			if (!(0x80 & $byte)) {
134
+				break;
135
+			}
136
+		}
137
+		return gmp_strval($tag, 10);
138
+	}
139 139
     
140
-    /**
141
-     *
142
-     * @see Encodable::toDER()
143
-     * @return string
144
-     */
145
-    public function toDER(): string
146
-    {
147
-        $bytes = [];
148
-        $byte = $this->_class << 6 | $this->_pc << 5;
149
-        $tag = $this->_tag->gmpObj();
150
-        if ($tag < 0x1f) {
151
-            $bytes[] = $byte | $tag;
152
-        } else { // long-form identifier
153
-            $bytes[] = $byte | 0x1f;
154
-            $octets = [];
155
-            for (; $tag > 0; $tag >>= 7) {
156
-                array_push($octets, gmp_intval(0x80 | ($tag & 0x7f)));
157
-            }
158
-            // last octet has bit 8 set to zero
159
-            $octets[0] &= 0x7f;
160
-            foreach (array_reverse($octets) as $octet) {
161
-                $bytes[] = $octet;
162
-            }
163
-        }
164
-        return pack("C*", ...$bytes);
165
-    }
140
+	/**
141
+	 *
142
+	 * @see Encodable::toDER()
143
+	 * @return string
144
+	 */
145
+	public function toDER(): string
146
+	{
147
+		$bytes = [];
148
+		$byte = $this->_class << 6 | $this->_pc << 5;
149
+		$tag = $this->_tag->gmpObj();
150
+		if ($tag < 0x1f) {
151
+			$bytes[] = $byte | $tag;
152
+		} else { // long-form identifier
153
+			$bytes[] = $byte | 0x1f;
154
+			$octets = [];
155
+			for (; $tag > 0; $tag >>= 7) {
156
+				array_push($octets, gmp_intval(0x80 | ($tag & 0x7f)));
157
+			}
158
+			// last octet has bit 8 set to zero
159
+			$octets[0] &= 0x7f;
160
+			foreach (array_reverse($octets) as $octet) {
161
+				$bytes[] = $octet;
162
+			}
163
+		}
164
+		return pack("C*", ...$bytes);
165
+	}
166 166
     
167
-    /**
168
-     * Get class of the type.
169
-     *
170
-     * @return int
171
-     */
172
-    public function typeClass(): int
173
-    {
174
-        return $this->_class;
175
-    }
167
+	/**
168
+	 * Get class of the type.
169
+	 *
170
+	 * @return int
171
+	 */
172
+	public function typeClass(): int
173
+	{
174
+		return $this->_class;
175
+	}
176 176
     
177
-    /**
178
-     * Get P/C.
179
-     *
180
-     * @return int
181
-     */
182
-    public function pc(): int
183
-    {
184
-        return $this->_pc;
185
-    }
177
+	/**
178
+	 * Get P/C.
179
+	 *
180
+	 * @return int
181
+	 */
182
+	public function pc(): int
183
+	{
184
+		return $this->_pc;
185
+	}
186 186
     
187
-    /**
188
-     * Get the tag number.
189
-     *
190
-     * @return string Base 10 integer string
191
-     */
192
-    public function tag(): string
193
-    {
194
-        return $this->_tag->base10();
195
-    }
187
+	/**
188
+	 * Get the tag number.
189
+	 *
190
+	 * @return string Base 10 integer string
191
+	 */
192
+	public function tag(): string
193
+	{
194
+		return $this->_tag->base10();
195
+	}
196 196
     
197
-    /**
198
-     * Get the tag as an integer.
199
-     *
200
-     * @return int
201
-     */
202
-    public function intTag(): int
203
-    {
204
-        return $this->_tag->intVal();
205
-    }
197
+	/**
198
+	 * Get the tag as an integer.
199
+	 *
200
+	 * @return int
201
+	 */
202
+	public function intTag(): int
203
+	{
204
+		return $this->_tag->intVal();
205
+	}
206 206
     
207
-    /**
208
-     * Check whether type is of an universal class.
209
-     *
210
-     * @return bool
211
-     */
212
-    public function isUniversal(): bool
213
-    {
214
-        return self::CLASS_UNIVERSAL == $this->_class;
215
-    }
207
+	/**
208
+	 * Check whether type is of an universal class.
209
+	 *
210
+	 * @return bool
211
+	 */
212
+	public function isUniversal(): bool
213
+	{
214
+		return self::CLASS_UNIVERSAL == $this->_class;
215
+	}
216 216
     
217
-    /**
218
-     * Check whether type is of an application class.
219
-     *
220
-     * @return bool
221
-     */
222
-    public function isApplication(): bool
223
-    {
224
-        return self::CLASS_APPLICATION == $this->_class;
225
-    }
217
+	/**
218
+	 * Check whether type is of an application class.
219
+	 *
220
+	 * @return bool
221
+	 */
222
+	public function isApplication(): bool
223
+	{
224
+		return self::CLASS_APPLICATION == $this->_class;
225
+	}
226 226
     
227
-    /**
228
-     * Check whether type is of a context specific class.
229
-     *
230
-     * @return bool
231
-     */
232
-    public function isContextSpecific(): bool
233
-    {
234
-        return self::CLASS_CONTEXT_SPECIFIC == $this->_class;
235
-    }
227
+	/**
228
+	 * Check whether type is of a context specific class.
229
+	 *
230
+	 * @return bool
231
+	 */
232
+	public function isContextSpecific(): bool
233
+	{
234
+		return self::CLASS_CONTEXT_SPECIFIC == $this->_class;
235
+	}
236 236
     
237
-    /**
238
-     * Check whether type is of a private class.
239
-     *
240
-     * @return bool
241
-     */
242
-    public function isPrivate(): bool
243
-    {
244
-        return self::CLASS_PRIVATE == $this->_class;
245
-    }
237
+	/**
238
+	 * Check whether type is of a private class.
239
+	 *
240
+	 * @return bool
241
+	 */
242
+	public function isPrivate(): bool
243
+	{
244
+		return self::CLASS_PRIVATE == $this->_class;
245
+	}
246 246
     
247
-    /**
248
-     * Check whether content is primitive type.
249
-     *
250
-     * @return bool
251
-     */
252
-    public function isPrimitive(): bool
253
-    {
254
-        return self::PRIMITIVE == $this->_pc;
255
-    }
247
+	/**
248
+	 * Check whether content is primitive type.
249
+	 *
250
+	 * @return bool
251
+	 */
252
+	public function isPrimitive(): bool
253
+	{
254
+		return self::PRIMITIVE == $this->_pc;
255
+	}
256 256
     
257
-    /**
258
-     * Check hether content is constructed type.
259
-     *
260
-     * @return bool
261
-     */
262
-    public function isConstructed(): bool
263
-    {
264
-        return self::CONSTRUCTED == $this->_pc;
265
-    }
257
+	/**
258
+	 * Check hether content is constructed type.
259
+	 *
260
+	 * @return bool
261
+	 */
262
+	public function isConstructed(): bool
263
+	{
264
+		return self::CONSTRUCTED == $this->_pc;
265
+	}
266 266
     
267
-    /**
268
-     * Get self with given type class.
269
-     *
270
-     * @param int $class One of <code>CLASS_*</code> enumerations
271
-     * @return self
272
-     */
273
-    public function withClass(int $class): Identifier
274
-    {
275
-        $obj = clone $this;
276
-        $obj->_class = 0b11 & $class;
277
-        return $obj;
278
-    }
267
+	/**
268
+	 * Get self with given type class.
269
+	 *
270
+	 * @param int $class One of <code>CLASS_*</code> enumerations
271
+	 * @return self
272
+	 */
273
+	public function withClass(int $class): Identifier
274
+	{
275
+		$obj = clone $this;
276
+		$obj->_class = 0b11 & $class;
277
+		return $obj;
278
+	}
279 279
     
280
-    /**
281
-     * Get self with given type tag.
282
-     *
283
-     * @param int|string $tag Tag number
284
-     * @return self
285
-     */
286
-    public function withTag($tag): Identifier
287
-    {
288
-        $obj = clone $this;
289
-        $obj->_tag = new BigInt($tag);
290
-        return $obj;
291
-    }
280
+	/**
281
+	 * Get self with given type tag.
282
+	 *
283
+	 * @param int|string $tag Tag number
284
+	 * @return self
285
+	 */
286
+	public function withTag($tag): Identifier
287
+	{
288
+		$obj = clone $this;
289
+		$obj->_tag = new BigInt($tag);
290
+		return $obj;
291
+	}
292 292
     
293
-    /**
294
-     * Get human readable name of the type class.
295
-     *
296
-     * @param int $class
297
-     * @return string
298
-     */
299
-    public static function classToName(int $class): string
300
-    {
301
-        if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) {
302
-            return "CLASS $class";
303
-        }
304
-        return self::MAP_CLASS_TO_NAME[$class];
305
-    }
293
+	/**
294
+	 * Get human readable name of the type class.
295
+	 *
296
+	 * @param int $class
297
+	 * @return string
298
+	 */
299
+	public static function classToName(int $class): string
300
+	{
301
+		if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) {
302
+			return "CLASS $class";
303
+		}
304
+		return self::MAP_CLASS_TO_NAME[$class];
305
+	}
306 306
 }
Please login to merge, or discard this patch.
lib/ASN1/Component/Length.php 2 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 ASN1\Component;
6 6
 
Please login to merge, or discard this patch.
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -12,214 +12,214 @@
 block discarded – undo
12 12
  */
13 13
 class Length implements Encodable
14 14
 {
15
-    /**
16
-     * Length.
17
-     *
18
-     * @var BigInt
19
-     */
20
-    private $_length;
15
+	/**
16
+	 * Length.
17
+	 *
18
+	 * @var BigInt
19
+	 */
20
+	private $_length;
21 21
     
22
-    /**
23
-     * Whether length is indefinite.
24
-     *
25
-     * @var bool
26
-     */
27
-    private $_indefinite;
22
+	/**
23
+	 * Whether length is indefinite.
24
+	 *
25
+	 * @var bool
26
+	 */
27
+	private $_indefinite;
28 28
     
29
-    /**
30
-     * Constructor.
31
-     *
32
-     * @param int|string $length Length
33
-     * @param bool $indefinite Whether length is indefinite
34
-     */
35
-    public function __construct($length, bool $indefinite = false)
36
-    {
37
-        $this->_length = new BigInt($length);
38
-        $this->_indefinite = $indefinite;
39
-    }
29
+	/**
30
+	 * Constructor.
31
+	 *
32
+	 * @param int|string $length Length
33
+	 * @param bool $indefinite Whether length is indefinite
34
+	 */
35
+	public function __construct($length, bool $indefinite = false)
36
+	{
37
+		$this->_length = new BigInt($length);
38
+		$this->_indefinite = $indefinite;
39
+	}
40 40
     
41
-    /**
42
-     * Decode length component from DER data.
43
-     *
44
-     * @param string $data DER encoded data
45
-     * @param int|null $offset Reference to the variable that contains offset
46
-     *        into the data where to start parsing. Variable is updated to
47
-     *        the offset next to the parsed length component. If null, start
48
-     *        from offset 0.
49
-     * @throws DecodeException If decoding fails
50
-     * @return self
51
-     */
52
-    public static function fromDER(string $data, int &$offset = null): self
53
-    {
54
-        $idx = $offset ?? 0;
55
-        $datalen = strlen($data);
56
-        if ($idx >= $datalen) {
57
-            throw new DecodeException(
58
-                "Unexpected end of data while decoding length.");
59
-        }
60
-        $indefinite = false;
61
-        $byte = ord($data[$idx++]);
62
-        // bits 7 to 1
63
-        $length = (0x7f & $byte);
64
-        // long form
65
-        if (0x80 & $byte) {
66
-            if (!$length) {
67
-                $indefinite = true;
68
-            } else {
69
-                if ($idx + $length > $datalen) {
70
-                    throw new DecodeException(
71
-                        "Unexpected end of data while decoding long form length.");
72
-                }
73
-                $length = self::_decodeLongFormLength($length, $data, $idx);
74
-            }
75
-        }
76
-        if (isset($offset)) {
77
-            $offset = $idx;
78
-        }
79
-        return new self($length, $indefinite);
80
-    }
41
+	/**
42
+	 * Decode length component from DER data.
43
+	 *
44
+	 * @param string $data DER encoded data
45
+	 * @param int|null $offset Reference to the variable that contains offset
46
+	 *        into the data where to start parsing. Variable is updated to
47
+	 *        the offset next to the parsed length component. If null, start
48
+	 *        from offset 0.
49
+	 * @throws DecodeException If decoding fails
50
+	 * @return self
51
+	 */
52
+	public static function fromDER(string $data, int &$offset = null): self
53
+	{
54
+		$idx = $offset ?? 0;
55
+		$datalen = strlen($data);
56
+		if ($idx >= $datalen) {
57
+			throw new DecodeException(
58
+				"Unexpected end of data while decoding length.");
59
+		}
60
+		$indefinite = false;
61
+		$byte = ord($data[$idx++]);
62
+		// bits 7 to 1
63
+		$length = (0x7f & $byte);
64
+		// long form
65
+		if (0x80 & $byte) {
66
+			if (!$length) {
67
+				$indefinite = true;
68
+			} else {
69
+				if ($idx + $length > $datalen) {
70
+					throw new DecodeException(
71
+						"Unexpected end of data while decoding long form length.");
72
+				}
73
+				$length = self::_decodeLongFormLength($length, $data, $idx);
74
+			}
75
+		}
76
+		if (isset($offset)) {
77
+			$offset = $idx;
78
+		}
79
+		return new self($length, $indefinite);
80
+	}
81 81
     
82
-    /**
83
-     * Decode long form length.
84
-     *
85
-     * @param int $length Number of octets
86
-     * @param string $data Data
87
-     * @param int $offset Reference to the variable containing offset to the
88
-     *        data.
89
-     * @throws DecodeException If decoding fails
90
-     * @return string Integer as a string
91
-     */
92
-    private static function _decodeLongFormLength(int $length, string $data,
93
-        int &$offset): string
94
-    {
95
-        // first octet must not be 0xff (spec 8.1.3.5c)
96
-        if ($length == 127) {
97
-            throw new DecodeException("Invalid number of length octets.");
98
-        }
99
-        $num = gmp_init(0, 10);
100
-        while (--$length >= 0) {
101
-            $byte = ord($data[$offset++]);
102
-            $num <<= 8;
103
-            $num |= $byte;
104
-        }
105
-        return gmp_strval($num);
106
-    }
82
+	/**
83
+	 * Decode long form length.
84
+	 *
85
+	 * @param int $length Number of octets
86
+	 * @param string $data Data
87
+	 * @param int $offset Reference to the variable containing offset to the
88
+	 *        data.
89
+	 * @throws DecodeException If decoding fails
90
+	 * @return string Integer as a string
91
+	 */
92
+	private static function _decodeLongFormLength(int $length, string $data,
93
+		int &$offset): string
94
+	{
95
+		// first octet must not be 0xff (spec 8.1.3.5c)
96
+		if ($length == 127) {
97
+			throw new DecodeException("Invalid number of length octets.");
98
+		}
99
+		$num = gmp_init(0, 10);
100
+		while (--$length >= 0) {
101
+			$byte = ord($data[$offset++]);
102
+			$num <<= 8;
103
+			$num |= $byte;
104
+		}
105
+		return gmp_strval($num);
106
+	}
107 107
     
108
-    /**
109
-     * Decode length from DER.
110
-     *
111
-     * Throws an exception if length doesn't match with expected or if data
112
-     * doesn't contain enough bytes.
113
-     *
114
-     * Requirement of definite length is relaxed contrary to the specification
115
-     * (sect. 10.1).
116
-     *
117
-     * @see self::fromDER
118
-     * @param string $data DER data
119
-     * @param int $offset Reference to the offset variable
120
-     * @param int|null $expected Expected length, null to bypass checking
121
-     * @throws DecodeException If decoding or expectation fails
122
-     * @return self
123
-     */
124
-    public static function expectFromDER(string $data, int &$offset,
125
-        int $expected = null): self
126
-    {
127
-        $idx = $offset;
128
-        $length = self::fromDER($data, $idx);
129
-        // if certain length was expected
130
-        if (isset($expected)) {
131
-            if ($length->isIndefinite()) {
132
-                throw new DecodeException('Expected length %d, got indefinite.',
133
-                    $expected);
134
-            }
135
-            if ($expected != $length->intLength()) {
136
-                throw new DecodeException(
137
-                    sprintf("Expected length %d, got %d.", $expected,
138
-                        $length->intLength()));
139
-            }
140
-        }
141
-        // check that enough data is available
142
-        if (!$length->isIndefinite() &&
143
-            strlen($data) < $idx + $length->intLength()) {
144
-            throw new DecodeException(
145
-                sprintf("Length %d overflows data, %d bytes left.",
146
-                    $length->intLength(), strlen($data) - $idx));
147
-        }
148
-        $offset = $idx;
149
-        return $length;
150
-    }
108
+	/**
109
+	 * Decode length from DER.
110
+	 *
111
+	 * Throws an exception if length doesn't match with expected or if data
112
+	 * doesn't contain enough bytes.
113
+	 *
114
+	 * Requirement of definite length is relaxed contrary to the specification
115
+	 * (sect. 10.1).
116
+	 *
117
+	 * @see self::fromDER
118
+	 * @param string $data DER data
119
+	 * @param int $offset Reference to the offset variable
120
+	 * @param int|null $expected Expected length, null to bypass checking
121
+	 * @throws DecodeException If decoding or expectation fails
122
+	 * @return self
123
+	 */
124
+	public static function expectFromDER(string $data, int &$offset,
125
+		int $expected = null): self
126
+	{
127
+		$idx = $offset;
128
+		$length = self::fromDER($data, $idx);
129
+		// if certain length was expected
130
+		if (isset($expected)) {
131
+			if ($length->isIndefinite()) {
132
+				throw new DecodeException('Expected length %d, got indefinite.',
133
+					$expected);
134
+			}
135
+			if ($expected != $length->intLength()) {
136
+				throw new DecodeException(
137
+					sprintf("Expected length %d, got %d.", $expected,
138
+						$length->intLength()));
139
+			}
140
+		}
141
+		// check that enough data is available
142
+		if (!$length->isIndefinite() &&
143
+			strlen($data) < $idx + $length->intLength()) {
144
+			throw new DecodeException(
145
+				sprintf("Length %d overflows data, %d bytes left.",
146
+					$length->intLength(), strlen($data) - $idx));
147
+		}
148
+		$offset = $idx;
149
+		return $length;
150
+	}
151 151
     
152
-    /**
153
-     *
154
-     * @see Encodable::toDER()
155
-     * @throws \DomainException If length is too large to encode
156
-     * @return string
157
-     */
158
-    public function toDER(): string
159
-    {
160
-        $bytes = [];
161
-        if ($this->_indefinite) {
162
-            $bytes[] = 0x80;
163
-        } else {
164
-            $num = $this->_length->gmpObj();
165
-            // long form
166
-            if ($num > 127) {
167
-                $octets = [];
168
-                for (; $num > 0; $num >>= 8) {
169
-                    $octets[] = gmp_intval(0xff & $num);
170
-                }
171
-                $count = count($octets);
172
-                // first octet must not be 0xff
173
-                if ($count >= 127) {
174
-                    throw new \DomainException("Too many length octets.");
175
-                }
176
-                $bytes[] = 0x80 | $count;
177
-                foreach (array_reverse($octets) as $octet) {
178
-                    $bytes[] = $octet;
179
-                }
180
-            } else { // short form
181
-                $bytes[] = gmp_intval($num);
182
-            }
183
-        }
184
-        return pack("C*", ...$bytes);
185
-    }
152
+	/**
153
+	 *
154
+	 * @see Encodable::toDER()
155
+	 * @throws \DomainException If length is too large to encode
156
+	 * @return string
157
+	 */
158
+	public function toDER(): string
159
+	{
160
+		$bytes = [];
161
+		if ($this->_indefinite) {
162
+			$bytes[] = 0x80;
163
+		} else {
164
+			$num = $this->_length->gmpObj();
165
+			// long form
166
+			if ($num > 127) {
167
+				$octets = [];
168
+				for (; $num > 0; $num >>= 8) {
169
+					$octets[] = gmp_intval(0xff & $num);
170
+				}
171
+				$count = count($octets);
172
+				// first octet must not be 0xff
173
+				if ($count >= 127) {
174
+					throw new \DomainException("Too many length octets.");
175
+				}
176
+				$bytes[] = 0x80 | $count;
177
+				foreach (array_reverse($octets) as $octet) {
178
+					$bytes[] = $octet;
179
+				}
180
+			} else { // short form
181
+				$bytes[] = gmp_intval($num);
182
+			}
183
+		}
184
+		return pack("C*", ...$bytes);
185
+	}
186 186
     
187
-    /**
188
-     * Get the length.
189
-     *
190
-     * @throws \LogicException If length is indefinite
191
-     * @return string Length as an integer string
192
-     */
193
-    public function length(): string
194
-    {
195
-        if ($this->_indefinite) {
196
-            throw new \LogicException("Length is indefinite.");
197
-        }
198
-        return $this->_length->base10();
199
-    }
187
+	/**
188
+	 * Get the length.
189
+	 *
190
+	 * @throws \LogicException If length is indefinite
191
+	 * @return string Length as an integer string
192
+	 */
193
+	public function length(): string
194
+	{
195
+		if ($this->_indefinite) {
196
+			throw new \LogicException("Length is indefinite.");
197
+		}
198
+		return $this->_length->base10();
199
+	}
200 200
     
201
-    /**
202
-     * Get the length as an integer.
203
-     *
204
-     * @throws \LogicException If length is indefinite
205
-     * @throws \RuntimeException If length overflows integer size
206
-     * @return int
207
-     */
208
-    public function intLength(): int
209
-    {
210
-        if ($this->_indefinite) {
211
-            throw new \LogicException("Length is indefinite.");
212
-        }
213
-        return $this->_length->intVal();
214
-    }
201
+	/**
202
+	 * Get the length as an integer.
203
+	 *
204
+	 * @throws \LogicException If length is indefinite
205
+	 * @throws \RuntimeException If length overflows integer size
206
+	 * @return int
207
+	 */
208
+	public function intLength(): int
209
+	{
210
+		if ($this->_indefinite) {
211
+			throw new \LogicException("Length is indefinite.");
212
+		}
213
+		return $this->_length->intVal();
214
+	}
215 215
     
216
-    /**
217
-     * Whether length is indefinite.
218
-     *
219
-     * @return bool
220
-     */
221
-    public function isIndefinite(): bool
222
-    {
223
-        return $this->_indefinite;
224
-    }
216
+	/**
217
+	 * Whether length is indefinite.
218
+	 *
219
+	 * @return bool
220
+	 */
221
+	public function isIndefinite(): bool
222
+	{
223
+		return $this->_indefinite;
224
+	}
225 225
 }
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,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types = 1);
2
+declare(strict_types=1);
3 3
 
4 4
 namespace ASN1\Type;
5 5
 
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
     public function __construct(ElementBase ...$elements)
48 48
     {
49 49
         $this->_elements = array_map(
50
-            function (ElementBase $el) {
50
+            function(ElementBase $el) {
51 51
                 return $el->asElement();
52 52
             }, $elements);
53 53
     }
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
     {
239 239
         if (!isset($this->_unspecifiedTypes)) {
240 240
             $this->_unspecifiedTypes = array_map(
241
-                function (Element $el) {
241
+                function(Element $el) {
242 242
                     return new UnspecifiedType($el);
243 243
                 }, $this->_elements);
244 244
         }
Please login to merge, or discard this patch.
Indentation   +368 added lines, -368 removed lines patch added patch discarded remove patch
@@ -13,396 +13,396 @@
 block discarded – undo
13 13
  * Base class for the constructed types.
14 14
  */
15 15
 abstract class Structure extends Element implements 
16
-    \Countable,
17
-    \IteratorAggregate
16
+	\Countable,
17
+	\IteratorAggregate
18 18
 {
19
-    use UniversalClass;
19
+	use UniversalClass;
20 20
     
21
-    /**
22
-     * Array of elements in the structure.
23
-     *
24
-     * @var Element[] $_elements
25
-     */
26
-    protected $_elements;
21
+	/**
22
+	 * Array of elements in the structure.
23
+	 *
24
+	 * @var Element[] $_elements
25
+	 */
26
+	protected $_elements;
27 27
     
28
-    /**
29
-     * Lookup table for the tagged elements.
30
-     *
31
-     * @var TaggedType[]|null $_taggedMap
32
-     */
33
-    private $_taggedMap;
28
+	/**
29
+	 * Lookup table for the tagged elements.
30
+	 *
31
+	 * @var TaggedType[]|null $_taggedMap
32
+	 */
33
+	private $_taggedMap;
34 34
     
35
-    /**
36
-     * Cache variable of elements wrapped into UnspecifiedType objects.
37
-     *
38
-     * @var UnspecifiedType[]|null $_unspecifiedTypes
39
-     */
40
-    private $_unspecifiedTypes;
35
+	/**
36
+	 * Cache variable of elements wrapped into UnspecifiedType objects.
37
+	 *
38
+	 * @var UnspecifiedType[]|null $_unspecifiedTypes
39
+	 */
40
+	private $_unspecifiedTypes;
41 41
     
42
-    /**
43
-     * Constructor.
44
-     *
45
-     * @param ElementBase ...$elements Any number of elements
46
-     */
47
-    public function __construct(ElementBase ...$elements)
48
-    {
49
-        $this->_elements = array_map(
50
-            function (ElementBase $el) {
51
-                return $el->asElement();
52
-            }, $elements);
53
-    }
42
+	/**
43
+	 * Constructor.
44
+	 *
45
+	 * @param ElementBase ...$elements Any number of elements
46
+	 */
47
+	public function __construct(ElementBase ...$elements)
48
+	{
49
+		$this->_elements = array_map(
50
+			function (ElementBase $el) {
51
+				return $el->asElement();
52
+			}, $elements);
53
+	}
54 54
     
55
-    /**
56
-     * Clone magic method.
57
-     */
58
-    public function __clone()
59
-    {
60
-        // clear cache-variables
61
-        $this->_taggedMap = null;
62
-        $this->_unspecifiedTypes = null;
63
-    }
55
+	/**
56
+	 * Clone magic method.
57
+	 */
58
+	public function __clone()
59
+	{
60
+		// clear cache-variables
61
+		$this->_taggedMap = null;
62
+		$this->_unspecifiedTypes = null;
63
+	}
64 64
     
65
-    /**
66
-     *
67
-     * @see \ASN1\Element::isConstructed()
68
-     * @return bool
69
-     */
70
-    public function isConstructed(): bool
71
-    {
72
-        return true;
73
-    }
65
+	/**
66
+	 *
67
+	 * @see \ASN1\Element::isConstructed()
68
+	 * @return bool
69
+	 */
70
+	public function isConstructed(): bool
71
+	{
72
+		return true;
73
+	}
74 74
     
75
-    /**
76
-     *
77
-     * @see \ASN1\Element::_encodedContentDER()
78
-     * @return string
79
-     */
80
-    protected function _encodedContentDER(): string
81
-    {
82
-        $data = "";
83
-        foreach ($this->_elements as $element) {
84
-            $data .= $element->toDER();
85
-        }
86
-        return $data;
87
-    }
75
+	/**
76
+	 *
77
+	 * @see \ASN1\Element::_encodedContentDER()
78
+	 * @return string
79
+	 */
80
+	protected function _encodedContentDER(): string
81
+	{
82
+		$data = "";
83
+		foreach ($this->_elements as $element) {
84
+			$data .= $element->toDER();
85
+		}
86
+		return $data;
87
+	}
88 88
     
89
-    /**
90
-     *
91
-     * {@inheritdoc}
92
-     * @see \ASN1\Element::_decodeFromDER()
93
-     * @return self
94
-     */
95
-    protected static function _decodeFromDER(Identifier $identifier,
96
-        string $data, int &$offset): ElementBase
97
-    {
98
-        if (!$identifier->isConstructed()) {
99
-            throw new DecodeException(
100
-                "Structured element must have constructed bit set.");
101
-        }
102
-        $idx = $offset;
103
-        $length = Length::expectFromDER($data, $idx);
104
-        if ($length->isIndefinite()) {
105
-            $type = self::_decodeIndefiniteLength($data, $idx);
106
-        } else {
107
-            $type = self::_decodeDefiniteLength($data, $idx,
108
-                $length->intLength());
109
-        }
110
-        $offset = $idx;
111
-        return $type;
112
-    }
89
+	/**
90
+	 *
91
+	 * {@inheritdoc}
92
+	 * @see \ASN1\Element::_decodeFromDER()
93
+	 * @return self
94
+	 */
95
+	protected static function _decodeFromDER(Identifier $identifier,
96
+		string $data, int &$offset): ElementBase
97
+	{
98
+		if (!$identifier->isConstructed()) {
99
+			throw new DecodeException(
100
+				"Structured element must have constructed bit set.");
101
+		}
102
+		$idx = $offset;
103
+		$length = Length::expectFromDER($data, $idx);
104
+		if ($length->isIndefinite()) {
105
+			$type = self::_decodeIndefiniteLength($data, $idx);
106
+		} else {
107
+			$type = self::_decodeDefiniteLength($data, $idx,
108
+				$length->intLength());
109
+		}
110
+		$offset = $idx;
111
+		return $type;
112
+	}
113 113
     
114
-    /**
115
-     * Decode elements for a definite length.
116
-     *
117
-     * @param string $data DER data
118
-     * @param int $offset Offset to data
119
-     * @param int $length Number of bytes to decode
120
-     * @throws DecodeException
121
-     * @return ElementBase
122
-     */
123
-    private static function _decodeDefiniteLength(string $data, int &$offset,
124
-        int $length): ElementBase
125
-    {
126
-        $idx = $offset;
127
-        $end = $idx + $length;
128
-        $elements = [];
129
-        while ($idx < $end) {
130
-            $elements[] = Element::fromDER($data, $idx);
131
-            // check that element didn't overflow length
132
-            if ($idx > $end) {
133
-                throw new DecodeException(
134
-                    "Structure's content overflows length.");
135
-            }
136
-        }
137
-        $offset = $idx;
138
-        // return instance by static late binding
139
-        return new static(...$elements);
140
-    }
114
+	/**
115
+	 * Decode elements for a definite length.
116
+	 *
117
+	 * @param string $data DER data
118
+	 * @param int $offset Offset to data
119
+	 * @param int $length Number of bytes to decode
120
+	 * @throws DecodeException
121
+	 * @return ElementBase
122
+	 */
123
+	private static function _decodeDefiniteLength(string $data, int &$offset,
124
+		int $length): ElementBase
125
+	{
126
+		$idx = $offset;
127
+		$end = $idx + $length;
128
+		$elements = [];
129
+		while ($idx < $end) {
130
+			$elements[] = Element::fromDER($data, $idx);
131
+			// check that element didn't overflow length
132
+			if ($idx > $end) {
133
+				throw new DecodeException(
134
+					"Structure's content overflows length.");
135
+			}
136
+		}
137
+		$offset = $idx;
138
+		// return instance by static late binding
139
+		return new static(...$elements);
140
+	}
141 141
     
142
-    /**
143
-     * Decode elements for an indefinite length.
144
-     *
145
-     * @param string $data DER data
146
-     * @param int $offset Offset to data
147
-     * @throws DecodeException
148
-     * @return ElementBase
149
-     */
150
-    private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase
151
-    {
152
-        $idx = $offset;
153
-        $elements = [];
154
-        $end = strlen($data);
155
-        while (true) {
156
-            if ($idx >= $end) {
157
-                throw new DecodeException(
158
-                    'Unexpected end of data while decoding indefinite length structure.');
159
-            }
160
-            $el = Element::fromDER($data, $idx);
161
-            if ($el->isType(self::TYPE_EOC)) {
162
-                break;
163
-            }
164
-            $elements[] = $el;
165
-        }
166
-        $offset = $idx;
167
-        $type = new static(...$elements);
168
-        $type->_indefiniteLength = true;
169
-        return $type;
170
-    }
142
+	/**
143
+	 * Decode elements for an indefinite length.
144
+	 *
145
+	 * @param string $data DER data
146
+	 * @param int $offset Offset to data
147
+	 * @throws DecodeException
148
+	 * @return ElementBase
149
+	 */
150
+	private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase
151
+	{
152
+		$idx = $offset;
153
+		$elements = [];
154
+		$end = strlen($data);
155
+		while (true) {
156
+			if ($idx >= $end) {
157
+				throw new DecodeException(
158
+					'Unexpected end of data while decoding indefinite length structure.');
159
+			}
160
+			$el = Element::fromDER($data, $idx);
161
+			if ($el->isType(self::TYPE_EOC)) {
162
+				break;
163
+			}
164
+			$elements[] = $el;
165
+		}
166
+		$offset = $idx;
167
+		$type = new static(...$elements);
168
+		$type->_indefiniteLength = true;
169
+		return $type;
170
+	}
171 171
     
172
-    /**
173
-     * Explode DER structure to DER encoded components that it contains.
174
-     *
175
-     * @param string $data
176
-     * @throws DecodeException
177
-     * @return string[]
178
-     */
179
-    public static function explodeDER(string $data): array
180
-    {
181
-        $offset = 0;
182
-        $identifier = Identifier::fromDER($data, $offset);
183
-        if (!$identifier->isConstructed()) {
184
-            throw new DecodeException("Element is not constructed.");
185
-        }
186
-        $length = Length::expectFromDER($data, $offset);
187
-        if ($length->isIndefinite()) {
188
-            throw new DecodeException(
189
-                'Explode not implemented for indefinite length encoding.');
190
-        }
191
-        $end = $offset + $length->intLength();
192
-        $parts = [];
193
-        while ($offset < $end) {
194
-            // start of the element
195
-            $idx = $offset;
196
-            // skip identifier
197
-            Identifier::fromDER($data, $offset);
198
-            // decode element length
199
-            $length = Length::expectFromDER($data, $offset)->intLength();
200
-            // extract der encoding of the element
201
-            $parts[] = substr($data, $idx, $offset - $idx + $length);
202
-            // update offset over content
203
-            $offset += $length;
204
-        }
205
-        return $parts;
206
-    }
172
+	/**
173
+	 * Explode DER structure to DER encoded components that it contains.
174
+	 *
175
+	 * @param string $data
176
+	 * @throws DecodeException
177
+	 * @return string[]
178
+	 */
179
+	public static function explodeDER(string $data): array
180
+	{
181
+		$offset = 0;
182
+		$identifier = Identifier::fromDER($data, $offset);
183
+		if (!$identifier->isConstructed()) {
184
+			throw new DecodeException("Element is not constructed.");
185
+		}
186
+		$length = Length::expectFromDER($data, $offset);
187
+		if ($length->isIndefinite()) {
188
+			throw new DecodeException(
189
+				'Explode not implemented for indefinite length encoding.');
190
+		}
191
+		$end = $offset + $length->intLength();
192
+		$parts = [];
193
+		while ($offset < $end) {
194
+			// start of the element
195
+			$idx = $offset;
196
+			// skip identifier
197
+			Identifier::fromDER($data, $offset);
198
+			// decode element length
199
+			$length = Length::expectFromDER($data, $offset)->intLength();
200
+			// extract der encoding of the element
201
+			$parts[] = substr($data, $idx, $offset - $idx + $length);
202
+			// update offset over content
203
+			$offset += $length;
204
+		}
205
+		return $parts;
206
+	}
207 207
     
208
-    /**
209
-     * Get self with an element at the given index replaced by another.
210
-     *
211
-     * @param int $idx Element index
212
-     * @param Element $el New element to insert into the structure
213
-     * @throws \OutOfBoundsException
214
-     * @return self
215
-     */
216
-    public function withReplaced(int $idx, Element $el): self
217
-    {
218
-        if (!isset($this->_elements[$idx])) {
219
-            throw new \OutOfBoundsException(
220
-                "Structure doesn't have element at index $idx.");
221
-        }
222
-        $obj = clone $this;
223
-        $obj->_elements[$idx] = $el;
224
-        return $obj;
225
-    }
208
+	/**
209
+	 * Get self with an element at the given index replaced by another.
210
+	 *
211
+	 * @param int $idx Element index
212
+	 * @param Element $el New element to insert into the structure
213
+	 * @throws \OutOfBoundsException
214
+	 * @return self
215
+	 */
216
+	public function withReplaced(int $idx, Element $el): self
217
+	{
218
+		if (!isset($this->_elements[$idx])) {
219
+			throw new \OutOfBoundsException(
220
+				"Structure doesn't have element at index $idx.");
221
+		}
222
+		$obj = clone $this;
223
+		$obj->_elements[$idx] = $el;
224
+		return $obj;
225
+	}
226 226
     
227
-    /**
228
-     * Get self with an element inserted before the given index.
229
-     *
230
-     * @param int $idx Element index
231
-     * @param Element $el New element to insert into the structure
232
-     * @throws \OutOfBoundsException
233
-     * @return self
234
-     */
235
-    public function withInserted(int $idx, Element $el): self
236
-    {
237
-        if (count($this->_elements) < $idx || $idx < 0) {
238
-            throw new \OutOfBoundsException("Index $idx is out of bounds.");
239
-        }
240
-        $obj = clone $this;
241
-        array_splice($obj->_elements, $idx, 0, [$el]);
242
-        return $obj;
243
-    }
227
+	/**
228
+	 * Get self with an element inserted before the given index.
229
+	 *
230
+	 * @param int $idx Element index
231
+	 * @param Element $el New element to insert into the structure
232
+	 * @throws \OutOfBoundsException
233
+	 * @return self
234
+	 */
235
+	public function withInserted(int $idx, Element $el): self
236
+	{
237
+		if (count($this->_elements) < $idx || $idx < 0) {
238
+			throw new \OutOfBoundsException("Index $idx is out of bounds.");
239
+		}
240
+		$obj = clone $this;
241
+		array_splice($obj->_elements, $idx, 0, [$el]);
242
+		return $obj;
243
+	}
244 244
     
245
-    /**
246
-     * Get self with an element appended to the end.
247
-     *
248
-     * @param Element $el Element to insert into the structure
249
-     * @return self
250
-     */
251
-    public function withAppended(Element $el): self
252
-    {
253
-        $obj = clone $this;
254
-        array_push($obj->_elements, $el);
255
-        return $obj;
256
-    }
245
+	/**
246
+	 * Get self with an element appended to the end.
247
+	 *
248
+	 * @param Element $el Element to insert into the structure
249
+	 * @return self
250
+	 */
251
+	public function withAppended(Element $el): self
252
+	{
253
+		$obj = clone $this;
254
+		array_push($obj->_elements, $el);
255
+		return $obj;
256
+	}
257 257
     
258
-    /**
259
-     * Get self with an element prepended in the beginning.
260
-     *
261
-     * @param Element $el Element to insert into the structure
262
-     * @return self
263
-     */
264
-    public function withPrepended(Element $el): self
265
-    {
266
-        $obj = clone $this;
267
-        array_unshift($obj->_elements, $el);
268
-        return $obj;
269
-    }
258
+	/**
259
+	 * Get self with an element prepended in the beginning.
260
+	 *
261
+	 * @param Element $el Element to insert into the structure
262
+	 * @return self
263
+	 */
264
+	public function withPrepended(Element $el): self
265
+	{
266
+		$obj = clone $this;
267
+		array_unshift($obj->_elements, $el);
268
+		return $obj;
269
+	}
270 270
     
271
-    /**
272
-     * Get self with an element at the given index removed.
273
-     *
274
-     * @param int $idx Element index
275
-     * @throws \OutOfBoundsException
276
-     * @return self
277
-     */
278
-    public function withoutElement(int $idx): self
279
-    {
280
-        if (!isset($this->_elements[$idx])) {
281
-            throw new \OutOfBoundsException(
282
-                "Structure doesn't have element at index $idx.");
283
-        }
284
-        $obj = clone $this;
285
-        array_splice($obj->_elements, $idx, 1);
286
-        return $obj;
287
-    }
271
+	/**
272
+	 * Get self with an element at the given index removed.
273
+	 *
274
+	 * @param int $idx Element index
275
+	 * @throws \OutOfBoundsException
276
+	 * @return self
277
+	 */
278
+	public function withoutElement(int $idx): self
279
+	{
280
+		if (!isset($this->_elements[$idx])) {
281
+			throw new \OutOfBoundsException(
282
+				"Structure doesn't have element at index $idx.");
283
+		}
284
+		$obj = clone $this;
285
+		array_splice($obj->_elements, $idx, 1);
286
+		return $obj;
287
+	}
288 288
     
289
-    /**
290
-     * Get elements in the structure.
291
-     *
292
-     * @return UnspecifiedType[]
293
-     */
294
-    public function elements(): array
295
-    {
296
-        if (!isset($this->_unspecifiedTypes)) {
297
-            $this->_unspecifiedTypes = array_map(
298
-                function (Element $el) {
299
-                    return new UnspecifiedType($el);
300
-                }, $this->_elements);
301
-        }
302
-        return $this->_unspecifiedTypes;
303
-    }
289
+	/**
290
+	 * Get elements in the structure.
291
+	 *
292
+	 * @return UnspecifiedType[]
293
+	 */
294
+	public function elements(): array
295
+	{
296
+		if (!isset($this->_unspecifiedTypes)) {
297
+			$this->_unspecifiedTypes = array_map(
298
+				function (Element $el) {
299
+					return new UnspecifiedType($el);
300
+				}, $this->_elements);
301
+		}
302
+		return $this->_unspecifiedTypes;
303
+	}
304 304
     
305
-    /**
306
-     * Check whether the structure has an element at the given index, optionally
307
-     * satisfying given tag expectation.
308
-     *
309
-     * @param int $idx Index 0..n
310
-     * @param int|null $expectedTag Optional type tag expectation
311
-     * @return bool
312
-     */
313
-    public function has(int $idx, $expectedTag = null): bool
314
-    {
315
-        if (!isset($this->_elements[$idx])) {
316
-            return false;
317
-        }
318
-        if (isset($expectedTag)) {
319
-            if (!$this->_elements[$idx]->isType($expectedTag)) {
320
-                return false;
321
-            }
322
-        }
323
-        return true;
324
-    }
305
+	/**
306
+	 * Check whether the structure has an element at the given index, optionally
307
+	 * satisfying given tag expectation.
308
+	 *
309
+	 * @param int $idx Index 0..n
310
+	 * @param int|null $expectedTag Optional type tag expectation
311
+	 * @return bool
312
+	 */
313
+	public function has(int $idx, $expectedTag = null): bool
314
+	{
315
+		if (!isset($this->_elements[$idx])) {
316
+			return false;
317
+		}
318
+		if (isset($expectedTag)) {
319
+			if (!$this->_elements[$idx]->isType($expectedTag)) {
320
+				return false;
321
+			}
322
+		}
323
+		return true;
324
+	}
325 325
     
326
-    /**
327
-     * Get the element at the given index, optionally checking that the element
328
-     * has a given tag.
329
-     *
330
-     * <strong>NOTE!</strong> Expectation checking is deprecated and should be
331
-     * done with <code>UnspecifiedType</code>.
332
-     *
333
-     * @param int $idx Index 0..n
334
-     * @param int|null $expectedTag Optional type tag expectation
335
-     * @throws \OutOfBoundsException If element doesn't exists
336
-     * @throws \UnexpectedValueException If expectation fails
337
-     * @return UnspecifiedType
338
-     */
339
-    public function at(int $idx, $expectedTag = null): UnspecifiedType
340
-    {
341
-        if (!isset($this->_elements[$idx])) {
342
-            throw new \OutOfBoundsException(
343
-                "Structure doesn't have an element at index $idx.");
344
-        }
345
-        $element = $this->_elements[$idx];
346
-        if (isset($expectedTag)) {
347
-            $element->expectType($expectedTag);
348
-        }
349
-        return new UnspecifiedType($element);
350
-    }
326
+	/**
327
+	 * Get the element at the given index, optionally checking that the element
328
+	 * has a given tag.
329
+	 *
330
+	 * <strong>NOTE!</strong> Expectation checking is deprecated and should be
331
+	 * done with <code>UnspecifiedType</code>.
332
+	 *
333
+	 * @param int $idx Index 0..n
334
+	 * @param int|null $expectedTag Optional type tag expectation
335
+	 * @throws \OutOfBoundsException If element doesn't exists
336
+	 * @throws \UnexpectedValueException If expectation fails
337
+	 * @return UnspecifiedType
338
+	 */
339
+	public function at(int $idx, $expectedTag = null): UnspecifiedType
340
+	{
341
+		if (!isset($this->_elements[$idx])) {
342
+			throw new \OutOfBoundsException(
343
+				"Structure doesn't have an element at index $idx.");
344
+		}
345
+		$element = $this->_elements[$idx];
346
+		if (isset($expectedTag)) {
347
+			$element->expectType($expectedTag);
348
+		}
349
+		return new UnspecifiedType($element);
350
+	}
351 351
     
352
-    /**
353
-     * Check whether the structure contains a context specific element with a
354
-     * given tag.
355
-     *
356
-     * @param int $tag Tag number
357
-     * @return bool
358
-     */
359
-    public function hasTagged(int $tag): bool
360
-    {
361
-        // lazily build lookup map
362
-        if (!isset($this->_taggedMap)) {
363
-            $this->_taggedMap = [];
364
-            foreach ($this->_elements as $element) {
365
-                if ($element->isTagged()) {
366
-                    $this->_taggedMap[$element->tag()] = $element;
367
-                }
368
-            }
369
-        }
370
-        return isset($this->_taggedMap[$tag]);
371
-    }
352
+	/**
353
+	 * Check whether the structure contains a context specific element with a
354
+	 * given tag.
355
+	 *
356
+	 * @param int $tag Tag number
357
+	 * @return bool
358
+	 */
359
+	public function hasTagged(int $tag): bool
360
+	{
361
+		// lazily build lookup map
362
+		if (!isset($this->_taggedMap)) {
363
+			$this->_taggedMap = [];
364
+			foreach ($this->_elements as $element) {
365
+				if ($element->isTagged()) {
366
+					$this->_taggedMap[$element->tag()] = $element;
367
+				}
368
+			}
369
+		}
370
+		return isset($this->_taggedMap[$tag]);
371
+	}
372 372
     
373
-    /**
374
-     * Get a context specific element tagged with a given tag.
375
-     *
376
-     * @param int $tag
377
-     * @throws \LogicException If tag doesn't exists
378
-     * @return TaggedType
379
-     */
380
-    public function getTagged(int $tag): TaggedType
381
-    {
382
-        if (!$this->hasTagged($tag)) {
383
-            throw new \LogicException("No tagged element for tag $tag.");
384
-        }
385
-        return $this->_taggedMap[$tag];
386
-    }
373
+	/**
374
+	 * Get a context specific element tagged with a given tag.
375
+	 *
376
+	 * @param int $tag
377
+	 * @throws \LogicException If tag doesn't exists
378
+	 * @return TaggedType
379
+	 */
380
+	public function getTagged(int $tag): TaggedType
381
+	{
382
+		if (!$this->hasTagged($tag)) {
383
+			throw new \LogicException("No tagged element for tag $tag.");
384
+		}
385
+		return $this->_taggedMap[$tag];
386
+	}
387 387
     
388
-    /**
389
-     *
390
-     * @see \Countable::count()
391
-     * @return int
392
-     */
393
-    public function count(): int
394
-    {
395
-        return count($this->_elements);
396
-    }
388
+	/**
389
+	 *
390
+	 * @see \Countable::count()
391
+	 * @return int
392
+	 */
393
+	public function count(): int
394
+	{
395
+		return count($this->_elements);
396
+	}
397 397
     
398
-    /**
399
-     * Get an iterator for the UnspecifiedElement objects.
400
-     *
401
-     * @see \IteratorAggregate::getIterator()
402
-     * @return \ArrayIterator
403
-     */
404
-    public function getIterator(): \ArrayIterator
405
-    {
406
-        return new \ArrayIterator($this->elements());
407
-    }
398
+	/**
399
+	 * Get an iterator for the UnspecifiedElement objects.
400
+	 *
401
+	 * @see \IteratorAggregate::getIterator()
402
+	 * @return \ArrayIterator
403
+	 */
404
+	public function getIterator(): \ArrayIterator
405
+	{
406
+		return new \ArrayIterator($this->elements());
407
+	}
408 408
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/TaggedType.php 2 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 ASN1\Type;
6 6
 
Please login to merge, or discard this patch.
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -15,81 +15,81 @@
 block discarded – undo
15 15
  */
16 16
 abstract class TaggedType extends Element
17 17
 {
18
-    /**
19
-     *
20
-     * {@inheritdoc}
21
-     */
22
-    protected static function _decodeFromDER(Identifier $identifier,
23
-        string $data, int &$offset): ElementBase
24
-    {
25
-        return DERTaggedType::_decodeFromDER($identifier, $data, $offset);
26
-    }
18
+	/**
19
+	 *
20
+	 * {@inheritdoc}
21
+	 */
22
+	protected static function _decodeFromDER(Identifier $identifier,
23
+		string $data, int &$offset): ElementBase
24
+	{
25
+		return DERTaggedType::_decodeFromDER($identifier, $data, $offset);
26
+	}
27 27
     
28
-    /**
29
-     * Check whether element supports explicit tagging.
30
-     *
31
-     * @param int|null $expectedTag Optional outer tag expectation
32
-     * @throws \UnexpectedValueException If expectation fails
33
-     * @return ExplicitTagging
34
-     */
35
-    public function expectExplicit($expectedTag = null): ExplicitTagging
36
-    {
37
-        $el = $this;
38
-        if (!$el instanceof ExplicitTagging) {
39
-            throw new \UnexpectedValueException(
40
-                "Element doesn't implement explicit tagging.");
41
-        }
42
-        if (isset($expectedTag)) {
43
-            $el->expectTagged($expectedTag);
44
-        }
45
-        return $el;
46
-    }
28
+	/**
29
+	 * Check whether element supports explicit tagging.
30
+	 *
31
+	 * @param int|null $expectedTag Optional outer tag expectation
32
+	 * @throws \UnexpectedValueException If expectation fails
33
+	 * @return ExplicitTagging
34
+	 */
35
+	public function expectExplicit($expectedTag = null): ExplicitTagging
36
+	{
37
+		$el = $this;
38
+		if (!$el instanceof ExplicitTagging) {
39
+			throw new \UnexpectedValueException(
40
+				"Element doesn't implement explicit tagging.");
41
+		}
42
+		if (isset($expectedTag)) {
43
+			$el->expectTagged($expectedTag);
44
+		}
45
+		return $el;
46
+	}
47 47
     
48
-    /**
49
-     * Get the wrapped inner element employing explicit tagging.
50
-     *
51
-     * @param int|null $expectedTag Optional outer tag expectation
52
-     * @throws \UnexpectedValueException If expectation fails
53
-     * @return UnspecifiedType
54
-     */
55
-    public function asExplicit($expectedTag = null): UnspecifiedType
56
-    {
57
-        return $this->expectExplicit($expectedTag)->explicit();
58
-    }
48
+	/**
49
+	 * Get the wrapped inner element employing explicit tagging.
50
+	 *
51
+	 * @param int|null $expectedTag Optional outer tag expectation
52
+	 * @throws \UnexpectedValueException If expectation fails
53
+	 * @return UnspecifiedType
54
+	 */
55
+	public function asExplicit($expectedTag = null): UnspecifiedType
56
+	{
57
+		return $this->expectExplicit($expectedTag)->explicit();
58
+	}
59 59
     
60
-    /**
61
-     * Check whether element supports implicit tagging.
62
-     *
63
-     * @param int|null $expectedTag Optional outer tag expectation
64
-     * @throws \UnexpectedValueException If expectation fails
65
-     * @return ImplicitTagging
66
-     */
67
-    public function expectImplicit($expectedTag = null): ImplicitTagging
68
-    {
69
-        $el = $this;
70
-        if (!$el instanceof ImplicitTagging) {
71
-            throw new \UnexpectedValueException(
72
-                "Element doesn't implement implicit tagging.");
73
-        }
74
-        if (isset($expectedTag)) {
75
-            $el->expectTagged($expectedTag);
76
-        }
77
-        return $el;
78
-    }
60
+	/**
61
+	 * Check whether element supports implicit tagging.
62
+	 *
63
+	 * @param int|null $expectedTag Optional outer tag expectation
64
+	 * @throws \UnexpectedValueException If expectation fails
65
+	 * @return ImplicitTagging
66
+	 */
67
+	public function expectImplicit($expectedTag = null): ImplicitTagging
68
+	{
69
+		$el = $this;
70
+		if (!$el instanceof ImplicitTagging) {
71
+			throw new \UnexpectedValueException(
72
+				"Element doesn't implement implicit tagging.");
73
+		}
74
+		if (isset($expectedTag)) {
75
+			$el->expectTagged($expectedTag);
76
+		}
77
+		return $el;
78
+	}
79 79
     
80
-    /**
81
-     * Get the wrapped inner element employing implicit tagging.
82
-     *
83
-     * @param int $tag Type tag of the inner element
84
-     * @param int|null $expectedTag Optional outer tag expectation
85
-     * @param int $expectedClass Optional inner type class expectation
86
-     * @throws \UnexpectedValueException If expectation fails
87
-     * @return UnspecifiedType
88
-     */
89
-    public function asImplicit(int $tag, $expectedTag = null,
90
-        int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType
91
-    {
92
-        return $this->expectImplicit($expectedTag)->implicit($tag,
93
-            $expectedClass);
94
-    }
80
+	/**
81
+	 * Get the wrapped inner element employing implicit tagging.
82
+	 *
83
+	 * @param int $tag Type tag of the inner element
84
+	 * @param int|null $expectedTag Optional outer tag expectation
85
+	 * @param int $expectedClass Optional inner type class expectation
86
+	 * @throws \UnexpectedValueException If expectation fails
87
+	 * @return UnspecifiedType
88
+	 */
89
+	public function asImplicit(int $tag, $expectedTag = null,
90
+		int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType
91
+	{
92
+		return $this->expectImplicit($expectedTag)->implicit($tag,
93
+			$expectedClass);
94
+	}
95 95
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/ObjectIdentifier.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types = 1);
2
+declare(strict_types=1);
3 3
 
4 4
 namespace ASN1\Type\Primitive;
5 5
 
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
     protected static function _implodeSubIDs(\GMP ...$subids): string
142 142
     {
143 143
         return implode(".",
144
-            array_map(function ($num) {
144
+            array_map(function($num) {
145 145
                 return gmp_strval($num, 10);
146 146
             }, $subids));
147 147
     }
Please login to merge, or discard this patch.
Indentation   +183 added lines, -183 removed lines patch added patch discarded remove patch
@@ -16,197 +16,197 @@
 block discarded – undo
16 16
  */
17 17
 class ObjectIdentifier extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Object identifier in dotted format.
24
-     *
25
-     * @var string
26
-     */
27
-    protected $_oid;
22
+	/**
23
+	 * Object identifier in dotted format.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	protected $_oid;
28 28
     
29
-    /**
30
-     * Object identifier split to sub ID's.
31
-     *
32
-     * @var \GMP[]
33
-     */
34
-    protected $_subids;
29
+	/**
30
+	 * Object identifier split to sub ID's.
31
+	 *
32
+	 * @var \GMP[]
33
+	 */
34
+	protected $_subids;
35 35
     
36
-    /**
37
-     * Constructor.
38
-     *
39
-     * @param string $oid OID in dotted format
40
-     */
41
-    public function __construct(string $oid)
42
-    {
43
-        $this->_oid = $oid;
44
-        $this->_subids = self::_explodeDottedOID($oid);
45
-        // if OID is non-empty
46
-        if (count($this->_subids) > 0) {
47
-            // check that at least two nodes are set
48
-            if (count($this->_subids) < 2) {
49
-                throw new \UnexpectedValueException(
50
-                    "OID must have at least two nodes.");
51
-            }
52
-            // check that root arc is in 0..2 range
53
-            if ($this->_subids[0] > 2) {
54
-                throw new \UnexpectedValueException(
55
-                    "Root arc must be in range of 0..2.");
56
-            }
57
-            // if root arc is 0 or 1, second node must be in 0..39 range
58
-            if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) {
59
-                throw new \UnexpectedValueException(
60
-                    "Second node must be in 0..39 range for root arcs 0 and 1.");
61
-            }
62
-        }
63
-        $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER;
64
-    }
36
+	/**
37
+	 * Constructor.
38
+	 *
39
+	 * @param string $oid OID in dotted format
40
+	 */
41
+	public function __construct(string $oid)
42
+	{
43
+		$this->_oid = $oid;
44
+		$this->_subids = self::_explodeDottedOID($oid);
45
+		// if OID is non-empty
46
+		if (count($this->_subids) > 0) {
47
+			// check that at least two nodes are set
48
+			if (count($this->_subids) < 2) {
49
+				throw new \UnexpectedValueException(
50
+					"OID must have at least two nodes.");
51
+			}
52
+			// check that root arc is in 0..2 range
53
+			if ($this->_subids[0] > 2) {
54
+				throw new \UnexpectedValueException(
55
+					"Root arc must be in range of 0..2.");
56
+			}
57
+			// if root arc is 0 or 1, second node must be in 0..39 range
58
+			if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) {
59
+				throw new \UnexpectedValueException(
60
+					"Second node must be in 0..39 range for root arcs 0 and 1.");
61
+			}
62
+		}
63
+		$this->_typeTag = self::TYPE_OBJECT_IDENTIFIER;
64
+	}
65 65
     
66
-    /**
67
-     * Get OID in dotted format.
68
-     *
69
-     * @return string
70
-     */
71
-    public function oid(): string
72
-    {
73
-        return $this->_oid;
74
-    }
66
+	/**
67
+	 * Get OID in dotted format.
68
+	 *
69
+	 * @return string
70
+	 */
71
+	public function oid(): string
72
+	{
73
+		return $this->_oid;
74
+	}
75 75
     
76
-    /**
77
-     *
78
-     * {@inheritdoc}
79
-     */
80
-    protected function _encodedContentDER(): string
81
-    {
82
-        $subids = $this->_subids;
83
-        // encode first two subids to one according to spec section 8.19.4
84
-        if (count($subids) >= 2) {
85
-            $num = ($subids[0] * 40) + $subids[1];
86
-            array_splice($subids, 0, 2, array($num));
87
-        }
88
-        return self::_encodeSubIDs(...$subids);
89
-    }
76
+	/**
77
+	 *
78
+	 * {@inheritdoc}
79
+	 */
80
+	protected function _encodedContentDER(): string
81
+	{
82
+		$subids = $this->_subids;
83
+		// encode first two subids to one according to spec section 8.19.4
84
+		if (count($subids) >= 2) {
85
+			$num = ($subids[0] * 40) + $subids[1];
86
+			array_splice($subids, 0, 2, array($num));
87
+		}
88
+		return self::_encodeSubIDs(...$subids);
89
+	}
90 90
     
91
-    /**
92
-     *
93
-     * {@inheritdoc}
94
-     * @return self
95
-     */
96
-    protected static function _decodeFromDER(Identifier $identifier,
97
-        string $data, int &$offset): ElementBase
98
-    {
99
-        $idx = $offset;
100
-        $len = Length::expectFromDER($data, $idx)->intLength();
101
-        $subids = self::_decodeSubIDs(substr($data, $idx, $len));
102
-        $idx += $len;
103
-        // decode first subidentifier according to spec section 8.19.4
104
-        if (isset($subids[0])) {
105
-            if ($subids[0] < 80) {
106
-                list($x, $y) = gmp_div_qr($subids[0], "40");
107
-            } else {
108
-                $x = gmp_init(2, 10);
109
-                $y = $subids[0] - 80;
110
-            }
111
-            array_splice($subids, 0, 1, array($x, $y));
112
-        }
113
-        $offset = $idx;
114
-        return new self(self::_implodeSubIDs(...$subids));
115
-    }
91
+	/**
92
+	 *
93
+	 * {@inheritdoc}
94
+	 * @return self
95
+	 */
96
+	protected static function _decodeFromDER(Identifier $identifier,
97
+		string $data, int &$offset): ElementBase
98
+	{
99
+		$idx = $offset;
100
+		$len = Length::expectFromDER($data, $idx)->intLength();
101
+		$subids = self::_decodeSubIDs(substr($data, $idx, $len));
102
+		$idx += $len;
103
+		// decode first subidentifier according to spec section 8.19.4
104
+		if (isset($subids[0])) {
105
+			if ($subids[0] < 80) {
106
+				list($x, $y) = gmp_div_qr($subids[0], "40");
107
+			} else {
108
+				$x = gmp_init(2, 10);
109
+				$y = $subids[0] - 80;
110
+			}
111
+			array_splice($subids, 0, 1, array($x, $y));
112
+		}
113
+		$offset = $idx;
114
+		return new self(self::_implodeSubIDs(...$subids));
115
+	}
116 116
     
117
-    /**
118
-     * Explode dotted OID to an array of sub ID's.
119
-     *
120
-     * @param string $oid OID in dotted format
121
-     * @return \GMP[] Array of GMP numbers
122
-     */
123
-    protected static function _explodeDottedOID(string $oid): array
124
-    {
125
-        $subids = [];
126
-        if (strlen($oid)) {
127
-            foreach (explode(".", $oid) as $subid) {
128
-                $n = @gmp_init($subid, 10);
129
-                if (false === $n) {
130
-                    throw new \UnexpectedValueException(
131
-                        "'$subid' is not a number.");
132
-                }
133
-                $subids[] = $n;
134
-            }
135
-        }
136
-        return $subids;
137
-    }
117
+	/**
118
+	 * Explode dotted OID to an array of sub ID's.
119
+	 *
120
+	 * @param string $oid OID in dotted format
121
+	 * @return \GMP[] Array of GMP numbers
122
+	 */
123
+	protected static function _explodeDottedOID(string $oid): array
124
+	{
125
+		$subids = [];
126
+		if (strlen($oid)) {
127
+			foreach (explode(".", $oid) as $subid) {
128
+				$n = @gmp_init($subid, 10);
129
+				if (false === $n) {
130
+					throw new \UnexpectedValueException(
131
+						"'$subid' is not a number.");
132
+				}
133
+				$subids[] = $n;
134
+			}
135
+		}
136
+		return $subids;
137
+	}
138 138
     
139
-    /**
140
-     * Implode an array of sub IDs to dotted OID format.
141
-     *
142
-     * @param \GMP ...$subids
143
-     * @return string
144
-     */
145
-    protected static function _implodeSubIDs(\GMP ...$subids): string
146
-    {
147
-        return implode(".",
148
-            array_map(function ($num) {
149
-                return gmp_strval($num, 10);
150
-            }, $subids));
151
-    }
139
+	/**
140
+	 * Implode an array of sub IDs to dotted OID format.
141
+	 *
142
+	 * @param \GMP ...$subids
143
+	 * @return string
144
+	 */
145
+	protected static function _implodeSubIDs(\GMP ...$subids): string
146
+	{
147
+		return implode(".",
148
+			array_map(function ($num) {
149
+				return gmp_strval($num, 10);
150
+			}, $subids));
151
+	}
152 152
     
153
-    /**
154
-     * Encode sub ID's to DER.
155
-     *
156
-     * @param \GMP ...$subids
157
-     * @return string
158
-     */
159
-    protected static function _encodeSubIDs(\GMP ...$subids): string
160
-    {
161
-        $data = "";
162
-        foreach ($subids as $subid) {
163
-            // if number fits to one base 128 byte
164
-            if ($subid < 128) {
165
-                $data .= chr(intval($subid));
166
-            } else { // encode to multiple bytes
167
-                $bytes = [];
168
-                do {
169
-                    array_unshift($bytes, 0x7f & gmp_intval($subid));
170
-                    $subid >>= 7;
171
-                } while ($subid > 0);
172
-                // all bytes except last must have bit 8 set to one
173
-                foreach (array_splice($bytes, 0, -1) as $byte) {
174
-                    $data .= chr(0x80 | $byte);
175
-                }
176
-                $data .= chr(reset($bytes));
177
-            }
178
-        }
179
-        return $data;
180
-    }
153
+	/**
154
+	 * Encode sub ID's to DER.
155
+	 *
156
+	 * @param \GMP ...$subids
157
+	 * @return string
158
+	 */
159
+	protected static function _encodeSubIDs(\GMP ...$subids): string
160
+	{
161
+		$data = "";
162
+		foreach ($subids as $subid) {
163
+			// if number fits to one base 128 byte
164
+			if ($subid < 128) {
165
+				$data .= chr(intval($subid));
166
+			} else { // encode to multiple bytes
167
+				$bytes = [];
168
+				do {
169
+					array_unshift($bytes, 0x7f & gmp_intval($subid));
170
+					$subid >>= 7;
171
+				} while ($subid > 0);
172
+				// all bytes except last must have bit 8 set to one
173
+				foreach (array_splice($bytes, 0, -1) as $byte) {
174
+					$data .= chr(0x80 | $byte);
175
+				}
176
+				$data .= chr(reset($bytes));
177
+			}
178
+		}
179
+		return $data;
180
+	}
181 181
     
182
-    /**
183
-     * Decode sub ID's from DER data.
184
-     *
185
-     * @param string $data
186
-     * @throws DecodeException
187
-     * @return \GMP[] Array of GMP numbers
188
-     */
189
-    protected static function _decodeSubIDs(string $data): array
190
-    {
191
-        $subids = [];
192
-        $idx = 0;
193
-        $end = strlen($data);
194
-        while ($idx < $end) {
195
-            $num = gmp_init("0", 10);
196
-            while (true) {
197
-                if ($idx >= $end) {
198
-                    throw new DecodeException("Unexpected end of data.");
199
-                }
200
-                $byte = ord($data[$idx++]);
201
-                $num |= $byte & 0x7f;
202
-                // bit 8 of the last octet is zero
203
-                if (!($byte & 0x80)) {
204
-                    break;
205
-                }
206
-                $num <<= 7;
207
-            }
208
-            $subids[] = $num;
209
-        }
210
-        return $subids;
211
-    }
182
+	/**
183
+	 * Decode sub ID's from DER data.
184
+	 *
185
+	 * @param string $data
186
+	 * @throws DecodeException
187
+	 * @return \GMP[] Array of GMP numbers
188
+	 */
189
+	protected static function _decodeSubIDs(string $data): array
190
+	{
191
+		$subids = [];
192
+		$idx = 0;
193
+		$end = strlen($data);
194
+		while ($idx < $end) {
195
+			$num = gmp_init("0", 10);
196
+			while (true) {
197
+				if ($idx >= $end) {
198
+					throw new DecodeException("Unexpected end of data.");
199
+				}
200
+				$byte = ord($data[$idx++]);
201
+				$num |= $byte & 0x7f;
202
+				// bit 8 of the last octet is zero
203
+				if (!($byte & 0x80)) {
204
+					break;
205
+				}
206
+				$num <<= 7;
207
+			}
208
+			$subids[] = $num;
209
+		}
210
+		return $subids;
211
+	}
212 212
 }
Please login to merge, or discard this patch.