|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Type\Primitive; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Component\Identifier; |
|
8
|
|
|
use Sop\ASN1\Component\Length; |
|
9
|
|
|
use Sop\ASN1\Element; |
|
10
|
|
|
use Sop\ASN1\Feature\ElementBase; |
|
11
|
|
|
use Sop\ASN1\Type\PrimitiveType; |
|
12
|
|
|
use Sop\ASN1\Type\UniversalClass; |
|
13
|
|
|
use Sop\ASN1\Util\BigInt; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Implements *INTEGER* type. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Integer extends Element |
|
19
|
|
|
{ |
|
20
|
|
|
use UniversalClass; |
|
21
|
|
|
use PrimitiveType; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The number. |
|
25
|
|
|
* |
|
26
|
|
|
* @var BigInt |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_number; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \GMP|int|string $number Base 10 integer |
|
34
|
|
|
*/ |
|
35
|
69 |
|
public function __construct($number) |
|
36
|
|
|
{ |
|
37
|
69 |
|
$this->_typeTag = self::TYPE_INTEGER; |
|
38
|
69 |
|
if (!self::_validateNumber($number)) { |
|
39
|
2 |
|
$var = is_scalar($number) ? strval($number) : gettype($number); |
|
40
|
2 |
|
throw new \InvalidArgumentException("'{$var}' is not a valid number."); |
|
41
|
|
|
} |
|
42
|
67 |
|
$this->_number = new BigInt($number); |
|
43
|
67 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the number as a base 10. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string Integer as a string |
|
49
|
|
|
*/ |
|
50
|
24 |
|
public function number(): string |
|
51
|
|
|
{ |
|
52
|
24 |
|
return $this->_number->base10(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the number as an integer type. |
|
57
|
|
|
*/ |
|
58
|
6 |
|
public function intNumber(): int |
|
59
|
|
|
{ |
|
60
|
6 |
|
return $this->_number->intVal(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
31 |
|
protected function _encodedContentDER(): string |
|
67
|
|
|
{ |
|
68
|
31 |
|
return $this->_number->signedOctets(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
36 |
|
protected static function _decodeFromDER(Identifier $identifier, |
|
75
|
|
|
string $data, int &$offset): ElementBase |
|
76
|
|
|
{ |
|
77
|
36 |
|
$idx = $offset; |
|
78
|
36 |
|
$length = Length::expectFromDER($data, $idx)->intLength(); |
|
79
|
35 |
|
$bytes = substr($data, $idx, $length); |
|
80
|
35 |
|
$idx += $length; |
|
81
|
35 |
|
$num = BigInt::fromSignedOctets($bytes)->gmpObj(); |
|
82
|
35 |
|
$offset = $idx; |
|
83
|
|
|
// late static binding since enumerated extends integer type |
|
84
|
35 |
|
return new static($num); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Test that number is valid for this context. |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $num |
|
91
|
|
|
*/ |
|
92
|
69 |
|
private static function _validateNumber($num): bool |
|
93
|
|
|
{ |
|
94
|
69 |
|
if (is_int($num)) { |
|
95
|
28 |
|
return true; |
|
96
|
|
|
} |
|
97
|
41 |
|
if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
98
|
4 |
|
return true; |
|
99
|
|
|
} |
|
100
|
37 |
|
if ($num instanceof \GMP) { |
|
101
|
35 |
|
return true; |
|
102
|
|
|
} |
|
103
|
2 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|