|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Util; |
|
6
|
|
|
|
|
7
|
|
|
class BigInt |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Number as a base10 integer string. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $_num; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Number as an integer type. |
|
18
|
|
|
* |
|
19
|
|
|
* @internal Lazily initialized |
|
20
|
|
|
* |
|
21
|
|
|
* @var null|int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $_intNum; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param int|string $num |
|
29
|
|
|
*/ |
|
30
|
392 |
|
public function __construct($num) |
|
31
|
|
|
{ |
|
32
|
392 |
|
$this->_num = strval($num); |
|
33
|
392 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function __toString() |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->base10(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the number as a base10 integer string. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
34 |
|
public function base10(): string |
|
49
|
|
|
{ |
|
50
|
34 |
|
return $this->_num; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the number as an integer. |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \RuntimeException If number overflows integer size |
|
57
|
|
|
* |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
253 |
|
public function intVal(): int |
|
61
|
|
|
{ |
|
62
|
253 |
|
if (!isset($this->_intNum)) { |
|
63
|
253 |
|
$num = gmp_init($this->_num, 10); |
|
64
|
253 |
|
if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
65
|
4 |
|
throw new \RuntimeException('Integer overflow.'); |
|
66
|
|
|
} |
|
67
|
249 |
|
if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
68
|
1 |
|
throw new \RuntimeException('Integer underflow.'); |
|
69
|
|
|
} |
|
70
|
248 |
|
$this->_intNum = gmp_intval($num); |
|
71
|
|
|
} |
|
72
|
248 |
|
return $this->_intNum; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the number as a GMP object. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \GMP |
|
79
|
|
|
*/ |
|
80
|
145 |
|
public function gmpObj(): \GMP |
|
81
|
|
|
{ |
|
82
|
145 |
|
return gmp_init($this->_num, 10); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the maximum integer value. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \GMP |
|
89
|
|
|
*/ |
|
90
|
253 |
|
private function _intMaxGmp(): \GMP |
|
91
|
|
|
{ |
|
92
|
253 |
|
static $gmp; |
|
93
|
253 |
|
if (!isset($gmp)) { |
|
94
|
1 |
|
$gmp = gmp_init(PHP_INT_MAX, 10); |
|
95
|
|
|
} |
|
96
|
253 |
|
return $gmp; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the minimum integer value. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \GMP |
|
103
|
|
|
*/ |
|
104
|
249 |
|
private function _intMinGmp(): \GMP |
|
105
|
|
|
{ |
|
106
|
249 |
|
static $gmp; |
|
107
|
249 |
|
if (!isset($gmp)) { |
|
108
|
1 |
|
$gmp = gmp_init(PHP_INT_MIN, 10); |
|
109
|
|
|
} |
|
110
|
249 |
|
return $gmp; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|