|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Linio\Type; |
|
6
|
|
|
|
|
7
|
|
|
class PreciseMoney extends Money |
|
8
|
|
|
{ |
|
9
|
|
|
const CALCULATION_SCALE = 10; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(float $amount = 0) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->amount = bcmul((string) $amount, '100', self::CALCULATION_SCALE); |
|
|
|
|
|
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function add(Money $operand): Money |
|
17
|
|
|
{ |
|
18
|
|
|
$result = bcadd($this->amount, (string) $operand->getAmount(), self::CALCULATION_SCALE); |
|
19
|
|
|
|
|
20
|
|
|
return Money::fromCents((float) $result); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function subtract(Money $operand): Money |
|
24
|
|
|
{ |
|
25
|
|
|
$result = bcsub($this->amount, (string) $operand->getAmount(), self::CALCULATION_SCALE); |
|
26
|
|
|
|
|
27
|
|
|
return Money::fromCents((float) $result); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function multiply(float $multiplier): Money |
|
31
|
|
|
{ |
|
32
|
|
|
$result = bcmul($this->amount, (string) $multiplier, self::CALCULATION_SCALE); |
|
33
|
|
|
|
|
34
|
|
|
return Money::fromCents((float) $result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function divide(float $divisor): Money |
|
38
|
|
|
{ |
|
39
|
|
|
$result = bcdiv($this->amount, (string) $divisor, self::CALCULATION_SCALE); |
|
40
|
|
|
|
|
41
|
|
|
return Money::fromCents((float) $result); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function getPercentage(float $percentage): Money |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$percentage = bcdiv((string) $percentage, '100', self::CALCULATION_SCALE); |
|
47
|
|
|
$result = bcmul($this->amount, (string) $percentage, self::CALCULATION_SCALE); |
|
48
|
|
|
|
|
49
|
|
|
return Money::fromCents((float) $result); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function applyPercentage(float $percentage): Money |
|
53
|
|
|
{ |
|
54
|
|
|
$percentage = $this->getPercentage($percentage); |
|
55
|
|
|
|
|
56
|
|
|
return $this->add($percentage); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function getInterest(float $rate, int $duration): Money |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$interest = bcdiv((string) $rate, '100', self::CALCULATION_SCALE); |
|
62
|
|
|
$result = bcmul( |
|
63
|
|
|
bcmul( |
|
64
|
|
|
$this->amount, |
|
65
|
|
|
(string) $duration, |
|
66
|
|
|
self::CALCULATION_SCALE |
|
67
|
|
|
), |
|
68
|
|
|
(string) $interest, |
|
69
|
|
|
self::CALCULATION_SCALE |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return Money::fromCents((float) $result); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function applyInterest(float $rate, int $duration): Money |
|
76
|
|
|
{ |
|
77
|
|
|
$interest = $this->getInterest($rate, $duration); |
|
78
|
|
|
|
|
79
|
|
|
return $this->add($interest); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function equals($other): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (!($other instanceof Money)) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return bccomp((string) $this->amount, (string) $other->getAmount()) === 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function greaterThan(Money $other): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return bccomp((string) $this->amount, (string) $other->getAmount()) === 1; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function lessThan(Money $other): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return bccomp((string) $this->amount, (string) $other->getAmount()) === -1; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getMoneyAmount(): float |
|
102
|
|
|
{ |
|
103
|
|
|
$money = bcdiv($this->amount, '100', self::CALCULATION_SCALE); |
|
104
|
|
|
|
|
105
|
|
|
return round($money, $this->scale); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getAmount(): int |
|
109
|
|
|
{ |
|
110
|
|
|
return (int) round($this->amount); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function setAmount(float $amount) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->amount = (string) $amount; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.