/
MathBaseAbstract.php
300 lines (261 loc) · 7.26 KB
/
MathBaseAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* This file is part of OpinHelpers.
* (c) Fabrice de Stefanis / https://github.com/fab2s/OpinHelpers
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/
namespace fab2s\Math\OpinHelpers;
/**
* Abstract class MathBaseAbstract
*/
abstract class MathBaseAbstract
{
/**
* Default precision
*/
const PRECISION = 9;
/**
* base <= 64 charlist
*/
const BASECHAR_64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
/**
* base <= 62 char list
*/
const BASECHAR_62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
/**
* base <= 36 charlist
*/
const BASECHAR_36 = '0123456789abcdefghijklmnopqrstuvwxyz';
/**
* base char cache for all supported bases (bellow 64)
*
* @var string[]
*/
protected static $baseChars = [
36 => self::BASECHAR_36,
62 => self::BASECHAR_62,
64 => self::BASECHAR_64,
];
/**
* if set, will be used as default for all consecutive instances
*
* @var int
*/
protected static $globalPrecision;
/**
* Used in static context, aligned with $globalPrecision, default to self::PRECISION
*
* @var int
*/
protected static $staticPrecision = self::PRECISION;
/**
* @var bool
*/
protected static $gmpSupport;
/**
* @var string
*/
protected $number;
/**
* Instance precision, initialized with globalPrecision, default to self::PRECISION
*
* @var int
*/
protected $precision = self::PRECISION;
/**
* @return string
*/
public function getNumber()
{
return $this->number;
}
/**
* @return bool
*/
public function isPositive()
{
return $this->number[0] !== '-';
}
/**
* @return bool
*/
public function hasDecimals()
{
return strpos($this->number, '.') !== false;
}
/**
* @return $this
*/
public function normalize()
{
$this->number = static::normalizeNumber($this->number);
return $this;
}
/**
* @param int $precision
*
* @return $this
*/
public function setPrecision($precision)
{
// even INT_32 should be enough precision
$this->precision = max(0, (int) $precision);
return $this;
}
/**
* @param int $precision
*/
public static function setGlobalPrecision($precision)
{
// even INT_32 should be enough precision
static::$globalPrecision = max(0, (int) $precision);
static::$staticPrecision = static::$globalPrecision;
}
/**
* @param bool $disable
*
* @return bool
*/
public static function gmpSupport($disable = false)
{
if ($disable) {
return static::$gmpSupport = false;
}
return static::$gmpSupport = function_exists('gmp_init');
}
/**
* There is no way around it, if you want to trust bcmath
* you need to feed it with VALID numbers
* Things like '1.1.1' or '12E16'are all 0 in bcmath world
*
* @param mixed $number
*
* @return bool
*/
public static function isNumber($number)
{
return (bool) preg_match('`^([+-]{1})?([0-9]+(\.[0-9]+)?|\.[0-9]+)$`', $number);
}
/**
* removes preceding / trailing 0, + and ws
*
* @param string $number
* @param string|null $default
*
* @return string|null
*/
public static function normalizeNumber($number, $default = null)
{
if (!static::isNumber($number)) {
return $default;
}
$sign = $number[0] === '-' ? '-' : '';
$number = ltrim((string) $number, '0+-');
if (strpos($number, '.') !== false) {
// also clear trailing 0
list($number, $dec) = explode('.', $number);
$dec = rtrim($dec, '0.');
$number = ($number ? $number : '0') . ($dec ? '.' . $dec : '');
}
return $number ? $sign . $number : '0';
}
/**
* @param int $base
* @param int $max
*
* @throws \InvalidArgumentException
*
* @return string
*/
public static function getBaseChar($base, $max = 64)
{
$base = (int) $base;
if ($base < 2 || $base > $max || $base > 64) {
throw new \InvalidArgumentException('Argument base is not valid, base 2 to 64 are supported');
}
if (!isset(static::$baseChars[$base])) {
if ($base > 62) {
static::$baseChars[$base] = ($base == 64) ? static::BASECHAR_64 : substr(static::BASECHAR_64, 0, $base);
} elseif ($base > 36) {
static::$baseChars[$base] = ($base == 62) ? static::BASECHAR_62 : substr(static::BASECHAR_62, 0, $base);
} else {
static::$baseChars[$base] = ($base == 36) ? static::BASECHAR_36 : substr(static::BASECHAR_36, 0, $base);
}
}
return static::$baseChars[$base];
}
/**
* Convert a from a given base (up to 62) to base 10.
*
* WARNING This method requires ext-gmp
*
* @param string $number
* @param int $fromBase
* @param int $toBase
*
* @return string
*
* @internal param int $base
*/
public static function baseConvert($number, $fromBase = 10, $toBase = 62)
{
return gmp_strval(gmp_init($number, $fromBase), $toBase);
}
/**
* @param string $number
* @param string|int $base
* @param string $baseChar
*
* @return string
*/
protected static function bcDec2Base($number, $base, $baseChar)
{
$result = '';
$numberLen = strlen($number);
// Now loop through each digit in the number
for ($i = $numberLen - 1; $i >= 0; --$i) {
$char = $number[$i]; // extract the last char from the number
$ord = strpos($baseChar, $char); // get the decimal value
if ($ord === false || $ord > $base) {
throw new \InvalidArgumentException('Argument number is invalid');
}
// Now convert the value+position to decimal
$result = bcadd($result, bcmul($ord, bcpow($base, ($numberLen - $i - 1))));
}
return $result ? $result : '0';
}
/**
* @param string|static $number
*
* @throws \InvalidArgumentException
*
* @return string
*/
protected static function validateInputNumber($number)
{
if ($number instanceof static) {
return $number->getNumber();
}
$number = trim($number);
if (!static::isNumber($number)) {
throw new \InvalidArgumentException('Argument number is not valid');
}
return $number;
}
/**
* @param int|string $integer
*
* @throws \InvalidArgumentException
*
* @return string
*/
protected static function validatePositiveInteger($integer)
{
$integer = max(0, (int) $integer);
if (!$integer) {
throw new \InvalidArgumentException('Argument number is not valid');
}
return (string) $integer;
}
}