1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 05.11.18 |
4
|
|
|
* Time: 21:06 |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace AlecRabbit\Money\Calculator; |
9
|
|
|
|
10
|
|
|
use AlecRabbit\Money\Contracts\CalculatorInterface; |
11
|
|
|
use BCMathExtended\BC; |
12
|
|
|
|
13
|
|
|
class BcMathCalculator implements CalculatorInterface |
14
|
|
|
{ |
15
|
|
|
private const _SCALE = EXTENDED_SCALE; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $scale; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param null|int $scale |
24
|
|
|
*/ |
25
|
156 |
|
public function __construct(?int $scale = null) |
26
|
|
|
{ |
27
|
156 |
|
$this->scale = $scale ?? static::_SCALE; |
28
|
156 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
1 |
|
public static function supported(): bool |
34
|
|
|
{ |
35
|
1 |
|
return \extension_loaded('bcmath'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
95 |
|
public function compare($a, $b): int |
42
|
|
|
{ |
43
|
95 |
|
return BC::comp($a, $b, $this->scale); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
70 |
|
public function add($amount, $addend): string |
50
|
|
|
{ |
51
|
70 |
|
return BC::add($amount, (string)$addend, $this->scale); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
65 |
|
public function subtract($amount, $subtrahend): string |
58
|
|
|
{ |
59
|
65 |
|
return BC::sub($amount, (string)$subtrahend, $this->scale); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
16 |
|
public function multiply($amount, $multiplier): string |
66
|
|
|
{ |
67
|
16 |
|
return BC::mul($amount, (string)$multiplier, $this->scale); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
29 |
|
public function divide($amount, $divisor): string |
74
|
|
|
{ |
75
|
29 |
|
return BC::div($amount, (string)$divisor, $this->scale); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
6 |
|
public function ceil($number): string |
82
|
|
|
{ |
83
|
6 |
|
return BC::ceil((string)$number); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
22 |
|
public function absolute($number): string |
90
|
|
|
{ |
91
|
22 |
|
return BC::abs((string)$number); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
* @param int $precision |
97
|
|
|
*/ |
98
|
87 |
|
public function round($number, $precision = 0): string |
99
|
|
|
{ |
100
|
87 |
|
return BC::round((string)$number, $precision ?? 0); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
40 |
|
public function share($amount, $ratio, $total, $precision): string |
107
|
|
|
{ |
108
|
|
|
return |
109
|
40 |
|
$this->round( |
110
|
40 |
|
BC::div( |
111
|
40 |
|
BC::mul($amount, (string)$ratio, $this->scale), |
112
|
40 |
|
(string)$total, |
113
|
40 |
|
$this->scale |
114
|
|
|
), |
115
|
40 |
|
$precision |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
25 |
|
public function floor($number): string |
123
|
|
|
{ |
124
|
25 |
|
return BC::floor($number); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
15 |
|
public function mod($amount, $divisor): string |
131
|
|
|
{ |
132
|
15 |
|
return BC::mod($amount, (string)$divisor, $this->scale); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|