1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of OpinHelpers. |
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/OpinHelpers |
6
|
|
|
* This source file is licensed under the MIT license which you will |
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fab2s\OpinHelpers; |
11
|
|
|
|
12
|
|
|
use fab2s\Math\OpinHelpers\MathOpsAbstract; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Math |
16
|
|
|
*/ |
17
|
|
|
class Math extends MathOpsAbstract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $number |
21
|
|
|
* |
22
|
|
|
* @throws \InvalidArgumentException |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function gte($number) |
27
|
|
|
{ |
28
|
|
|
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) >= 0); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $number |
33
|
|
|
* |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function gt($number) |
39
|
|
|
{ |
40
|
|
|
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 1); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $number |
45
|
|
|
* |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function lte($number) |
51
|
|
|
{ |
52
|
|
|
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) <= 0); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $number |
57
|
|
|
* |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function lt($number) |
63
|
|
|
{ |
64
|
|
|
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === -1); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $number |
69
|
|
|
* |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function eq($number) |
75
|
|
|
{ |
76
|
|
|
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 0); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* returns the highest number among all arguments |
81
|
|
|
* |
82
|
|
|
* @param string[] $numbers |
83
|
|
|
* |
84
|
|
|
* @throws \InvalidArgumentException |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function max(...$numbers) |
89
|
|
|
{ |
90
|
|
|
foreach ($numbers as $number) { |
91
|
|
|
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === 1) { |
92
|
|
|
$this->number = $number; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* returns the smallest number among all arguments |
101
|
|
|
* |
102
|
|
|
* @param string[] $numbers |
103
|
|
|
* |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function min(...$numbers) |
109
|
|
|
{ |
110
|
|
|
foreach ($numbers as $number) { |
111
|
|
|
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === -1) { |
112
|
|
|
$this->number = $number; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* convert decimal value to any other base bellow or equals to 64 |
121
|
|
|
* |
122
|
|
|
* @param int $base |
123
|
|
|
* |
124
|
|
|
* @throws \InvalidArgumentException |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function toBase($base) |
129
|
|
|
{ |
130
|
|
|
if ($this->normalize()->hasDecimals()) { |
131
|
|
|
throw new \InvalidArgumentException('Argument number is not an integer in ' . __METHOD__); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// do not mutate, only support positive integers |
135
|
|
|
$number = ltrim((string) $this, '-'); |
136
|
|
|
if (static::$gmpSupport && $base <= 62) { |
137
|
|
|
return static::baseConvert($number, 10, $base); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$result = ''; |
141
|
|
|
$baseChar = static::getBaseChar($base); |
142
|
|
|
while (bccomp($number, 0) != 0) { // still data to process |
143
|
|
|
$rem = bcmod($number, $base); // calc the remainder |
144
|
|
|
$number = bcdiv(bcsub($number, $rem), $base); |
145
|
|
|
$result = $baseChar[$rem] . $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$result = $result ? $result : $baseChar[0]; |
149
|
|
|
|
150
|
|
|
return (string) $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $decimals |
155
|
|
|
* @param string $decPoint |
156
|
|
|
* @param string $thousandsSep |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function format($decimals = 0, $decPoint = '.', $thousandsSep = ' ') |
161
|
|
|
{ |
162
|
|
|
$decimals = max(0, (int) $decimals); |
163
|
|
|
$dec = ''; |
164
|
|
|
// do not mutate |
165
|
|
|
$number = (new static($this))->round($decimals)->normalize(); |
166
|
|
|
$sign = $number->isPositive() ? '' : '-'; |
167
|
|
|
if ($number->abs()->hasDecimals()) { |
168
|
|
|
list($number, $dec) = explode('.', (string) $number); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($decimals) { |
172
|
|
|
$dec = sprintf("%'0-" . $decimals . 's', $dec); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $sign . preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/", $thousandsSep, $number) . ($decimals ? $decPoint . $dec : ''); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// OMG a dynamic static anti pattern ^^ |
180
|
|
|
Math::gmpSupport(); |
181
|
|
|
|