1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ocsp\Asn1\Element; |
4
|
|
|
|
5
|
|
|
use Ocsp\Asn1\Encoder; |
6
|
|
|
use Ocsp\Asn1\TaggableElement; |
7
|
|
|
use Ocsp\Asn1\TaggableElementTrait; |
8
|
|
|
use Ocsp\Asn1\UniversalTagID; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ASN.1 element: BIT STRING. |
12
|
|
|
*/ |
13
|
|
|
class BitString implements TaggableElement |
14
|
|
|
{ |
15
|
|
|
use TaggableElementTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The bytes containing the value bits. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $bytes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The number of unused bits in the last byte. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $numTrailingBits; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new instance. |
33
|
|
|
* |
34
|
|
|
* @param string $bytes the bytes containing the value bits |
35
|
|
|
* @param int $numTrailingBits the number of unused bits in the last byte |
36
|
|
|
* |
37
|
|
|
* @return static |
38
|
|
|
*/ |
39
|
3 |
|
public static function create($bytes, $numTrailingBits) |
40
|
|
|
{ |
41
|
3 |
|
$result = new static(); |
42
|
|
|
|
43
|
3 |
|
return $result->setValue($bytes, $numTrailingBits); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
* |
49
|
|
|
* @see \Ocsp\Asn1\Element::getClass() |
50
|
|
|
*/ |
51
|
2 |
|
public function getClass() |
52
|
|
|
{ |
53
|
2 |
|
return static::CLASS_UNIVERSAL; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @see \Ocsp\Asn1\Element::getTypeID() |
60
|
|
|
*/ |
61
|
2 |
|
public function getTypeID() |
62
|
|
|
{ |
63
|
2 |
|
return UniversalTagID::BIT_STRING; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
* |
69
|
|
|
* @see \Ocsp\Asn1\Element::isConstructed() |
70
|
|
|
*/ |
71
|
|
|
public function isConstructed() |
72
|
|
|
{ |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the bytes containing the value bits. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public function getBytes() |
82
|
|
|
{ |
83
|
2 |
|
return $this->bytes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the number of unused bits in the last byte. |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getNumTrailingBits() |
92
|
|
|
{ |
93
|
|
|
return $this->numTrailingBits; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Change the value of this BIT STRING. |
98
|
|
|
* |
99
|
|
|
* @param string $bytes the bytes containing the value bits |
100
|
|
|
* @param int $numTrailingBits the number of unused bits in the last byte |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
3 |
|
public function setValue($bytes, $numTrailingBits) |
105
|
|
|
{ |
106
|
3 |
|
$this->bytes = (string) $bytes; |
107
|
3 |
|
$this->numTrailingBits = (int) $numTrailingBits; |
108
|
|
|
|
109
|
3 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
* |
115
|
|
|
* @see \Ocsp\Asn1\Element::getEncodedValue() |
116
|
|
|
*/ |
117
|
|
|
public function getEncodedValue(Encoder $encoder) |
118
|
|
|
{ |
119
|
|
|
return $encoder->encodeBitString($this->getBytes(), $this->getNumTrailingBits()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|