@@ -20,105 +20,105 @@ |
||
| 20 | 20 | * May be encoded back to complete DER encoding. |
| 21 | 21 | */ |
| 22 | 22 | class DERTaggedType extends TaggedType implements |
| 23 | - ExplicitTagging, |
|
| 24 | - ImplicitTagging |
|
| 23 | + ExplicitTagging, |
|
| 24 | + ImplicitTagging |
|
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Identifier. |
|
| 28 | - * |
|
| 29 | - * @var Identifier |
|
| 30 | - */ |
|
| 31 | - private $_identifier; |
|
| 26 | + /** |
|
| 27 | + * Identifier. |
|
| 28 | + * |
|
| 29 | + * @var Identifier |
|
| 30 | + */ |
|
| 31 | + private $_identifier; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * DER data. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $_data; |
|
| 33 | + /** |
|
| 34 | + * DER data. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $_data; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Offset to data. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_offset; |
|
| 40 | + /** |
|
| 41 | + * Offset to data. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_offset; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor. |
|
| 49 | - * |
|
| 50 | - * @param Identifier $identifier |
|
| 51 | - * @param string $data |
|
| 52 | - * @param int $offset Offset to next byte after identifier |
|
| 53 | - */ |
|
| 54 | - public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | - { |
|
| 56 | - $this->_identifier = $identifier; |
|
| 57 | - $this->_data = $data; |
|
| 58 | - $this->_offset = $offset; |
|
| 59 | - $this->_typeTag = intval($identifier->tag()); |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor. |
|
| 49 | + * |
|
| 50 | + * @param Identifier $identifier |
|
| 51 | + * @param string $data |
|
| 52 | + * @param int $offset Offset to next byte after identifier |
|
| 53 | + */ |
|
| 54 | + public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | + { |
|
| 56 | + $this->_identifier = $identifier; |
|
| 57 | + $this->_data = $data; |
|
| 58 | + $this->_offset = $offset; |
|
| 59 | + $this->_typeTag = intval($identifier->tag()); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * |
|
| 64 | - * @see \ASN1\Element::typeClass() |
|
| 65 | - * @return int |
|
| 66 | - */ |
|
| 67 | - public function typeClass(): int |
|
| 68 | - { |
|
| 69 | - return $this->_identifier->typeClass(); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * |
|
| 64 | + * @see \ASN1\Element::typeClass() |
|
| 65 | + * @return int |
|
| 66 | + */ |
|
| 67 | + public function typeClass(): int |
|
| 68 | + { |
|
| 69 | + return $this->_identifier->typeClass(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \ASN1\Element::isConstructed() |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isConstructed(): bool |
|
| 78 | - { |
|
| 79 | - return $this->_identifier->isConstructed(); |
|
| 80 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \ASN1\Element::isConstructed() |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isConstructed(): bool |
|
| 78 | + { |
|
| 79 | + return $this->_identifier->isConstructed(); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * |
|
| 84 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - protected function _encodedContentDER(): string |
|
| 88 | - { |
|
| 89 | - $idx = $this->_offset; |
|
| 90 | - $length = Length::expectFromDER($this->_data, $idx)->intVal(); |
|
| 91 | - return substr($this->_data, $idx, $length); |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * |
|
| 84 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + protected function _encodedContentDER(): string |
|
| 88 | + { |
|
| 89 | + $idx = $this->_offset; |
|
| 90 | + $length = Length::expectFromDER($this->_data, $idx)->intVal(); |
|
| 91 | + return substr($this->_data, $idx, $length); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * |
|
| 96 | - * {@inheritdoc} |
|
| 97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | - * @return UnspecifiedType |
|
| 99 | - */ |
|
| 100 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | - { |
|
| 102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | - $cls = self::_determineImplClass($identifier); |
|
| 104 | - $idx = $this->_offset; |
|
| 105 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | - return new UnspecifiedType($element); |
|
| 107 | - } |
|
| 94 | + /** |
|
| 95 | + * |
|
| 96 | + * {@inheritdoc} |
|
| 97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | + * @return UnspecifiedType |
|
| 99 | + */ |
|
| 100 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | + { |
|
| 102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | + $cls = self::_determineImplClass($identifier); |
|
| 104 | + $idx = $this->_offset; |
|
| 105 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | + return new UnspecifiedType($element); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * |
|
| 111 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | - * @return UnspecifiedType |
|
| 113 | - */ |
|
| 114 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | - { |
|
| 116 | - $idx = $this->_offset; |
|
| 117 | - Length::expectFromDER($this->_data, $idx); |
|
| 118 | - $element = Element::fromDER($this->_data, $idx); |
|
| 119 | - if (isset($expectedTag)) { |
|
| 120 | - $element->expectType($expectedTag); |
|
| 121 | - } |
|
| 122 | - return new UnspecifiedType($element); |
|
| 123 | - } |
|
| 109 | + /** |
|
| 110 | + * |
|
| 111 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | + * @return UnspecifiedType |
|
| 113 | + */ |
|
| 114 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | + { |
|
| 116 | + $idx = $this->_offset; |
|
| 117 | + Length::expectFromDER($this->_data, $idx); |
|
| 118 | + $element = Element::fromDER($this->_data, $idx); |
|
| 119 | + if (isset($expectedTag)) { |
|
| 120 | + $element->expectType($expectedTag); |
|
| 121 | + } |
|
| 122 | + return new UnspecifiedType($element); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -12,208 +12,208 @@ |
||
| 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 | - * @return int |
|
| 196 | - */ |
|
| 197 | - public function intVal(): int |
|
| 198 | - { |
|
| 199 | - if ($this->_indefinite) { |
|
| 200 | - throw new \LogicException("Length is indefinite."); |
|
| 201 | - } |
|
| 194 | + /** |
|
| 195 | + * @return int |
|
| 196 | + */ |
|
| 197 | + public function intVal(): int |
|
| 198 | + { |
|
| 199 | + if ($this->_indefinite) { |
|
| 200 | + throw new \LogicException("Length is indefinite."); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - if (gmp_cmp(gmp_init($this->_length, 10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
| 204 | - throw new \LogicException("Integer length too large"); |
|
| 205 | - } |
|
| 203 | + if (gmp_cmp(gmp_init($this->_length, 10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
| 204 | + throw new \LogicException("Integer length too large"); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - return (int) $this->_length; |
|
| 208 | - } |
|
| 207 | + return (int) $this->_length; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Whether length is indefinite. |
|
| 212 | - * |
|
| 213 | - * @return boolean |
|
| 214 | - */ |
|
| 215 | - public function isIndefinite(): bool |
|
| 216 | - { |
|
| 217 | - return $this->_indefinite; |
|
| 218 | - } |
|
| 210 | + /** |
|
| 211 | + * Whether length is indefinite. |
|
| 212 | + * |
|
| 213 | + * @return boolean |
|
| 214 | + */ |
|
| 215 | + public function isIndefinite(): bool |
|
| 216 | + { |
|
| 217 | + return $this->_indefinite; |
|
| 218 | + } |
|
| 219 | 219 | } |