1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Simple; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Mathable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MathService |
9
|
|
|
* @package Bavix\Wallet\Services |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
*/ |
12
|
|
|
class Math implements Mathable |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $scale; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string|int|float $first |
22
|
|
|
* @param string|int|float $second |
23
|
|
|
* @param null|int $scale |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function add($first, $second, ?int $scale = null): string |
27
|
|
|
{ |
28
|
|
|
return $this->round($first + $second, $this->scale($scale)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|int|float $first |
33
|
|
|
* @param string|int|float $second |
34
|
|
|
* @param null|int $scale |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function sub($first, $second, ?int $scale = null): string |
38
|
|
|
{ |
39
|
|
|
return $this->round($first - $second, $this->scale($scale)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|int|float $first |
44
|
|
|
* @param string|int|float $second |
45
|
|
|
* @param null|int $scale |
46
|
|
|
* @return float|int|string|null |
47
|
|
|
*/ |
48
|
|
|
public function div($first, $second, ?int $scale = null): string |
49
|
|
|
{ |
50
|
|
|
return $this->round($first / $second, $this->scale($scale)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|int|float $first |
55
|
|
|
* @param string|int|float $second |
56
|
|
|
* @param null|int $scale |
57
|
|
|
* @return float|int|string |
58
|
|
|
*/ |
59
|
|
|
public function mul($first, $second, ?int $scale = null): string |
60
|
|
|
{ |
61
|
|
|
return $this->round($first * $second, $this->scale($scale)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|int|float $first |
66
|
|
|
* @param string|int|float $second |
67
|
|
|
* @param null|int $scale |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function pow($first, $second, ?int $scale = null): string |
71
|
|
|
{ |
72
|
|
|
return $this->round($first ** $second, $this->scale($scale)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string|int|float $number |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function ceil($number): string |
80
|
|
|
{ |
81
|
|
|
return ceil($number); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string|int|float $number |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function floor($number): string |
89
|
|
|
{ |
90
|
|
|
return floor($number); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string|int|float $number |
95
|
|
|
* @param int $precision |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function round($number, int $precision = 0): string |
99
|
|
|
{ |
100
|
|
|
return round($number, $precision); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $first |
105
|
|
|
* @param $second |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function compare($first, $second): int |
109
|
|
|
{ |
110
|
|
|
return $first <=> $second; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $scale |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
protected function scale(?int $scale = null): int |
118
|
|
|
{ |
119
|
|
|
if ($scale !== null) { |
120
|
|
|
return $scale; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->scale === null) { |
124
|
|
|
$this->scale = (int)config('wallet.math.scale', 64); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->scale; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|