1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Linio\Type; |
6
|
|
|
|
7
|
|
|
class Money |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Money amount in cents. |
11
|
|
|
* |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $amount = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Number of digits after the decimal place. |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $scale = 2; |
22
|
|
|
|
23
|
|
|
public function __construct(float $amount = 0) |
24
|
|
|
{ |
25
|
|
|
$this->amount = (int) round($amount * 100); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function fromCents(float $cents): Money |
29
|
|
|
{ |
30
|
|
|
$money = new static(); |
31
|
|
|
$money->setAmount($cents); |
32
|
|
|
|
33
|
|
|
return $money; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function add(Money $operand): Money |
37
|
|
|
{ |
38
|
|
|
$result = $this->amount + $operand->getAmount(); |
39
|
|
|
|
40
|
|
|
return static::fromCents($result); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function subtract(Money $operand): Money |
44
|
|
|
{ |
45
|
|
|
$result = $this->amount - $operand->getAmount(); |
46
|
|
|
|
47
|
|
|
return static::fromCents($result); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function multiply(float $multiplier): Money |
51
|
|
|
{ |
52
|
|
|
$result = $this->amount * $multiplier; |
53
|
|
|
|
54
|
|
|
return static::fromCents($result); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function divide(float $divisor): Money |
58
|
|
|
{ |
59
|
|
|
$result = $this->amount / $divisor; |
60
|
|
|
|
61
|
|
|
return static::fromCents($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getPercentage(float $percentage): Money |
65
|
|
|
{ |
66
|
|
|
$percentage = $percentage / 100; |
67
|
|
|
$result = $this->amount * $percentage; |
68
|
|
|
|
69
|
|
|
return static::fromCents($result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function applyPercentage(float $percentage): Money |
73
|
|
|
{ |
74
|
|
|
$percentage = $this->getPercentage($percentage); |
75
|
|
|
|
76
|
|
|
return $this->add($percentage); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getInterest(float $rate, int $duration): Money |
80
|
|
|
{ |
81
|
|
|
$interest = $rate / 100; |
82
|
|
|
$result = ($this->amount * $duration) * $interest; |
83
|
|
|
|
84
|
|
|
return static::fromCents($result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function applyInterest(float $rate, int $duration): Money |
88
|
|
|
{ |
89
|
|
|
$interest = $this->getInterest($rate, $duration); |
90
|
|
|
|
91
|
|
|
return $this->add($interest); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function equals($other): bool |
95
|
|
|
{ |
96
|
|
|
if (!($other instanceof static)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->amount === $other->getAmount(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function greaterThan(Money $other): bool |
104
|
|
|
{ |
105
|
|
|
return $this->amount >= $other->getAmount(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function lessThan(Money $other): bool |
109
|
|
|
{ |
110
|
|
|
return $this->amount <= $other->getAmount(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function isZero(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->amount == 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isPositive(): bool |
119
|
|
|
{ |
120
|
|
|
return $this->amount > 0; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function isNegative(): bool |
124
|
|
|
{ |
125
|
|
|
return $this->amount < 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getMoneyAmount(): float |
129
|
|
|
{ |
130
|
|
|
$money = $this->amount / 100; |
131
|
|
|
|
132
|
|
|
return round($money, $this->scale); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getAmount(): int |
136
|
|
|
{ |
137
|
|
|
return $this->amount; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setAmount(float $amount) |
141
|
|
|
{ |
142
|
|
|
$this->amount = (int) round($amount); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getScale(): int |
146
|
|
|
{ |
147
|
|
|
return $this->scale; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setScale(int $scale) |
151
|
|
|
{ |
152
|
|
|
$this->scale = $scale; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function __toString(): string |
156
|
|
|
{ |
157
|
|
|
return (string) $this->amount; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|