GitHub Access Token became invalid

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