1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace B2Binpay; |
5
|
|
|
|
6
|
|
|
use Litipk\BigNumbers\Decimal; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Carry about precision according to currency using BigNumbers |
10
|
|
|
* |
11
|
|
|
* @package B2Binpay |
12
|
|
|
*/ |
13
|
|
|
class Amount |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Currency |
17
|
|
|
*/ |
18
|
|
|
private $currency; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Decimal |
22
|
|
|
*/ |
23
|
|
|
private $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int|null |
27
|
|
|
*/ |
28
|
|
|
private $precision; |
29
|
|
|
|
30
|
|
|
/** |
31
|
40 |
|
* @param Currency $currency |
32
|
|
|
*/ |
33
|
40 |
|
public function __construct(Currency $currency) |
34
|
40 |
|
{ |
35
|
|
|
$this->currency = $currency; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $sum |
40
|
|
|
* @param int|null $pow |
41
|
39 |
|
* @param int|null $iso |
42
|
|
|
*/ |
43
|
39 |
|
public function set(string $sum, int $pow = null, int $iso = null) |
44
|
|
|
{ |
45
|
39 |
|
$value = Decimal::fromString($sum); |
46
|
|
|
|
47
|
39 |
|
$this->precision = $iso ? $this->currency->getPrecision($iso) : 0; |
48
|
32 |
|
|
49
|
32 |
|
if (empty($pow)) { |
50
|
|
|
$scale = max($this->calcScale($sum), $this->currency->getMaxPrecision()); |
51
|
18 |
|
$value = Decimal::fromString($sum, $scale); |
52
|
18 |
|
} else { |
53
|
|
|
$div = Decimal::fromInteger(10)->pow(Decimal::fromInteger($pow)); |
54
|
|
|
$value = $value->div($div); |
55
|
39 |
|
} |
56
|
39 |
|
|
57
|
|
|
$this->value = $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
26 |
|
* @return string |
62
|
|
|
*/ |
63
|
26 |
|
public function getValue(): string |
64
|
|
|
{ |
65
|
26 |
|
$return = $this->value; |
66
|
18 |
|
|
67
|
|
|
if (0 !== $this->precision) { |
68
|
|
|
$return = $this->value->ceil($this->precision); |
69
|
26 |
|
} |
70
|
|
|
|
71
|
|
|
return (string)$return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
7 |
|
* @return int |
76
|
|
|
*/ |
77
|
7 |
|
public function getPrecision(): int |
78
|
|
|
{ |
79
|
|
|
return $this->precision; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
21 |
|
* @return string |
84
|
|
|
*/ |
85
|
21 |
|
public function getPowered(): string |
86
|
21 |
|
{ |
87
|
|
|
$pow = Decimal::fromInteger($this->precision); |
|
|
|
|
88
|
21 |
|
$mul = Decimal::fromInteger(10)->pow($pow); |
89
|
|
|
|
90
|
|
|
return (string)$this->value->mul($mul)->ceil(0); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Amount $rate |
95
|
|
|
* @param int $precision |
96
|
14 |
|
* @return Amount |
97
|
|
|
*/ |
98
|
14 |
|
public function convert(Amount $rate, int $precision): Amount |
99
|
|
|
{ |
100
|
14 |
|
$this->precision = $precision; |
101
|
|
|
|
102
|
14 |
|
$mul = Decimal::fromString($rate->getValue()); |
103
|
|
|
|
104
|
14 |
|
$this->value = $this->value->mul($mul); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $percent |
111
|
10 |
|
* @return Amount |
112
|
|
|
*/ |
113
|
10 |
|
public function percentage(int $percent): Amount |
114
|
|
|
{ |
115
|
10 |
|
$mul = Decimal::fromInteger($percent)->div(Decimal::fromInteger(100)); |
116
|
|
|
|
117
|
10 |
|
$this->value = $this->value->add($this->value->mul($mul)); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $sum |
124
|
33 |
|
* @return int |
125
|
|
|
*/ |
126
|
33 |
|
public function calcScale(string $sum): int |
127
|
|
|
{ |
128
|
33 |
|
$parts = explode('.', $sum); |
129
|
|
|
|
130
|
|
|
return empty($parts[1]) ? 0 : strlen($parts[1]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|