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