GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — indefinite ( 96169e...95c197 )
by Joni
02:06
created
lib/ASN1/Type/Primitive/RelativeOID.php 1 patch
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -12,39 +12,39 @@
 block discarded – undo
12 12
  */
13 13
 class RelativeOID extends ObjectIdentifier
14 14
 {
15
-    /**
16
-     * Constructor.
17
-     *
18
-     * @param string $oid OID in dotted format
19
-     */
20
-    public function __construct(string $oid)
21
-    {
22
-        $this->_oid = $oid;
23
-        $this->_subids = self::_explodeDottedOID($oid);
24
-        $this->_typeTag = self::TYPE_RELATIVE_OID;
25
-    }
15
+	/**
16
+	 * Constructor.
17
+	 *
18
+	 * @param string $oid OID in dotted format
19
+	 */
20
+	public function __construct(string $oid)
21
+	{
22
+		$this->_oid = $oid;
23
+		$this->_subids = self::_explodeDottedOID($oid);
24
+		$this->_typeTag = self::TYPE_RELATIVE_OID;
25
+	}
26 26
     
27
-    /**
28
-     *
29
-     * {@inheritdoc}
30
-     */
31
-    protected function _encodedContentDER(): string
32
-    {
33
-        return self::_encodeSubIDs(...$this->_subids);
34
-    }
27
+	/**
28
+	 *
29
+	 * {@inheritdoc}
30
+	 */
31
+	protected function _encodedContentDER(): string
32
+	{
33
+		return self::_encodeSubIDs(...$this->_subids);
34
+	}
35 35
     
36
-    /**
37
-     *
38
-     * {@inheritdoc}
39
-     * @return self
40
-     */
41
-    protected static function _decodeFromDER(Identifier $identifier,
42
-        string $data, int &$offset): ElementBase
43
-    {
44
-        $idx = $offset;
45
-        $len = Length::expectFromDER($data, $idx)->intLength();
46
-        $subids = self::_decodeSubIDs(substr($data, $idx, $len));
47
-        $offset = $idx + $len;
48
-        return new self(self::_implodeSubIDs(...$subids));
49
-    }
36
+	/**
37
+	 *
38
+	 * {@inheritdoc}
39
+	 * @return self
40
+	 */
41
+	protected static function _decodeFromDER(Identifier $identifier,
42
+		string $data, int &$offset): ElementBase
43
+	{
44
+		$idx = $offset;
45
+		$len = Length::expectFromDER($data, $idx)->intLength();
46
+		$subids = self::_decodeSubIDs(substr($data, $idx, $len));
47
+		$offset = $idx + $len;
48
+		return new self(self::_implodeSubIDs(...$subids));
49
+	}
50 50
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/TimeType.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -10,88 +10,88 @@
 block discarded – undo
10 10
  */
11 11
 abstract class TimeType extends Element
12 12
 {
13
-    /**
14
-     * UTC timezone.
15
-     *
16
-     * @var string
17
-     */
18
-    const TZ_UTC = "UTC";
13
+	/**
14
+	 * UTC timezone.
15
+	 *
16
+	 * @var string
17
+	 */
18
+	const TZ_UTC = "UTC";
19 19
     
20
-    /**
21
-     * Date and time.
22
-     *
23
-     * @var \DateTimeImmutable $_dateTime
24
-     */
25
-    protected $_dateTime;
20
+	/**
21
+	 * Date and time.
22
+	 *
23
+	 * @var \DateTimeImmutable $_dateTime
24
+	 */
25
+	protected $_dateTime;
26 26
     
27
-    /**
28
-     * Constructor.
29
-     *
30
-     * @param \DateTimeImmutable $dt
31
-     */
32
-    public function __construct(\DateTimeImmutable $dt)
33
-    {
34
-        $this->_dateTime = $dt;
35
-    }
27
+	/**
28
+	 * Constructor.
29
+	 *
30
+	 * @param \DateTimeImmutable $dt
31
+	 */
32
+	public function __construct(\DateTimeImmutable $dt)
33
+	{
34
+		$this->_dateTime = $dt;
35
+	}
36 36
     
37
-    /**
38
-     * Initialize from datetime string.
39
-     *
40
-     * @link http://php.net/manual/en/datetime.formats.php
41
-     * @param string $time Time string
42
-     * @param string|null $tz Timezone, if null use default.
43
-     * @throws \RuntimeException
44
-     * @return self
45
-     */
46
-    public static function fromString(string $time, string $tz = null): self
47
-    {
48
-        try {
49
-            if (!isset($tz)) {
50
-                $tz = date_default_timezone_get();
51
-            }
52
-            return new static(
53
-                new \DateTimeImmutable($time, self::_createTimeZone($tz)));
54
-        } catch (\Exception $e) {
55
-            throw new \RuntimeException(
56
-                "Failed to create DateTime: " .
57
-                self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
58
-        }
59
-    }
37
+	/**
38
+	 * Initialize from datetime string.
39
+	 *
40
+	 * @link http://php.net/manual/en/datetime.formats.php
41
+	 * @param string $time Time string
42
+	 * @param string|null $tz Timezone, if null use default.
43
+	 * @throws \RuntimeException
44
+	 * @return self
45
+	 */
46
+	public static function fromString(string $time, string $tz = null): self
47
+	{
48
+		try {
49
+			if (!isset($tz)) {
50
+				$tz = date_default_timezone_get();
51
+			}
52
+			return new static(
53
+				new \DateTimeImmutable($time, self::_createTimeZone($tz)));
54
+		} catch (\Exception $e) {
55
+			throw new \RuntimeException(
56
+				"Failed to create DateTime: " .
57
+				self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
58
+		}
59
+	}
60 60
     
61
-    /**
62
-     * Get the date and time.
63
-     *
64
-     * @return \DateTimeImmutable
65
-     */
66
-    public function dateTime(): \DateTimeImmutable
67
-    {
68
-        return $this->_dateTime;
69
-    }
61
+	/**
62
+	 * Get the date and time.
63
+	 *
64
+	 * @return \DateTimeImmutable
65
+	 */
66
+	public function dateTime(): \DateTimeImmutable
67
+	{
68
+		return $this->_dateTime;
69
+	}
70 70
     
71
-    /**
72
-     * Create DateTimeZone object from string.
73
-     *
74
-     * @param string $tz
75
-     * @throws \UnexpectedValueException If timezone is invalid
76
-     * @return \DateTimeZone
77
-     */
78
-    protected static function _createTimeZone(string $tz): \DateTimeZone
79
-    {
80
-        try {
81
-            return new \DateTimeZone($tz);
82
-        } catch (\Exception $e) {
83
-            throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
84
-        }
85
-    }
71
+	/**
72
+	 * Create DateTimeZone object from string.
73
+	 *
74
+	 * @param string $tz
75
+	 * @throws \UnexpectedValueException If timezone is invalid
76
+	 * @return \DateTimeZone
77
+	 */
78
+	protected static function _createTimeZone(string $tz): \DateTimeZone
79
+	{
80
+		try {
81
+			return new \DateTimeZone($tz);
82
+		} catch (\Exception $e) {
83
+			throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
84
+		}
85
+	}
86 86
     
87
-    /**
88
-     * Get last error caused by DateTimeImmutable.
89
-     *
90
-     * @return string
91
-     */
92
-    protected static function _getLastDateTimeImmutableErrorsStr(): string
93
-    {
94
-        $errors = \DateTimeImmutable::getLastErrors()["errors"];
95
-        return implode(", ", $errors);
96
-    }
87
+	/**
88
+	 * Get last error caused by DateTimeImmutable.
89
+	 *
90
+	 * @return string
91
+	 */
92
+	protected static function _getLastDateTimeImmutableErrorsStr(): string
93
+	{
94
+		$errors = \DateTimeImmutable::getLastErrors()["errors"];
95
+		return implode(", ", $errors);
96
+	}
97 97
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/UnspecifiedType.php 1 patch
Indentation   +661 added lines, -661 removed lines patch added patch discarded remove patch
@@ -16,665 +16,665 @@
 block discarded – undo
16 16
  */
17 17
 class UnspecifiedType implements ElementBase
18 18
 {
19
-    /**
20
-     * The wrapped element.
21
-     *
22
-     * @var Element
23
-     */
24
-    private $_element;
25
-    
26
-    /**
27
-     * Constructor.
28
-     *
29
-     * @param Element $el
30
-     */
31
-    public function __construct(Element $el)
32
-    {
33
-        $this->_element = $el;
34
-    }
35
-    
36
-    /**
37
-     * Initialize from DER data.
38
-     *
39
-     * @param string $data DER encoded data
40
-     * @return self
41
-     */
42
-    public static function fromDER(string $data): self
43
-    {
44
-        return Element::fromDER($data)->asUnspecified();
45
-    }
46
-    
47
-    /**
48
-     * Initialize from ElementBase interface.
49
-     *
50
-     * @param ElementBase $el
51
-     * @return self
52
-     */
53
-    public static function fromElementBase(ElementBase $el): self
54
-    {
55
-        // if element is already wrapped
56
-        if ($el instanceof self) {
57
-            return $el;
58
-        }
59
-        return new self($el->asElement());
60
-    }
61
-    
62
-    /**
63
-     * Compatibility method to dispatch calls to the wrapped element.
64
-     *
65
-     * @deprecated Use <code>as*</code> accessor methods to ensure strict type
66
-     * @param string $mtd Method name
67
-     * @param array $args Arguments
68
-     * @return mixed
69
-     */
70
-    public function __call($mtd, array $args)
71
-    {
72
-        return call_user_func_array([$this->_element, $mtd], $args);
73
-    }
74
-    
75
-    /**
76
-     * Get the wrapped element as a context specific tagged type.
77
-     *
78
-     * @throws \UnexpectedValueException If the element is not tagged
79
-     * @return TaggedType
80
-     */
81
-    public function asTagged(): TaggedType
82
-    {
83
-        if (!$this->_element instanceof TaggedType) {
84
-            throw new \UnexpectedValueException(
85
-                "Tagged element expected, got " . $this->_typeDescriptorString());
86
-        }
87
-        return $this->_element;
88
-    }
89
-    
90
-    /**
91
-     * Get the wrapped element as an application specific type.
92
-     *
93
-     * @throws \UnexpectedValueException If element is not application specific
94
-     * @return \ASN1\Type\Tagged\ApplicationType
95
-     */
96
-    public function asApplication(): Tagged\ApplicationType
97
-    {
98
-        if (!$this->_element instanceof Tagged\ApplicationType) {
99
-            throw new \UnexpectedValueException(
100
-                "Application type expected, got " .
101
-                $this->_typeDescriptorString());
102
-        }
103
-        return $this->_element;
104
-    }
105
-    
106
-    /**
107
-     * Get the wrapped element as a private tagged type.
108
-     *
109
-     * @throws \UnexpectedValueException If element is not using private tagging
110
-     * @return \ASN1\Type\Tagged\PrivateType
111
-     */
112
-    public function asPrivate(): Tagged\PrivateType
113
-    {
114
-        if (!$this->_element instanceof Tagged\PrivateType) {
115
-            throw new \UnexpectedValueException(
116
-                "Private type expected, got " . $this->_typeDescriptorString());
117
-        }
118
-        return $this->_element;
119
-    }
120
-    
121
-    /**
122
-     * Get the wrapped element as a boolean type.
123
-     *
124
-     * @throws \UnexpectedValueException If the element is not a boolean
125
-     * @return \ASN1\Type\Primitive\Boolean
126
-     */
127
-    public function asBoolean(): Primitive\Boolean
128
-    {
129
-        if (!$this->_element instanceof Primitive\Boolean) {
130
-            throw new \UnexpectedValueException(
131
-                $this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
132
-        }
133
-        return $this->_element;
134
-    }
135
-    
136
-    /**
137
-     * Get the wrapped element as an integer type.
138
-     *
139
-     * @throws \UnexpectedValueException If the element is not an integer
140
-     * @return \ASN1\Type\Primitive\Integer
141
-     */
142
-    public function asInteger(): Primitive\Integer
143
-    {
144
-        if (!$this->_element instanceof Primitive\Integer) {
145
-            throw new \UnexpectedValueException(
146
-                $this->_generateExceptionMessage(Element::TYPE_INTEGER));
147
-        }
148
-        return $this->_element;
149
-    }
150
-    
151
-    /**
152
-     * Get the wrapped element as a bit string type.
153
-     *
154
-     * @throws \UnexpectedValueException If the element is not a bit string
155
-     * @return \ASN1\Type\Primitive\BitString
156
-     */
157
-    public function asBitString(): Primitive\BitString
158
-    {
159
-        if (!$this->_element instanceof Primitive\BitString) {
160
-            throw new \UnexpectedValueException(
161
-                $this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
162
-        }
163
-        return $this->_element;
164
-    }
165
-    
166
-    /**
167
-     * Get the wrapped element as an octet string type.
168
-     *
169
-     * @throws \UnexpectedValueException If the element is not an octet string
170
-     * @return \ASN1\Type\Primitive\OctetString
171
-     */
172
-    public function asOctetString(): Primitive\OctetString
173
-    {
174
-        if (!$this->_element instanceof Primitive\OctetString) {
175
-            throw new \UnexpectedValueException(
176
-                $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
177
-        }
178
-        return $this->_element;
179
-    }
180
-    
181
-    /**
182
-     * Get the wrapped element as a null type.
183
-     *
184
-     * @throws \UnexpectedValueException If the element is not a null
185
-     * @return \ASN1\Type\Primitive\NullType
186
-     */
187
-    public function asNull(): Primitive\NullType
188
-    {
189
-        if (!$this->_element instanceof Primitive\NullType) {
190
-            throw new \UnexpectedValueException(
191
-                $this->_generateExceptionMessage(Element::TYPE_NULL));
192
-        }
193
-        return $this->_element;
194
-    }
195
-    
196
-    /**
197
-     * Get the wrapped element as an object identifier type.
198
-     *
199
-     * @throws \UnexpectedValueException If the element is not an object
200
-     *         identifier
201
-     * @return \ASN1\Type\Primitive\ObjectIdentifier
202
-     */
203
-    public function asObjectIdentifier(): Primitive\ObjectIdentifier
204
-    {
205
-        if (!$this->_element instanceof Primitive\ObjectIdentifier) {
206
-            throw new \UnexpectedValueException(
207
-                $this->_generateExceptionMessage(
208
-                    Element::TYPE_OBJECT_IDENTIFIER));
209
-        }
210
-        return $this->_element;
211
-    }
212
-    
213
-    /**
214
-     * Get the wrapped element as an object descriptor type.
215
-     *
216
-     * @throws \UnexpectedValueException If the element is not an object
217
-     *         descriptor
218
-     * @return \ASN1\Type\Primitive\ObjectDescriptor
219
-     */
220
-    public function asObjectDescriptor(): Primitive\ObjectDescriptor
221
-    {
222
-        if (!$this->_element instanceof Primitive\ObjectDescriptor) {
223
-            throw new \UnexpectedValueException(
224
-                $this->_generateExceptionMessage(
225
-                    Element::TYPE_OBJECT_DESCRIPTOR));
226
-        }
227
-        return $this->_element;
228
-    }
229
-    
230
-    /**
231
-     * Get the wrapped element as a real type.
232
-     *
233
-     * @throws \UnexpectedValueException If the element is not a real
234
-     * @return \ASN1\Type\Primitive\Real
235
-     */
236
-    public function asReal(): Primitive\Real
237
-    {
238
-        if (!$this->_element instanceof Primitive\Real) {
239
-            throw new \UnexpectedValueException(
240
-                $this->_generateExceptionMessage(Element::TYPE_REAL));
241
-        }
242
-        return $this->_element;
243
-    }
244
-    
245
-    /**
246
-     * Get the wrapped element as an enumerated type.
247
-     *
248
-     * @throws \UnexpectedValueException If the element is not an enumerated
249
-     * @return \ASN1\Type\Primitive\Enumerated
250
-     */
251
-    public function asEnumerated(): Primitive\Enumerated
252
-    {
253
-        if (!$this->_element instanceof Primitive\Enumerated) {
254
-            throw new \UnexpectedValueException(
255
-                $this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
256
-        }
257
-        return $this->_element;
258
-    }
259
-    
260
-    /**
261
-     * Get the wrapped element as a UTF8 string type.
262
-     *
263
-     * @throws \UnexpectedValueException If the element is not a UTF8 string
264
-     * @return \ASN1\Type\Primitive\UTF8String
265
-     */
266
-    public function asUTF8String(): Primitive\UTF8String
267
-    {
268
-        if (!$this->_element instanceof Primitive\UTF8String) {
269
-            throw new \UnexpectedValueException(
270
-                $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
271
-        }
272
-        return $this->_element;
273
-    }
274
-    
275
-    /**
276
-     * Get the wrapped element as a relative OID type.
277
-     *
278
-     * @throws \UnexpectedValueException If the element is not a relative OID
279
-     * @return \ASN1\Type\Primitive\RelativeOID
280
-     */
281
-    public function asRelativeOID(): Primitive\RelativeOID
282
-    {
283
-        if (!$this->_element instanceof Primitive\RelativeOID) {
284
-            throw new \UnexpectedValueException(
285
-                $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
286
-        }
287
-        return $this->_element;
288
-    }
289
-    
290
-    /**
291
-     * Get the wrapped element as a sequence type.
292
-     *
293
-     * @throws \UnexpectedValueException If the element is not a sequence
294
-     * @return \ASN1\Type\Constructed\Sequence
295
-     */
296
-    public function asSequence(): Constructed\Sequence
297
-    {
298
-        if (!$this->_element instanceof Constructed\Sequence) {
299
-            throw new \UnexpectedValueException(
300
-                $this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
301
-        }
302
-        return $this->_element;
303
-    }
304
-    
305
-    /**
306
-     * Get the wrapped element as a set type.
307
-     *
308
-     * @throws \UnexpectedValueException If the element is not a set
309
-     * @return \ASN1\Type\Constructed\Set
310
-     */
311
-    public function asSet(): Constructed\Set
312
-    {
313
-        if (!$this->_element instanceof Constructed\Set) {
314
-            throw new \UnexpectedValueException(
315
-                $this->_generateExceptionMessage(Element::TYPE_SET));
316
-        }
317
-        return $this->_element;
318
-    }
319
-    
320
-    /**
321
-     * Get the wrapped element as a numeric string type.
322
-     *
323
-     * @throws \UnexpectedValueException If the element is not a numeric string
324
-     * @return \ASN1\Type\Primitive\NumericString
325
-     */
326
-    public function asNumericString(): Primitive\NumericString
327
-    {
328
-        if (!$this->_element instanceof Primitive\NumericString) {
329
-            throw new \UnexpectedValueException(
330
-                $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
331
-        }
332
-        return $this->_element;
333
-    }
334
-    
335
-    /**
336
-     * Get the wrapped element as a printable string type.
337
-     *
338
-     * @throws \UnexpectedValueException If the element is not a printable
339
-     *         string
340
-     * @return \ASN1\Type\Primitive\PrintableString
341
-     */
342
-    public function asPrintableString(): Primitive\PrintableString
343
-    {
344
-        if (!$this->_element instanceof Primitive\PrintableString) {
345
-            throw new \UnexpectedValueException(
346
-                $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
347
-        }
348
-        return $this->_element;
349
-    }
350
-    
351
-    /**
352
-     * Get the wrapped element as a T61 string type.
353
-     *
354
-     * @throws \UnexpectedValueException If the element is not a T61 string
355
-     * @return \ASN1\Type\Primitive\T61String
356
-     */
357
-    public function asT61String(): Primitive\T61String
358
-    {
359
-        if (!$this->_element instanceof Primitive\T61String) {
360
-            throw new \UnexpectedValueException(
361
-                $this->_generateExceptionMessage(Element::TYPE_T61_STRING));
362
-        }
363
-        return $this->_element;
364
-    }
365
-    
366
-    /**
367
-     * Get the wrapped element as a videotex string type.
368
-     *
369
-     * @throws \UnexpectedValueException If the element is not a videotex string
370
-     * @return \ASN1\Type\Primitive\VideotexString
371
-     */
372
-    public function asVideotexString(): Primitive\VideotexString
373
-    {
374
-        if (!$this->_element instanceof Primitive\VideotexString) {
375
-            throw new \UnexpectedValueException(
376
-                $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
377
-        }
378
-        return $this->_element;
379
-    }
380
-    
381
-    /**
382
-     * Get the wrapped element as a IA5 string type.
383
-     *
384
-     * @throws \UnexpectedValueException If the element is not a IA5 string
385
-     * @return \ASN1\Type\Primitive\IA5String
386
-     */
387
-    public function asIA5String(): Primitive\IA5String
388
-    {
389
-        if (!$this->_element instanceof Primitive\IA5String) {
390
-            throw new \UnexpectedValueException(
391
-                $this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
392
-        }
393
-        return $this->_element;
394
-    }
395
-    
396
-    /**
397
-     * Get the wrapped element as an UTC time type.
398
-     *
399
-     * @throws \UnexpectedValueException If the element is not a UTC time
400
-     * @return \ASN1\Type\Primitive\UTCTime
401
-     */
402
-    public function asUTCTime(): Primitive\UTCTime
403
-    {
404
-        if (!$this->_element instanceof Primitive\UTCTime) {
405
-            throw new \UnexpectedValueException(
406
-                $this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
407
-        }
408
-        return $this->_element;
409
-    }
410
-    
411
-    /**
412
-     * Get the wrapped element as a generalized time type.
413
-     *
414
-     * @throws \UnexpectedValueException If the element is not a generalized
415
-     *         time
416
-     * @return \ASN1\Type\Primitive\GeneralizedTime
417
-     */
418
-    public function asGeneralizedTime(): Primitive\GeneralizedTime
419
-    {
420
-        if (!$this->_element instanceof Primitive\GeneralizedTime) {
421
-            throw new \UnexpectedValueException(
422
-                $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
423
-        }
424
-        return $this->_element;
425
-    }
426
-    
427
-    /**
428
-     * Get the wrapped element as a graphic string type.
429
-     *
430
-     * @throws \UnexpectedValueException If the element is not a graphic string
431
-     * @return \ASN1\Type\Primitive\GraphicString
432
-     */
433
-    public function asGraphicString(): Primitive\GraphicString
434
-    {
435
-        if (!$this->_element instanceof Primitive\GraphicString) {
436
-            throw new \UnexpectedValueException(
437
-                $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
438
-        }
439
-        return $this->_element;
440
-    }
441
-    
442
-    /**
443
-     * Get the wrapped element as a visible string type.
444
-     *
445
-     * @throws \UnexpectedValueException If the element is not a visible string
446
-     * @return \ASN1\Type\Primitive\VisibleString
447
-     */
448
-    public function asVisibleString(): Primitive\VisibleString
449
-    {
450
-        if (!$this->_element instanceof Primitive\VisibleString) {
451
-            throw new \UnexpectedValueException(
452
-                $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
453
-        }
454
-        return $this->_element;
455
-    }
456
-    
457
-    /**
458
-     * Get the wrapped element as a general string type.
459
-     *
460
-     * @throws \UnexpectedValueException If the element is not general string
461
-     * @return \ASN1\Type\Primitive\GeneralString
462
-     */
463
-    public function asGeneralString(): Primitive\GeneralString
464
-    {
465
-        if (!$this->_element instanceof Primitive\GeneralString) {
466
-            throw new \UnexpectedValueException(
467
-                $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
468
-        }
469
-        return $this->_element;
470
-    }
471
-    
472
-    /**
473
-     * Get the wrapped element as a universal string type.
474
-     *
475
-     * @throws \UnexpectedValueException If the element is not a universal
476
-     *         string
477
-     * @return \ASN1\Type\Primitive\UniversalString
478
-     */
479
-    public function asUniversalString(): Primitive\UniversalString
480
-    {
481
-        if (!$this->_element instanceof Primitive\UniversalString) {
482
-            throw new \UnexpectedValueException(
483
-                $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
484
-        }
485
-        return $this->_element;
486
-    }
487
-    
488
-    /**
489
-     * Get the wrapped element as a character string type.
490
-     *
491
-     * @throws \UnexpectedValueException If the element is not a character
492
-     *         string
493
-     * @return \ASN1\Type\Primitive\CharacterString
494
-     */
495
-    public function asCharacterString(): Primitive\CharacterString
496
-    {
497
-        if (!$this->_element instanceof Primitive\CharacterString) {
498
-            throw new \UnexpectedValueException(
499
-                $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
500
-        }
501
-        return $this->_element;
502
-    }
503
-    
504
-    /**
505
-     * Get the wrapped element as a BMP string type.
506
-     *
507
-     * @throws \UnexpectedValueException If the element is not a bmp string
508
-     * @return \ASN1\Type\Primitive\BMPString
509
-     */
510
-    public function asBMPString(): Primitive\BMPString
511
-    {
512
-        if (!$this->_element instanceof Primitive\BMPString) {
513
-            throw new \UnexpectedValueException(
514
-                $this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
515
-        }
516
-        return $this->_element;
517
-    }
518
-    
519
-    /**
520
-     * Get the wrapped element as any string type.
521
-     *
522
-     * @throws \UnexpectedValueException If the element is not a string
523
-     * @return StringType
524
-     */
525
-    public function asString(): StringType
526
-    {
527
-        if (!$this->_element instanceof StringType) {
528
-            throw new \UnexpectedValueException(
529
-                $this->_generateExceptionMessage(Element::TYPE_STRING));
530
-        }
531
-        return $this->_element;
532
-    }
533
-    
534
-    /**
535
-     * Get the wrapped element as any time type.
536
-     *
537
-     * @throws \UnexpectedValueException If the element is not a time
538
-     * @return TimeType
539
-     */
540
-    public function asTime(): TimeType
541
-    {
542
-        if (!$this->_element instanceof TimeType) {
543
-            throw new \UnexpectedValueException(
544
-                $this->_generateExceptionMessage(Element::TYPE_TIME));
545
-        }
546
-        return $this->_element;
547
-    }
548
-    
549
-    /**
550
-     * Generate message for exceptions thrown by <code>as*</code> methods.
551
-     *
552
-     * @param int $tag Type tag of the expected element
553
-     * @return string
554
-     */
555
-    private function _generateExceptionMessage(int $tag): string
556
-    {
557
-        return sprintf("%s expected, got %s.", Element::tagToName($tag),
558
-            $this->_typeDescriptorString());
559
-    }
560
-    
561
-    /**
562
-     * Get textual description of the wrapped element for debugging purposes.
563
-     *
564
-     * @return string
565
-     */
566
-    private function _typeDescriptorString(): string
567
-    {
568
-        $type_cls = $this->_element->typeClass();
569
-        $tag = $this->_element->tag();
570
-        if ($type_cls == Identifier::CLASS_UNIVERSAL) {
571
-            return Element::tagToName($tag);
572
-        }
573
-        return Identifier::classToName($type_cls) . " TAG $tag";
574
-    }
575
-    
576
-    /**
577
-     *
578
-     * @see \ASN1\Feature\Encodable::toDER()
579
-     * @return string
580
-     */
581
-    public function toDER(): string
582
-    {
583
-        return $this->_element->toDER();
584
-    }
585
-    
586
-    /**
587
-     *
588
-     * @see \ASN1\Feature\ElementBase::typeClass()
589
-     * @return int
590
-     */
591
-    public function typeClass(): int
592
-    {
593
-        return $this->_element->typeClass();
594
-    }
595
-    
596
-    /**
597
-     *
598
-     * @see \ASN1\Feature\ElementBase::isConstructed()
599
-     * @return bool
600
-     */
601
-    public function isConstructed(): bool
602
-    {
603
-        return $this->_element->isConstructed();
604
-    }
605
-    
606
-    /**
607
-     *
608
-     * @see \ASN1\Feature\ElementBase::tag()
609
-     * @return int
610
-     */
611
-    public function tag(): int
612
-    {
613
-        return $this->_element->tag();
614
-    }
615
-    
616
-    /**
617
-     *
618
-     * {@inheritdoc}
619
-     * @see \ASN1\Feature\ElementBase::isType()
620
-     * @return bool
621
-     */
622
-    public function isType(int $tag): bool
623
-    {
624
-        return $this->_element->isType($tag);
625
-    }
626
-    
627
-    /**
628
-     *
629
-     * @deprecated Use any <code>as*</code> accessor method first to ensure
630
-     *             type strictness.
631
-     * @see \ASN1\Feature\ElementBase::expectType()
632
-     * @return ElementBase
633
-     */
634
-    public function expectType(int $tag): ElementBase
635
-    {
636
-        return $this->_element->expectType($tag);
637
-    }
638
-    
639
-    /**
640
-     *
641
-     * @see \ASN1\Feature\ElementBase::isTagged()
642
-     * @return bool
643
-     */
644
-    public function isTagged(): bool
645
-    {
646
-        return $this->_element->isTagged();
647
-    }
648
-    
649
-    /**
650
-     *
651
-     * @deprecated Use any <code>as*</code> accessor method first to ensure
652
-     *             type strictness.
653
-     * @see \ASN1\Feature\ElementBase::expectTagged()
654
-     * @return TaggedType
655
-     */
656
-    public function expectTagged($tag = null): TaggedType
657
-    {
658
-        return $this->_element->expectTagged($tag);
659
-    }
660
-    
661
-    /**
662
-     *
663
-     * @see \ASN1\Feature\ElementBase::asElement()
664
-     * @return Element
665
-     */
666
-    public function asElement(): Element
667
-    {
668
-        return $this->_element;
669
-    }
670
-    
671
-    /**
672
-     *
673
-     * {@inheritdoc}
674
-     * @return UnspecifiedType
675
-     */
676
-    public function asUnspecified(): UnspecifiedType
677
-    {
678
-        return $this;
679
-    }
19
+	/**
20
+	 * The wrapped element.
21
+	 *
22
+	 * @var Element
23
+	 */
24
+	private $_element;
25
+    
26
+	/**
27
+	 * Constructor.
28
+	 *
29
+	 * @param Element $el
30
+	 */
31
+	public function __construct(Element $el)
32
+	{
33
+		$this->_element = $el;
34
+	}
35
+    
36
+	/**
37
+	 * Initialize from DER data.
38
+	 *
39
+	 * @param string $data DER encoded data
40
+	 * @return self
41
+	 */
42
+	public static function fromDER(string $data): self
43
+	{
44
+		return Element::fromDER($data)->asUnspecified();
45
+	}
46
+    
47
+	/**
48
+	 * Initialize from ElementBase interface.
49
+	 *
50
+	 * @param ElementBase $el
51
+	 * @return self
52
+	 */
53
+	public static function fromElementBase(ElementBase $el): self
54
+	{
55
+		// if element is already wrapped
56
+		if ($el instanceof self) {
57
+			return $el;
58
+		}
59
+		return new self($el->asElement());
60
+	}
61
+    
62
+	/**
63
+	 * Compatibility method to dispatch calls to the wrapped element.
64
+	 *
65
+	 * @deprecated Use <code>as*</code> accessor methods to ensure strict type
66
+	 * @param string $mtd Method name
67
+	 * @param array $args Arguments
68
+	 * @return mixed
69
+	 */
70
+	public function __call($mtd, array $args)
71
+	{
72
+		return call_user_func_array([$this->_element, $mtd], $args);
73
+	}
74
+    
75
+	/**
76
+	 * Get the wrapped element as a context specific tagged type.
77
+	 *
78
+	 * @throws \UnexpectedValueException If the element is not tagged
79
+	 * @return TaggedType
80
+	 */
81
+	public function asTagged(): TaggedType
82
+	{
83
+		if (!$this->_element instanceof TaggedType) {
84
+			throw new \UnexpectedValueException(
85
+				"Tagged element expected, got " . $this->_typeDescriptorString());
86
+		}
87
+		return $this->_element;
88
+	}
89
+    
90
+	/**
91
+	 * Get the wrapped element as an application specific type.
92
+	 *
93
+	 * @throws \UnexpectedValueException If element is not application specific
94
+	 * @return \ASN1\Type\Tagged\ApplicationType
95
+	 */
96
+	public function asApplication(): Tagged\ApplicationType
97
+	{
98
+		if (!$this->_element instanceof Tagged\ApplicationType) {
99
+			throw new \UnexpectedValueException(
100
+				"Application type expected, got " .
101
+				$this->_typeDescriptorString());
102
+		}
103
+		return $this->_element;
104
+	}
105
+    
106
+	/**
107
+	 * Get the wrapped element as a private tagged type.
108
+	 *
109
+	 * @throws \UnexpectedValueException If element is not using private tagging
110
+	 * @return \ASN1\Type\Tagged\PrivateType
111
+	 */
112
+	public function asPrivate(): Tagged\PrivateType
113
+	{
114
+		if (!$this->_element instanceof Tagged\PrivateType) {
115
+			throw new \UnexpectedValueException(
116
+				"Private type expected, got " . $this->_typeDescriptorString());
117
+		}
118
+		return $this->_element;
119
+	}
120
+    
121
+	/**
122
+	 * Get the wrapped element as a boolean type.
123
+	 *
124
+	 * @throws \UnexpectedValueException If the element is not a boolean
125
+	 * @return \ASN1\Type\Primitive\Boolean
126
+	 */
127
+	public function asBoolean(): Primitive\Boolean
128
+	{
129
+		if (!$this->_element instanceof Primitive\Boolean) {
130
+			throw new \UnexpectedValueException(
131
+				$this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
132
+		}
133
+		return $this->_element;
134
+	}
135
+    
136
+	/**
137
+	 * Get the wrapped element as an integer type.
138
+	 *
139
+	 * @throws \UnexpectedValueException If the element is not an integer
140
+	 * @return \ASN1\Type\Primitive\Integer
141
+	 */
142
+	public function asInteger(): Primitive\Integer
143
+	{
144
+		if (!$this->_element instanceof Primitive\Integer) {
145
+			throw new \UnexpectedValueException(
146
+				$this->_generateExceptionMessage(Element::TYPE_INTEGER));
147
+		}
148
+		return $this->_element;
149
+	}
150
+    
151
+	/**
152
+	 * Get the wrapped element as a bit string type.
153
+	 *
154
+	 * @throws \UnexpectedValueException If the element is not a bit string
155
+	 * @return \ASN1\Type\Primitive\BitString
156
+	 */
157
+	public function asBitString(): Primitive\BitString
158
+	{
159
+		if (!$this->_element instanceof Primitive\BitString) {
160
+			throw new \UnexpectedValueException(
161
+				$this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
162
+		}
163
+		return $this->_element;
164
+	}
165
+    
166
+	/**
167
+	 * Get the wrapped element as an octet string type.
168
+	 *
169
+	 * @throws \UnexpectedValueException If the element is not an octet string
170
+	 * @return \ASN1\Type\Primitive\OctetString
171
+	 */
172
+	public function asOctetString(): Primitive\OctetString
173
+	{
174
+		if (!$this->_element instanceof Primitive\OctetString) {
175
+			throw new \UnexpectedValueException(
176
+				$this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
177
+		}
178
+		return $this->_element;
179
+	}
180
+    
181
+	/**
182
+	 * Get the wrapped element as a null type.
183
+	 *
184
+	 * @throws \UnexpectedValueException If the element is not a null
185
+	 * @return \ASN1\Type\Primitive\NullType
186
+	 */
187
+	public function asNull(): Primitive\NullType
188
+	{
189
+		if (!$this->_element instanceof Primitive\NullType) {
190
+			throw new \UnexpectedValueException(
191
+				$this->_generateExceptionMessage(Element::TYPE_NULL));
192
+		}
193
+		return $this->_element;
194
+	}
195
+    
196
+	/**
197
+	 * Get the wrapped element as an object identifier type.
198
+	 *
199
+	 * @throws \UnexpectedValueException If the element is not an object
200
+	 *         identifier
201
+	 * @return \ASN1\Type\Primitive\ObjectIdentifier
202
+	 */
203
+	public function asObjectIdentifier(): Primitive\ObjectIdentifier
204
+	{
205
+		if (!$this->_element instanceof Primitive\ObjectIdentifier) {
206
+			throw new \UnexpectedValueException(
207
+				$this->_generateExceptionMessage(
208
+					Element::TYPE_OBJECT_IDENTIFIER));
209
+		}
210
+		return $this->_element;
211
+	}
212
+    
213
+	/**
214
+	 * Get the wrapped element as an object descriptor type.
215
+	 *
216
+	 * @throws \UnexpectedValueException If the element is not an object
217
+	 *         descriptor
218
+	 * @return \ASN1\Type\Primitive\ObjectDescriptor
219
+	 */
220
+	public function asObjectDescriptor(): Primitive\ObjectDescriptor
221
+	{
222
+		if (!$this->_element instanceof Primitive\ObjectDescriptor) {
223
+			throw new \UnexpectedValueException(
224
+				$this->_generateExceptionMessage(
225
+					Element::TYPE_OBJECT_DESCRIPTOR));
226
+		}
227
+		return $this->_element;
228
+	}
229
+    
230
+	/**
231
+	 * Get the wrapped element as a real type.
232
+	 *
233
+	 * @throws \UnexpectedValueException If the element is not a real
234
+	 * @return \ASN1\Type\Primitive\Real
235
+	 */
236
+	public function asReal(): Primitive\Real
237
+	{
238
+		if (!$this->_element instanceof Primitive\Real) {
239
+			throw new \UnexpectedValueException(
240
+				$this->_generateExceptionMessage(Element::TYPE_REAL));
241
+		}
242
+		return $this->_element;
243
+	}
244
+    
245
+	/**
246
+	 * Get the wrapped element as an enumerated type.
247
+	 *
248
+	 * @throws \UnexpectedValueException If the element is not an enumerated
249
+	 * @return \ASN1\Type\Primitive\Enumerated
250
+	 */
251
+	public function asEnumerated(): Primitive\Enumerated
252
+	{
253
+		if (!$this->_element instanceof Primitive\Enumerated) {
254
+			throw new \UnexpectedValueException(
255
+				$this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
256
+		}
257
+		return $this->_element;
258
+	}
259
+    
260
+	/**
261
+	 * Get the wrapped element as a UTF8 string type.
262
+	 *
263
+	 * @throws \UnexpectedValueException If the element is not a UTF8 string
264
+	 * @return \ASN1\Type\Primitive\UTF8String
265
+	 */
266
+	public function asUTF8String(): Primitive\UTF8String
267
+	{
268
+		if (!$this->_element instanceof Primitive\UTF8String) {
269
+			throw new \UnexpectedValueException(
270
+				$this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
271
+		}
272
+		return $this->_element;
273
+	}
274
+    
275
+	/**
276
+	 * Get the wrapped element as a relative OID type.
277
+	 *
278
+	 * @throws \UnexpectedValueException If the element is not a relative OID
279
+	 * @return \ASN1\Type\Primitive\RelativeOID
280
+	 */
281
+	public function asRelativeOID(): Primitive\RelativeOID
282
+	{
283
+		if (!$this->_element instanceof Primitive\RelativeOID) {
284
+			throw new \UnexpectedValueException(
285
+				$this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
286
+		}
287
+		return $this->_element;
288
+	}
289
+    
290
+	/**
291
+	 * Get the wrapped element as a sequence type.
292
+	 *
293
+	 * @throws \UnexpectedValueException If the element is not a sequence
294
+	 * @return \ASN1\Type\Constructed\Sequence
295
+	 */
296
+	public function asSequence(): Constructed\Sequence
297
+	{
298
+		if (!$this->_element instanceof Constructed\Sequence) {
299
+			throw new \UnexpectedValueException(
300
+				$this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
301
+		}
302
+		return $this->_element;
303
+	}
304
+    
305
+	/**
306
+	 * Get the wrapped element as a set type.
307
+	 *
308
+	 * @throws \UnexpectedValueException If the element is not a set
309
+	 * @return \ASN1\Type\Constructed\Set
310
+	 */
311
+	public function asSet(): Constructed\Set
312
+	{
313
+		if (!$this->_element instanceof Constructed\Set) {
314
+			throw new \UnexpectedValueException(
315
+				$this->_generateExceptionMessage(Element::TYPE_SET));
316
+		}
317
+		return $this->_element;
318
+	}
319
+    
320
+	/**
321
+	 * Get the wrapped element as a numeric string type.
322
+	 *
323
+	 * @throws \UnexpectedValueException If the element is not a numeric string
324
+	 * @return \ASN1\Type\Primitive\NumericString
325
+	 */
326
+	public function asNumericString(): Primitive\NumericString
327
+	{
328
+		if (!$this->_element instanceof Primitive\NumericString) {
329
+			throw new \UnexpectedValueException(
330
+				$this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
331
+		}
332
+		return $this->_element;
333
+	}
334
+    
335
+	/**
336
+	 * Get the wrapped element as a printable string type.
337
+	 *
338
+	 * @throws \UnexpectedValueException If the element is not a printable
339
+	 *         string
340
+	 * @return \ASN1\Type\Primitive\PrintableString
341
+	 */
342
+	public function asPrintableString(): Primitive\PrintableString
343
+	{
344
+		if (!$this->_element instanceof Primitive\PrintableString) {
345
+			throw new \UnexpectedValueException(
346
+				$this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
347
+		}
348
+		return $this->_element;
349
+	}
350
+    
351
+	/**
352
+	 * Get the wrapped element as a T61 string type.
353
+	 *
354
+	 * @throws \UnexpectedValueException If the element is not a T61 string
355
+	 * @return \ASN1\Type\Primitive\T61String
356
+	 */
357
+	public function asT61String(): Primitive\T61String
358
+	{
359
+		if (!$this->_element instanceof Primitive\T61String) {
360
+			throw new \UnexpectedValueException(
361
+				$this->_generateExceptionMessage(Element::TYPE_T61_STRING));
362
+		}
363
+		return $this->_element;
364
+	}
365
+    
366
+	/**
367
+	 * Get the wrapped element as a videotex string type.
368
+	 *
369
+	 * @throws \UnexpectedValueException If the element is not a videotex string
370
+	 * @return \ASN1\Type\Primitive\VideotexString
371
+	 */
372
+	public function asVideotexString(): Primitive\VideotexString
373
+	{
374
+		if (!$this->_element instanceof Primitive\VideotexString) {
375
+			throw new \UnexpectedValueException(
376
+				$this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
377
+		}
378
+		return $this->_element;
379
+	}
380
+    
381
+	/**
382
+	 * Get the wrapped element as a IA5 string type.
383
+	 *
384
+	 * @throws \UnexpectedValueException If the element is not a IA5 string
385
+	 * @return \ASN1\Type\Primitive\IA5String
386
+	 */
387
+	public function asIA5String(): Primitive\IA5String
388
+	{
389
+		if (!$this->_element instanceof Primitive\IA5String) {
390
+			throw new \UnexpectedValueException(
391
+				$this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
392
+		}
393
+		return $this->_element;
394
+	}
395
+    
396
+	/**
397
+	 * Get the wrapped element as an UTC time type.
398
+	 *
399
+	 * @throws \UnexpectedValueException If the element is not a UTC time
400
+	 * @return \ASN1\Type\Primitive\UTCTime
401
+	 */
402
+	public function asUTCTime(): Primitive\UTCTime
403
+	{
404
+		if (!$this->_element instanceof Primitive\UTCTime) {
405
+			throw new \UnexpectedValueException(
406
+				$this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
407
+		}
408
+		return $this->_element;
409
+	}
410
+    
411
+	/**
412
+	 * Get the wrapped element as a generalized time type.
413
+	 *
414
+	 * @throws \UnexpectedValueException If the element is not a generalized
415
+	 *         time
416
+	 * @return \ASN1\Type\Primitive\GeneralizedTime
417
+	 */
418
+	public function asGeneralizedTime(): Primitive\GeneralizedTime
419
+	{
420
+		if (!$this->_element instanceof Primitive\GeneralizedTime) {
421
+			throw new \UnexpectedValueException(
422
+				$this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
423
+		}
424
+		return $this->_element;
425
+	}
426
+    
427
+	/**
428
+	 * Get the wrapped element as a graphic string type.
429
+	 *
430
+	 * @throws \UnexpectedValueException If the element is not a graphic string
431
+	 * @return \ASN1\Type\Primitive\GraphicString
432
+	 */
433
+	public function asGraphicString(): Primitive\GraphicString
434
+	{
435
+		if (!$this->_element instanceof Primitive\GraphicString) {
436
+			throw new \UnexpectedValueException(
437
+				$this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
438
+		}
439
+		return $this->_element;
440
+	}
441
+    
442
+	/**
443
+	 * Get the wrapped element as a visible string type.
444
+	 *
445
+	 * @throws \UnexpectedValueException If the element is not a visible string
446
+	 * @return \ASN1\Type\Primitive\VisibleString
447
+	 */
448
+	public function asVisibleString(): Primitive\VisibleString
449
+	{
450
+		if (!$this->_element instanceof Primitive\VisibleString) {
451
+			throw new \UnexpectedValueException(
452
+				$this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
453
+		}
454
+		return $this->_element;
455
+	}
456
+    
457
+	/**
458
+	 * Get the wrapped element as a general string type.
459
+	 *
460
+	 * @throws \UnexpectedValueException If the element is not general string
461
+	 * @return \ASN1\Type\Primitive\GeneralString
462
+	 */
463
+	public function asGeneralString(): Primitive\GeneralString
464
+	{
465
+		if (!$this->_element instanceof Primitive\GeneralString) {
466
+			throw new \UnexpectedValueException(
467
+				$this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
468
+		}
469
+		return $this->_element;
470
+	}
471
+    
472
+	/**
473
+	 * Get the wrapped element as a universal string type.
474
+	 *
475
+	 * @throws \UnexpectedValueException If the element is not a universal
476
+	 *         string
477
+	 * @return \ASN1\Type\Primitive\UniversalString
478
+	 */
479
+	public function asUniversalString(): Primitive\UniversalString
480
+	{
481
+		if (!$this->_element instanceof Primitive\UniversalString) {
482
+			throw new \UnexpectedValueException(
483
+				$this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
484
+		}
485
+		return $this->_element;
486
+	}
487
+    
488
+	/**
489
+	 * Get the wrapped element as a character string type.
490
+	 *
491
+	 * @throws \UnexpectedValueException If the element is not a character
492
+	 *         string
493
+	 * @return \ASN1\Type\Primitive\CharacterString
494
+	 */
495
+	public function asCharacterString(): Primitive\CharacterString
496
+	{
497
+		if (!$this->_element instanceof Primitive\CharacterString) {
498
+			throw new \UnexpectedValueException(
499
+				$this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
500
+		}
501
+		return $this->_element;
502
+	}
503
+    
504
+	/**
505
+	 * Get the wrapped element as a BMP string type.
506
+	 *
507
+	 * @throws \UnexpectedValueException If the element is not a bmp string
508
+	 * @return \ASN1\Type\Primitive\BMPString
509
+	 */
510
+	public function asBMPString(): Primitive\BMPString
511
+	{
512
+		if (!$this->_element instanceof Primitive\BMPString) {
513
+			throw new \UnexpectedValueException(
514
+				$this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
515
+		}
516
+		return $this->_element;
517
+	}
518
+    
519
+	/**
520
+	 * Get the wrapped element as any string type.
521
+	 *
522
+	 * @throws \UnexpectedValueException If the element is not a string
523
+	 * @return StringType
524
+	 */
525
+	public function asString(): StringType
526
+	{
527
+		if (!$this->_element instanceof StringType) {
528
+			throw new \UnexpectedValueException(
529
+				$this->_generateExceptionMessage(Element::TYPE_STRING));
530
+		}
531
+		return $this->_element;
532
+	}
533
+    
534
+	/**
535
+	 * Get the wrapped element as any time type.
536
+	 *
537
+	 * @throws \UnexpectedValueException If the element is not a time
538
+	 * @return TimeType
539
+	 */
540
+	public function asTime(): TimeType
541
+	{
542
+		if (!$this->_element instanceof TimeType) {
543
+			throw new \UnexpectedValueException(
544
+				$this->_generateExceptionMessage(Element::TYPE_TIME));
545
+		}
546
+		return $this->_element;
547
+	}
548
+    
549
+	/**
550
+	 * Generate message for exceptions thrown by <code>as*</code> methods.
551
+	 *
552
+	 * @param int $tag Type tag of the expected element
553
+	 * @return string
554
+	 */
555
+	private function _generateExceptionMessage(int $tag): string
556
+	{
557
+		return sprintf("%s expected, got %s.", Element::tagToName($tag),
558
+			$this->_typeDescriptorString());
559
+	}
560
+    
561
+	/**
562
+	 * Get textual description of the wrapped element for debugging purposes.
563
+	 *
564
+	 * @return string
565
+	 */
566
+	private function _typeDescriptorString(): string
567
+	{
568
+		$type_cls = $this->_element->typeClass();
569
+		$tag = $this->_element->tag();
570
+		if ($type_cls == Identifier::CLASS_UNIVERSAL) {
571
+			return Element::tagToName($tag);
572
+		}
573
+		return Identifier::classToName($type_cls) . " TAG $tag";
574
+	}
575
+    
576
+	/**
577
+	 *
578
+	 * @see \ASN1\Feature\Encodable::toDER()
579
+	 * @return string
580
+	 */
581
+	public function toDER(): string
582
+	{
583
+		return $this->_element->toDER();
584
+	}
585
+    
586
+	/**
587
+	 *
588
+	 * @see \ASN1\Feature\ElementBase::typeClass()
589
+	 * @return int
590
+	 */
591
+	public function typeClass(): int
592
+	{
593
+		return $this->_element->typeClass();
594
+	}
595
+    
596
+	/**
597
+	 *
598
+	 * @see \ASN1\Feature\ElementBase::isConstructed()
599
+	 * @return bool
600
+	 */
601
+	public function isConstructed(): bool
602
+	{
603
+		return $this->_element->isConstructed();
604
+	}
605
+    
606
+	/**
607
+	 *
608
+	 * @see \ASN1\Feature\ElementBase::tag()
609
+	 * @return int
610
+	 */
611
+	public function tag(): int
612
+	{
613
+		return $this->_element->tag();
614
+	}
615
+    
616
+	/**
617
+	 *
618
+	 * {@inheritdoc}
619
+	 * @see \ASN1\Feature\ElementBase::isType()
620
+	 * @return bool
621
+	 */
622
+	public function isType(int $tag): bool
623
+	{
624
+		return $this->_element->isType($tag);
625
+	}
626
+    
627
+	/**
628
+	 *
629
+	 * @deprecated Use any <code>as*</code> accessor method first to ensure
630
+	 *             type strictness.
631
+	 * @see \ASN1\Feature\ElementBase::expectType()
632
+	 * @return ElementBase
633
+	 */
634
+	public function expectType(int $tag): ElementBase
635
+	{
636
+		return $this->_element->expectType($tag);
637
+	}
638
+    
639
+	/**
640
+	 *
641
+	 * @see \ASN1\Feature\ElementBase::isTagged()
642
+	 * @return bool
643
+	 */
644
+	public function isTagged(): bool
645
+	{
646
+		return $this->_element->isTagged();
647
+	}
648
+    
649
+	/**
650
+	 *
651
+	 * @deprecated Use any <code>as*</code> accessor method first to ensure
652
+	 *             type strictness.
653
+	 * @see \ASN1\Feature\ElementBase::expectTagged()
654
+	 * @return TaggedType
655
+	 */
656
+	public function expectTagged($tag = null): TaggedType
657
+	{
658
+		return $this->_element->expectTagged($tag);
659
+	}
660
+    
661
+	/**
662
+	 *
663
+	 * @see \ASN1\Feature\ElementBase::asElement()
664
+	 * @return Element
665
+	 */
666
+	public function asElement(): Element
667
+	{
668
+		return $this->_element;
669
+	}
670
+    
671
+	/**
672
+	 *
673
+	 * {@inheritdoc}
674
+	 * @return UnspecifiedType
675
+	 */
676
+	public function asUnspecified(): UnspecifiedType
677
+	{
678
+		return $this;
679
+	}
680 680
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/PrimitiveString.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -13,41 +13,41 @@
 block discarded – undo
13 13
  */
14 14
 abstract class PrimitiveString extends StringType
15 15
 {
16
-    use PrimitiveType;
16
+	use PrimitiveType;
17 17
     
18
-    /**
19
-     *
20
-     * @see \ASN1\Element::_encodedContentDER()
21
-     * @return string
22
-     */
23
-    protected function _encodedContentDER(): string
24
-    {
25
-        return $this->_string;
26
-    }
18
+	/**
19
+	 *
20
+	 * @see \ASN1\Element::_encodedContentDER()
21
+	 * @return string
22
+	 */
23
+	protected function _encodedContentDER(): string
24
+	{
25
+		return $this->_string;
26
+	}
27 27
     
28
-    /**
29
-     *
30
-     * {@inheritdoc}
31
-     * @see \ASN1\Element::_decodeFromDER()
32
-     * @return self
33
-     */
34
-    protected static function _decodeFromDER(Identifier $identifier,
35
-        string $data, int &$offset): ElementBase
36
-    {
37
-        $idx = $offset;
38
-        if (!$identifier->isPrimitive()) {
39
-            throw new DecodeException("DER encoded string must be primitive.");
40
-        }
41
-        $length = Length::expectFromDER($data, $idx)->intLength();
42
-        $str = $length ? substr($data, $idx, $length) : "";
43
-        // substr should never return false, since length is
44
-        // checked by Length::expectFromDER.
45
-        assert(is_string($str), new DecodeException("substr"));
46
-        $offset = $idx + $length;
47
-        try {
48
-            return new static($str);
49
-        } catch (\InvalidArgumentException $e) {
50
-            throw new DecodeException($e->getMessage(), 0, $e);
51
-        }
52
-    }
28
+	/**
29
+	 *
30
+	 * {@inheritdoc}
31
+	 * @see \ASN1\Element::_decodeFromDER()
32
+	 * @return self
33
+	 */
34
+	protected static function _decodeFromDER(Identifier $identifier,
35
+		string $data, int &$offset): ElementBase
36
+	{
37
+		$idx = $offset;
38
+		if (!$identifier->isPrimitive()) {
39
+			throw new DecodeException("DER encoded string must be primitive.");
40
+		}
41
+		$length = Length::expectFromDER($data, $idx)->intLength();
42
+		$str = $length ? substr($data, $idx, $length) : "";
43
+		// substr should never return false, since length is
44
+		// checked by Length::expectFromDER.
45
+		assert(is_string($str), new DecodeException("substr"));
46
+		$offset = $idx + $length;
47
+		try {
48
+			return new static($str);
49
+		} catch (\InvalidArgumentException $e) {
50
+			throw new DecodeException($e->getMessage(), 0, $e);
51
+		}
52
+	}
53 53
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/Real.php 1 patch
Indentation   +252 added lines, -252 removed lines patch added patch discarded remove patch
@@ -16,273 +16,273 @@
 block discarded – undo
16 16
  */
17 17
 class Real extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Regex pattern to parse NR3 form number conforming to DER.
24
-     *
25
-     * @var string
26
-     */
27
-    const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/';
22
+	/**
23
+	 * Regex pattern to parse NR3 form number conforming to DER.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/';
28 28
     
29
-    /**
30
-     * Regex pattern to parse PHP exponent number format.
31
-     *
32
-     * @link http://php.net/manual/en/language.types.float.php
33
-     * @var string
34
-     */
35
-    const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */
36
-        '([+\-]?'. // sign
37
-        '(?:'.
38
-            '\d+'. // LNUM
39
-            '|'.
40
-            '(?:\d*\.\d+|\d+\.\d*)'. // DNUM
41
-        '))[eE]'.
42
-        '([+\-]?\d+)'. // exponent
43
-    '$/'; /* @formatter:on */
29
+	/**
30
+	 * Regex pattern to parse PHP exponent number format.
31
+	 *
32
+	 * @link http://php.net/manual/en/language.types.float.php
33
+	 * @var string
34
+	 */
35
+	const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */
36
+		'([+\-]?'. // sign
37
+		'(?:'.
38
+			'\d+'. // LNUM
39
+			'|'.
40
+			'(?:\d*\.\d+|\d+\.\d*)'. // DNUM
41
+		'))[eE]'.
42
+		'([+\-]?\d+)'. // exponent
43
+	'$/'; /* @formatter:on */
44 44
     
45
-    /**
46
-     * Number zero represented in NR3 form.
47
-     *
48
-     * @var string
49
-     */
50
-    const NR3_ZERO = ".E+0";
45
+	/**
46
+	 * Number zero represented in NR3 form.
47
+	 *
48
+	 * @var string
49
+	 */
50
+	const NR3_ZERO = ".E+0";
51 51
     
52
-    /**
53
-     * Number in NR3 form.
54
-     *
55
-     * @var string
56
-     */
57
-    private $_number;
52
+	/**
53
+	 * Number in NR3 form.
54
+	 *
55
+	 * @var string
56
+	 */
57
+	private $_number;
58 58
     
59
-    /**
60
-     * Constructor.
61
-     *
62
-     * @param string $number Number in NR3 form.
63
-     */
64
-    public function __construct(string $number)
65
-    {
66
-        $this->_typeTag = self::TYPE_REAL;
67
-        if (!self::_validateNumber($number)) {
68
-            throw new \InvalidArgumentException(
69
-                "'$number' is not a valid NR3 form real.");
70
-        }
71
-        $this->_number = $number;
72
-    }
59
+	/**
60
+	 * Constructor.
61
+	 *
62
+	 * @param string $number Number in NR3 form.
63
+	 */
64
+	public function __construct(string $number)
65
+	{
66
+		$this->_typeTag = self::TYPE_REAL;
67
+		if (!self::_validateNumber($number)) {
68
+			throw new \InvalidArgumentException(
69
+				"'$number' is not a valid NR3 form real.");
70
+		}
71
+		$this->_number = $number;
72
+	}
73 73
     
74
-    /**
75
-     * Initialize from float.
76
-     *
77
-     * @param float $number
78
-     * @return self
79
-     */
80
-    public static function fromFloat(float $number): self
81
-    {
82
-        return new self(self::_decimalToNR3(strval($number)));
83
-    }
74
+	/**
75
+	 * Initialize from float.
76
+	 *
77
+	 * @param float $number
78
+	 * @return self
79
+	 */
80
+	public static function fromFloat(float $number): self
81
+	{
82
+		return new self(self::_decimalToNR3(strval($number)));
83
+	}
84 84
     
85
-    /**
86
-     * Get number as a float.
87
-     *
88
-     * @return float
89
-     */
90
-    public function float(): float
91
-    {
92
-        return self::_nr3ToDecimal($this->_number);
93
-    }
85
+	/**
86
+	 * Get number as a float.
87
+	 *
88
+	 * @return float
89
+	 */
90
+	public function float(): float
91
+	{
92
+		return self::_nr3ToDecimal($this->_number);
93
+	}
94 94
     
95
-    /**
96
-     *
97
-     * {@inheritdoc}
98
-     */
99
-    protected function _encodedContentDER(): string
100
-    {
101
-        /* if the real value is the value zero, there shall be no contents
95
+	/**
96
+	 *
97
+	 * {@inheritdoc}
98
+	 */
99
+	protected function _encodedContentDER(): string
100
+	{
101
+		/* if the real value is the value zero, there shall be no contents
102 102
          octets in the encoding. (X.690 07-2002, section 8.5.2) */
103
-        if (self::NR3_ZERO == $this->_number) {
104
-            return "";
105
-        }
106
-        // encode in NR3 decimal encoding
107
-        $data = chr(0x03) . $this->_number;
108
-        return $data;
109
-    }
103
+		if (self::NR3_ZERO == $this->_number) {
104
+			return "";
105
+		}
106
+		// encode in NR3 decimal encoding
107
+		$data = chr(0x03) . $this->_number;
108
+		return $data;
109
+	}
110 110
     
111
-    /**
112
-     *
113
-     * {@inheritdoc}
114
-     * @return self
115
-     */
116
-    protected static function _decodeFromDER(Identifier $identifier,
117
-        string $data, int &$offset): ElementBase
118
-    {
119
-        $idx = $offset;
120
-        $length = Length::expectFromDER($data, $idx)->intLength();
121
-        // if length is zero, value is zero (spec 8.5.2)
122
-        if (!$length) {
123
-            $obj = new self(self::NR3_ZERO);
124
-        } else {
125
-            $bytes = substr($data, $idx, $length);
126
-            $byte = ord($bytes[0]);
127
-            if (0x80 & $byte) { // bit 8 = 1
128
-                $obj = self::_decodeBinaryEncoding($bytes);
129
-            } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0
130
-                $obj = self::_decodeDecimalEncoding($bytes);
131
-            } else { // bit 8 = 0, bit 7 = 1
132
-                $obj = self::_decodeSpecialRealValue($bytes);
133
-            }
134
-        }
135
-        $offset = $idx + $length;
136
-        return $obj;
137
-    }
111
+	/**
112
+	 *
113
+	 * {@inheritdoc}
114
+	 * @return self
115
+	 */
116
+	protected static function _decodeFromDER(Identifier $identifier,
117
+		string $data, int &$offset): ElementBase
118
+	{
119
+		$idx = $offset;
120
+		$length = Length::expectFromDER($data, $idx)->intLength();
121
+		// if length is zero, value is zero (spec 8.5.2)
122
+		if (!$length) {
123
+			$obj = new self(self::NR3_ZERO);
124
+		} else {
125
+			$bytes = substr($data, $idx, $length);
126
+			$byte = ord($bytes[0]);
127
+			if (0x80 & $byte) { // bit 8 = 1
128
+				$obj = self::_decodeBinaryEncoding($bytes);
129
+			} else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0
130
+				$obj = self::_decodeDecimalEncoding($bytes);
131
+			} else { // bit 8 = 0, bit 7 = 1
132
+				$obj = self::_decodeSpecialRealValue($bytes);
133
+			}
134
+		}
135
+		$offset = $idx + $length;
136
+		return $obj;
137
+	}
138 138
     
139
-    /**
140
-     *
141
-     * @todo Implement
142
-     * @param string $data
143
-     */
144
-    protected static function _decodeBinaryEncoding(string $data)
145
-    {
146
-        throw new \RuntimeException(
147
-            "Binary encoding of REAL is not implemented.");
148
-    }
139
+	/**
140
+	 *
141
+	 * @todo Implement
142
+	 * @param string $data
143
+	 */
144
+	protected static function _decodeBinaryEncoding(string $data)
145
+	{
146
+		throw new \RuntimeException(
147
+			"Binary encoding of REAL is not implemented.");
148
+	}
149 149
     
150
-    /**
151
-     *
152
-     * @param string $data
153
-     * @throws \RuntimeException
154
-     * @return \ASN1\Type\Primitive\Real
155
-     */
156
-    protected static function _decodeDecimalEncoding(string $data): Real
157
-    {
158
-        $nr = ord($data[0]) & 0x03;
159
-        if ($nr != 0x03) {
160
-            throw new \RuntimeException("Only NR3 form supported.");
161
-        }
162
-        $str = substr($data, 1);
163
-        return new self($str);
164
-    }
150
+	/**
151
+	 *
152
+	 * @param string $data
153
+	 * @throws \RuntimeException
154
+	 * @return \ASN1\Type\Primitive\Real
155
+	 */
156
+	protected static function _decodeDecimalEncoding(string $data): Real
157
+	{
158
+		$nr = ord($data[0]) & 0x03;
159
+		if ($nr != 0x03) {
160
+			throw new \RuntimeException("Only NR3 form supported.");
161
+		}
162
+		$str = substr($data, 1);
163
+		return new self($str);
164
+	}
165 165
     
166
-    /**
167
-     *
168
-     * @todo Implement
169
-     * @param string $data
170
-     */
171
-    protected static function _decodeSpecialRealValue(string $data)
172
-    {
173
-        if (strlen($data) != 1) {
174
-            throw new DecodeException(
175
-                "SpecialRealValue must have one content octet.");
176
-        }
177
-        $byte = ord($data[0]);
178
-        if ($byte == 0x40) { // positive infinity
179
-            throw new \RuntimeException("PLUS-INFINITY not supported.");
180
-        } else if ($byte == 0x41) { // negative infinity
181
-            throw new \RuntimeException("MINUS-INFINITY not supported.");
182
-        } else {
183
-            throw new DecodeException("Invalid SpecialRealValue encoding.");
184
-        }
185
-    }
166
+	/**
167
+	 *
168
+	 * @todo Implement
169
+	 * @param string $data
170
+	 */
171
+	protected static function _decodeSpecialRealValue(string $data)
172
+	{
173
+		if (strlen($data) != 1) {
174
+			throw new DecodeException(
175
+				"SpecialRealValue must have one content octet.");
176
+		}
177
+		$byte = ord($data[0]);
178
+		if ($byte == 0x40) { // positive infinity
179
+			throw new \RuntimeException("PLUS-INFINITY not supported.");
180
+		} else if ($byte == 0x41) { // negative infinity
181
+			throw new \RuntimeException("MINUS-INFINITY not supported.");
182
+		} else {
183
+			throw new DecodeException("Invalid SpecialRealValue encoding.");
184
+		}
185
+	}
186 186
     
187
-    /**
188
-     * Convert decimal number string to NR3 form.
189
-     *
190
-     * @param string $str
191
-     * @return string
192
-     */
193
-    private static function _decimalToNR3(string $str): string
194
-    {
195
-        // if number is in exponent form
196
-        /** @var $match string[] */
197
-        if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) {
198
-            $parts = explode(".", $match[1]);
199
-            $m = ltrim($parts[0], "0");
200
-            $e = intval($match[2]);
201
-            // if mantissa had decimals
202
-            if (count($parts) == 2) {
203
-                $d = rtrim($parts[1], "0");
204
-                $e -= strlen($d);
205
-                $m .= $d;
206
-            }
207
-        } else {
208
-            // explode from decimal
209
-            $parts = explode(".", $str);
210
-            $m = ltrim($parts[0], "0");
211
-            // if number had decimals
212
-            if (count($parts) == 2) {
213
-                // exponent is negative number of the decimals
214
-                $e = -strlen($parts[1]);
215
-                // append decimals to the mantissa
216
-                $m .= $parts[1];
217
-            } else {
218
-                $e = 0;
219
-            }
220
-            // shift trailing zeroes from the mantissa to the exponent
221
-            while (substr($m, -1) === "0") {
222
-                $e++;
223
-                $m = substr($m, 0, -1);
224
-            }
225
-        }
226
-        /* if exponent is zero, it must be prefixed with a "+" sign
187
+	/**
188
+	 * Convert decimal number string to NR3 form.
189
+	 *
190
+	 * @param string $str
191
+	 * @return string
192
+	 */
193
+	private static function _decimalToNR3(string $str): string
194
+	{
195
+		// if number is in exponent form
196
+		/** @var $match string[] */
197
+		if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) {
198
+			$parts = explode(".", $match[1]);
199
+			$m = ltrim($parts[0], "0");
200
+			$e = intval($match[2]);
201
+			// if mantissa had decimals
202
+			if (count($parts) == 2) {
203
+				$d = rtrim($parts[1], "0");
204
+				$e -= strlen($d);
205
+				$m .= $d;
206
+			}
207
+		} else {
208
+			// explode from decimal
209
+			$parts = explode(".", $str);
210
+			$m = ltrim($parts[0], "0");
211
+			// if number had decimals
212
+			if (count($parts) == 2) {
213
+				// exponent is negative number of the decimals
214
+				$e = -strlen($parts[1]);
215
+				// append decimals to the mantissa
216
+				$m .= $parts[1];
217
+			} else {
218
+				$e = 0;
219
+			}
220
+			// shift trailing zeroes from the mantissa to the exponent
221
+			while (substr($m, -1) === "0") {
222
+				$e++;
223
+				$m = substr($m, 0, -1);
224
+			}
225
+		}
226
+		/* if exponent is zero, it must be prefixed with a "+" sign
227 227
          (X.690 07-2002, section 11.3.2.6) */
228
-        if (0 == $e) {
229
-            $es = "+";
230
-        } else {
231
-            $es = $e < 0 ? "-" : "";
232
-        }
233
-        return sprintf("%s.E%s%d", $m, $es, abs($e));
234
-    }
228
+		if (0 == $e) {
229
+			$es = "+";
230
+		} else {
231
+			$es = $e < 0 ? "-" : "";
232
+		}
233
+		return sprintf("%s.E%s%d", $m, $es, abs($e));
234
+	}
235 235
     
236
-    /**
237
-     * Convert NR3 form number to decimal.
238
-     *
239
-     * @param string $str
240
-     * @throws \UnexpectedValueException
241
-     * @return float
242
-     */
243
-    private static function _nr3ToDecimal(string $str): float
244
-    {
245
-        /** @var $match string[] */
246
-        if (!preg_match(self::NR3_REGEX, $str, $match)) {
247
-            throw new \UnexpectedValueException(
248
-                "'$str' is not a valid NR3 form real.");
249
-        }
250
-        $m = $match[2];
251
-        // if number started with minus sign
252
-        $inv = $match[1] == "-";
253
-        $e = intval($match[3]);
254
-        // positive exponent
255
-        if ($e > 0) {
256
-            // pad with trailing zeroes
257
-            $num = $m . str_repeat("0", $e);
258
-        } else if ($e < 0) {
259
-            // pad with leading zeroes
260
-            if (strlen($m) < abs($e)) {
261
-                $m = str_repeat("0", abs($e) - strlen($m)) . $m;
262
-            }
263
-            // insert decimal point
264
-            $num = substr($m, 0, $e) . "." . substr($m, $e);
265
-        } else {
266
-            $num = empty($m) ? "0" : $m;
267
-        }
268
-        // if number is negative
269
-        if ($inv) {
270
-            $num = "-$num";
271
-        }
272
-        return floatval($num);
273
-    }
236
+	/**
237
+	 * Convert NR3 form number to decimal.
238
+	 *
239
+	 * @param string $str
240
+	 * @throws \UnexpectedValueException
241
+	 * @return float
242
+	 */
243
+	private static function _nr3ToDecimal(string $str): float
244
+	{
245
+		/** @var $match string[] */
246
+		if (!preg_match(self::NR3_REGEX, $str, $match)) {
247
+			throw new \UnexpectedValueException(
248
+				"'$str' is not a valid NR3 form real.");
249
+		}
250
+		$m = $match[2];
251
+		// if number started with minus sign
252
+		$inv = $match[1] == "-";
253
+		$e = intval($match[3]);
254
+		// positive exponent
255
+		if ($e > 0) {
256
+			// pad with trailing zeroes
257
+			$num = $m . str_repeat("0", $e);
258
+		} else if ($e < 0) {
259
+			// pad with leading zeroes
260
+			if (strlen($m) < abs($e)) {
261
+				$m = str_repeat("0", abs($e) - strlen($m)) . $m;
262
+			}
263
+			// insert decimal point
264
+			$num = substr($m, 0, $e) . "." . substr($m, $e);
265
+		} else {
266
+			$num = empty($m) ? "0" : $m;
267
+		}
268
+		// if number is negative
269
+		if ($inv) {
270
+			$num = "-$num";
271
+		}
272
+		return floatval($num);
273
+	}
274 274
     
275
-    /**
276
-     * Test that number is valid for this context.
277
-     *
278
-     * @param mixed $num
279
-     * @return bool
280
-     */
281
-    private static function _validateNumber($num): bool
282
-    {
283
-        if (!preg_match(self::NR3_REGEX, $num)) {
284
-            return false;
285
-        }
286
-        return true;
287
-    }
275
+	/**
276
+	 * Test that number is valid for this context.
277
+	 *
278
+	 * @param mixed $num
279
+	 * @return bool
280
+	 */
281
+	private static function _validateNumber($num): bool
282
+	{
283
+		if (!preg_match(self::NR3_REGEX, $num)) {
284
+			return false;
285
+		}
286
+		return true;
287
+	}
288 288
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/ObjectIdentifier.php 1 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.
lib/ASN1/Type/Primitive/UTCTime.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -16,73 +16,73 @@
 block discarded – undo
16 16
  */
17 17
 class UTCTime extends TimeType
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Regular expression to parse date.
24
-     *
25
-     * DER restricts format to UTC timezone (Z suffix).
26
-     *
27
-     * @var string
28
-     */
29
-    const REGEX = /* @formatter:off */ '#^' .
30
-        '(\d\d)' . /* YY */
31
-        '(\d\d)' . /* MM */
32
-        '(\d\d)' . /* DD */
33
-        '(\d\d)' . /* hh */
34
-        '(\d\d)' . /* mm */
35
-        '(\d\d)' . /* ss */
36
-        'Z' . /* TZ */
37
-        '$#' /* @formatter:on */;
22
+	/**
23
+	 * Regular expression to parse date.
24
+	 *
25
+	 * DER restricts format to UTC timezone (Z suffix).
26
+	 *
27
+	 * @var string
28
+	 */
29
+	const REGEX = /* @formatter:off */ '#^' .
30
+		'(\d\d)' . /* YY */
31
+		'(\d\d)' . /* MM */
32
+		'(\d\d)' . /* DD */
33
+		'(\d\d)' . /* hh */
34
+		'(\d\d)' . /* mm */
35
+		'(\d\d)' . /* ss */
36
+		'Z' . /* TZ */
37
+		'$#' /* @formatter:on */;
38 38
     
39
-    /**
40
-     * Constructor.
41
-     *
42
-     * @param \DateTimeImmutable $dt
43
-     */
44
-    public function __construct(\DateTimeImmutable $dt)
45
-    {
46
-        $this->_typeTag = self::TYPE_UTC_TIME;
47
-        parent::__construct($dt);
48
-    }
39
+	/**
40
+	 * Constructor.
41
+	 *
42
+	 * @param \DateTimeImmutable $dt
43
+	 */
44
+	public function __construct(\DateTimeImmutable $dt)
45
+	{
46
+		$this->_typeTag = self::TYPE_UTC_TIME;
47
+		parent::__construct($dt);
48
+	}
49 49
     
50
-    /**
51
-     *
52
-     * {@inheritdoc}
53
-     */
54
-    protected function _encodedContentDER(): string
55
-    {
56
-        $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC));
57
-        return $dt->format("ymdHis\Z");
58
-    }
50
+	/**
51
+	 *
52
+	 * {@inheritdoc}
53
+	 */
54
+	protected function _encodedContentDER(): string
55
+	{
56
+		$dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC));
57
+		return $dt->format("ymdHis\Z");
58
+	}
59 59
     
60
-    /**
61
-     *
62
-     * {@inheritdoc}
63
-     * @return self
64
-     */
65
-    protected static function _decodeFromDER(Identifier $identifier,
66
-        string $data, int &$offset): ElementBase
67
-    {
68
-        $idx = $offset;
69
-        $length = Length::expectFromDER($data, $idx)->intLength();
70
-        $str = substr($data, $idx, $length);
71
-        $idx += $length;
72
-        /** @var $match string[] */
73
-        if (!preg_match(self::REGEX, $str, $match)) {
74
-            throw new DecodeException("Invalid UTCTime format.");
75
-        }
76
-        list(, $year, $month, $day, $hour, $minute, $second) = $match;
77
-        $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC;
78
-        $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time,
79
-            self::_createTimeZone(self::TZ_UTC));
80
-        if (!$dt) {
81
-            throw new DecodeException(
82
-                "Failed to decode UTCTime: " .
83
-                self::_getLastDateTimeImmutableErrorsStr());
84
-        }
85
-        $offset = $idx;
86
-        return new self($dt);
87
-    }
60
+	/**
61
+	 *
62
+	 * {@inheritdoc}
63
+	 * @return self
64
+	 */
65
+	protected static function _decodeFromDER(Identifier $identifier,
66
+		string $data, int &$offset): ElementBase
67
+	{
68
+		$idx = $offset;
69
+		$length = Length::expectFromDER($data, $idx)->intLength();
70
+		$str = substr($data, $idx, $length);
71
+		$idx += $length;
72
+		/** @var $match string[] */
73
+		if (!preg_match(self::REGEX, $str, $match)) {
74
+			throw new DecodeException("Invalid UTCTime format.");
75
+		}
76
+		list(, $year, $month, $day, $hour, $minute, $second) = $match;
77
+		$time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC;
78
+		$dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time,
79
+			self::_createTimeZone(self::TZ_UTC));
80
+		if (!$dt) {
81
+			throw new DecodeException(
82
+				"Failed to decode UTCTime: " .
83
+				self::_getLastDateTimeImmutableErrorsStr());
84
+		}
85
+		$offset = $idx;
86
+		return new self($dt);
87
+	}
88 88
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/Boolean.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -16,64 +16,64 @@
 block discarded – undo
16 16
  */
17 17
 class Boolean extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Value.
24
-     *
25
-     * @var bool
26
-     */
27
-    private $_bool;
22
+	/**
23
+	 * Value.
24
+	 *
25
+	 * @var bool
26
+	 */
27
+	private $_bool;
28 28
     
29
-    /**
30
-     * Constructor.
31
-     *
32
-     * @param bool $bool
33
-     */
34
-    public function __construct(bool $bool)
35
-    {
36
-        $this->_typeTag = self::TYPE_BOOLEAN;
37
-        $this->_bool = $bool;
38
-    }
29
+	/**
30
+	 * Constructor.
31
+	 *
32
+	 * @param bool $bool
33
+	 */
34
+	public function __construct(bool $bool)
35
+	{
36
+		$this->_typeTag = self::TYPE_BOOLEAN;
37
+		$this->_bool = $bool;
38
+	}
39 39
     
40
-    /**
41
-     * Get the value.
42
-     *
43
-     * @return bool
44
-     */
45
-    public function value(): bool
46
-    {
47
-        return $this->_bool;
48
-    }
40
+	/**
41
+	 * Get the value.
42
+	 *
43
+	 * @return bool
44
+	 */
45
+	public function value(): bool
46
+	{
47
+		return $this->_bool;
48
+	}
49 49
     
50
-    /**
51
-     *
52
-     * {@inheritdoc}
53
-     */
54
-    protected function _encodedContentDER(): string
55
-    {
56
-        return $this->_bool ? chr(0xff) : chr(0);
57
-    }
50
+	/**
51
+	 *
52
+	 * {@inheritdoc}
53
+	 */
54
+	protected function _encodedContentDER(): string
55
+	{
56
+		return $this->_bool ? chr(0xff) : chr(0);
57
+	}
58 58
     
59
-    /**
60
-     *
61
-     * {@inheritdoc}
62
-     * @return self
63
-     */
64
-    protected static function _decodeFromDER(Identifier $identifier,
65
-        string $data, int &$offset): ElementBase
66
-    {
67
-        $idx = $offset;
68
-        Length::expectFromDER($data, $idx, 1);
69
-        $byte = ord($data[$idx++]);
70
-        if ($byte !== 0) {
71
-            if ($byte != 0xff) {
72
-                throw new DecodeException(
73
-                    "DER encoded boolean true must have all bits set to 1.");
74
-            }
75
-        }
76
-        $offset = $idx;
77
-        return new self($byte !== 0);
78
-    }
59
+	/**
60
+	 *
61
+	 * {@inheritdoc}
62
+	 * @return self
63
+	 */
64
+	protected static function _decodeFromDER(Identifier $identifier,
65
+		string $data, int &$offset): ElementBase
66
+	{
67
+		$idx = $offset;
68
+		Length::expectFromDER($data, $idx, 1);
69
+		$byte = ord($data[$idx++]);
70
+		if ($byte !== 0) {
71
+			if ($byte != 0xff) {
72
+				throw new DecodeException(
73
+					"DER encoded boolean true must have all bits set to 1.");
74
+			}
75
+		}
76
+		$offset = $idx;
77
+		return new self($byte !== 0);
78
+	}
79 79
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/BitString.php 1 patch
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -16,195 +16,195 @@
 block discarded – undo
16 16
  */
17 17
 class BitString extends StringType
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Number of unused bits in the last octet.
24
-     *
25
-     * @var int $_unusedBits
26
-     */
27
-    protected $_unusedBits;
22
+	/**
23
+	 * Number of unused bits in the last octet.
24
+	 *
25
+	 * @var int $_unusedBits
26
+	 */
27
+	protected $_unusedBits;
28 28
     
29
-    /**
30
-     * Constructor.
31
-     *
32
-     * @param string $string Content octets
33
-     * @param int $unused_bits Number of unused bits in the last octet
34
-     */
35
-    public function __construct(string $string, int $unused_bits = 0)
36
-    {
37
-        $this->_typeTag = self::TYPE_BIT_STRING;
38
-        parent::__construct($string);
39
-        $this->_unusedBits = $unused_bits;
40
-    }
29
+	/**
30
+	 * Constructor.
31
+	 *
32
+	 * @param string $string Content octets
33
+	 * @param int $unused_bits Number of unused bits in the last octet
34
+	 */
35
+	public function __construct(string $string, int $unused_bits = 0)
36
+	{
37
+		$this->_typeTag = self::TYPE_BIT_STRING;
38
+		parent::__construct($string);
39
+		$this->_unusedBits = $unused_bits;
40
+	}
41 41
     
42
-    /**
43
-     * Get the number of bits in the string.
44
-     *
45
-     * @return int
46
-     */
47
-    public function numBits(): int
48
-    {
49
-        return strlen($this->_string) * 8 - $this->_unusedBits;
50
-    }
42
+	/**
43
+	 * Get the number of bits in the string.
44
+	 *
45
+	 * @return int
46
+	 */
47
+	public function numBits(): int
48
+	{
49
+		return strlen($this->_string) * 8 - $this->_unusedBits;
50
+	}
51 51
     
52
-    /**
53
-     * Get the number of unused bits in the last octet of the string.
54
-     *
55
-     * @return int
56
-     */
57
-    public function unusedBits(): int
58
-    {
59
-        return $this->_unusedBits;
60
-    }
52
+	/**
53
+	 * Get the number of unused bits in the last octet of the string.
54
+	 *
55
+	 * @return int
56
+	 */
57
+	public function unusedBits(): int
58
+	{
59
+		return $this->_unusedBits;
60
+	}
61 61
     
62
-    /**
63
-     * Test whether bit is set.
64
-     *
65
-     * @param int $idx Bit index.
66
-     *        Most significant bit of the first octet is index 0.
67
-     * @return bool
68
-     */
69
-    public function testBit(int $idx): bool
70
-    {
71
-        // octet index
72
-        $oi = (int) floor($idx / 8);
73
-        // if octet is outside range
74
-        if ($oi < 0 || $oi >= strlen($this->_string)) {
75
-            throw new \OutOfBoundsException("Index is out of bounds.");
76
-        }
77
-        // bit index
78
-        $bi = $idx % 8;
79
-        // if tested bit is last octet's unused bit
80
-        if ($oi == strlen($this->_string) - 1) {
81
-            if ($bi >= 8 - $this->_unusedBits) {
82
-                throw new \OutOfBoundsException(
83
-                    "Index refers to an unused bit.");
84
-            }
85
-        }
86
-        $byte = $this->_string[$oi];
87
-        // index 0 is the most significant bit in byte
88
-        $mask = 0x01 << (7 - $bi);
89
-        return (ord($byte) & $mask) > 0;
90
-    }
62
+	/**
63
+	 * Test whether bit is set.
64
+	 *
65
+	 * @param int $idx Bit index.
66
+	 *        Most significant bit of the first octet is index 0.
67
+	 * @return bool
68
+	 */
69
+	public function testBit(int $idx): bool
70
+	{
71
+		// octet index
72
+		$oi = (int) floor($idx / 8);
73
+		// if octet is outside range
74
+		if ($oi < 0 || $oi >= strlen($this->_string)) {
75
+			throw new \OutOfBoundsException("Index is out of bounds.");
76
+		}
77
+		// bit index
78
+		$bi = $idx % 8;
79
+		// if tested bit is last octet's unused bit
80
+		if ($oi == strlen($this->_string) - 1) {
81
+			if ($bi >= 8 - $this->_unusedBits) {
82
+				throw new \OutOfBoundsException(
83
+					"Index refers to an unused bit.");
84
+			}
85
+		}
86
+		$byte = $this->_string[$oi];
87
+		// index 0 is the most significant bit in byte
88
+		$mask = 0x01 << (7 - $bi);
89
+		return (ord($byte) & $mask) > 0;
90
+	}
91 91
     
92
-    /**
93
-     * Get range of bits.
94
-     *
95
-     * @param int $start Index of first bit
96
-     * @param int $length Number of bits in range
97
-     * @throws \OutOfBoundsException
98
-     * @return string Integer of $length bits
99
-     */
100
-    public function range(int $start, int $length): string
101
-    {
102
-        if (!$length) {
103
-            return "0";
104
-        }
105
-        if ($start + $length > $this->numBits()) {
106
-            throw new \OutOfBoundsException("Not enough bits.");
107
-        }
108
-        $bits = gmp_init(0);
109
-        $idx = $start;
110
-        $end = $start + $length;
111
-        while (true) {
112
-            $bit = $this->testBit($idx) ? 1 : 0;
113
-            $bits |= $bit;
114
-            if (++$idx >= $end) {
115
-                break;
116
-            }
117
-            $bits <<= 1;
118
-        }
119
-        return gmp_strval($bits, 10);
120
-    }
92
+	/**
93
+	 * Get range of bits.
94
+	 *
95
+	 * @param int $start Index of first bit
96
+	 * @param int $length Number of bits in range
97
+	 * @throws \OutOfBoundsException
98
+	 * @return string Integer of $length bits
99
+	 */
100
+	public function range(int $start, int $length): string
101
+	{
102
+		if (!$length) {
103
+			return "0";
104
+		}
105
+		if ($start + $length > $this->numBits()) {
106
+			throw new \OutOfBoundsException("Not enough bits.");
107
+		}
108
+		$bits = gmp_init(0);
109
+		$idx = $start;
110
+		$end = $start + $length;
111
+		while (true) {
112
+			$bit = $this->testBit($idx) ? 1 : 0;
113
+			$bits |= $bit;
114
+			if (++$idx >= $end) {
115
+				break;
116
+			}
117
+			$bits <<= 1;
118
+		}
119
+		return gmp_strval($bits, 10);
120
+	}
121 121
     
122
-    /**
123
-     * Get a copy of the bit string with trailing zeroes removed.
124
-     *
125
-     * @return self
126
-     */
127
-    public function withoutTrailingZeroes(): self
128
-    {
129
-        // if bit string was empty
130
-        if (!strlen($this->_string)) {
131
-            return new self("");
132
-        }
133
-        $bits = $this->_string;
134
-        // count number of empty trailing octets
135
-        $unused_octets = 0;
136
-        for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) {
137
-            if ($bits[$idx] != "\x0") {
138
-                break;
139
-            }
140
-        }
141
-        // strip trailing octets
142
-        if ($unused_octets) {
143
-            $bits = substr($bits, 0, -$unused_octets);
144
-        }
145
-        // if bit string was full of zeroes
146
-        if (!strlen($bits)) {
147
-            return new self("");
148
-        }
149
-        // count number of trailing zeroes in the last octet
150
-        $unused_bits = 0;
151
-        $byte = ord($bits[strlen($bits) - 1]);
152
-        while (!($byte & 0x01)) {
153
-            $unused_bits++;
154
-            $byte >>= 1;
155
-        }
156
-        return new self($bits, $unused_bits);
157
-    }
122
+	/**
123
+	 * Get a copy of the bit string with trailing zeroes removed.
124
+	 *
125
+	 * @return self
126
+	 */
127
+	public function withoutTrailingZeroes(): self
128
+	{
129
+		// if bit string was empty
130
+		if (!strlen($this->_string)) {
131
+			return new self("");
132
+		}
133
+		$bits = $this->_string;
134
+		// count number of empty trailing octets
135
+		$unused_octets = 0;
136
+		for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) {
137
+			if ($bits[$idx] != "\x0") {
138
+				break;
139
+			}
140
+		}
141
+		// strip trailing octets
142
+		if ($unused_octets) {
143
+			$bits = substr($bits, 0, -$unused_octets);
144
+		}
145
+		// if bit string was full of zeroes
146
+		if (!strlen($bits)) {
147
+			return new self("");
148
+		}
149
+		// count number of trailing zeroes in the last octet
150
+		$unused_bits = 0;
151
+		$byte = ord($bits[strlen($bits) - 1]);
152
+		while (!($byte & 0x01)) {
153
+			$unused_bits++;
154
+			$byte >>= 1;
155
+		}
156
+		return new self($bits, $unused_bits);
157
+	}
158 158
     
159
-    /**
160
-     *
161
-     * {@inheritdoc}
162
-     */
163
-    protected function _encodedContentDER(): string
164
-    {
165
-        $der = chr($this->_unusedBits);
166
-        $der .= $this->_string;
167
-        if ($this->_unusedBits) {
168
-            $octet = $der[strlen($der) - 1];
169
-            // set unused bits to zero
170
-            $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1));
171
-            $der[strlen($der) - 1] = $octet;
172
-        }
173
-        return $der;
174
-    }
159
+	/**
160
+	 *
161
+	 * {@inheritdoc}
162
+	 */
163
+	protected function _encodedContentDER(): string
164
+	{
165
+		$der = chr($this->_unusedBits);
166
+		$der .= $this->_string;
167
+		if ($this->_unusedBits) {
168
+			$octet = $der[strlen($der) - 1];
169
+			// set unused bits to zero
170
+			$octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1));
171
+			$der[strlen($der) - 1] = $octet;
172
+		}
173
+		return $der;
174
+	}
175 175
     
176
-    /**
177
-     *
178
-     * {@inheritdoc}
179
-     * @return self
180
-     */
181
-    protected static function _decodeFromDER(Identifier $identifier,
182
-        string $data, int &$offset): ElementBase
183
-    {
184
-        $idx = $offset;
185
-        $length = Length::expectFromDER($data, $idx);
186
-        if ($length->intLength() < 1) {
187
-            throw new DecodeException("Bit string length must be at least 1.");
188
-        }
189
-        $unused_bits = ord($data[$idx++]);
190
-        if ($unused_bits > 7) {
191
-            throw new DecodeException(
192
-                "Unused bits in a bit string must be less than 8.");
193
-        }
194
-        $str_len = $length->intLength() - 1;
195
-        if ($str_len) {
196
-            $str = substr($data, $idx, $str_len);
197
-            if ($unused_bits) {
198
-                $mask = (1 << $unused_bits) - 1;
199
-                if (ord($str[strlen($str) - 1]) & $mask) {
200
-                    throw new DecodeException(
201
-                        "DER encoded bit string must have zero padding.");
202
-                }
203
-            }
204
-        } else {
205
-            $str = "";
206
-        }
207
-        $offset = $idx + $str_len;
208
-        return new self($str, $unused_bits);
209
-    }
176
+	/**
177
+	 *
178
+	 * {@inheritdoc}
179
+	 * @return self
180
+	 */
181
+	protected static function _decodeFromDER(Identifier $identifier,
182
+		string $data, int &$offset): ElementBase
183
+	{
184
+		$idx = $offset;
185
+		$length = Length::expectFromDER($data, $idx);
186
+		if ($length->intLength() < 1) {
187
+			throw new DecodeException("Bit string length must be at least 1.");
188
+		}
189
+		$unused_bits = ord($data[$idx++]);
190
+		if ($unused_bits > 7) {
191
+			throw new DecodeException(
192
+				"Unused bits in a bit string must be less than 8.");
193
+		}
194
+		$str_len = $length->intLength() - 1;
195
+		if ($str_len) {
196
+			$str = substr($data, $idx, $str_len);
197
+			if ($unused_bits) {
198
+				$mask = (1 << $unused_bits) - 1;
199
+				if (ord($str[strlen($str) - 1]) & $mask) {
200
+					throw new DecodeException(
201
+						"DER encoded bit string must have zero padding.");
202
+				}
203
+			}
204
+		} else {
205
+			$str = "";
206
+		}
207
+		$offset = $idx + $str_len;
208
+		return new self($str, $unused_bits);
209
+	}
210 210
 }
Please login to merge, or discard this patch.