1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSW\Money; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use WSW\Money\Support\Calculation; |
7
|
|
|
use WSW\Money\Support\Formatters; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Money |
11
|
|
|
* |
12
|
|
|
* @package WSW\Money |
13
|
|
|
* @author Ronaldo Matos Rodrigues <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class Money |
16
|
|
|
{ |
17
|
|
|
use Calculation; |
18
|
|
|
use Formatters; |
19
|
|
|
|
20
|
|
|
const SCALE = 6; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $amount; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Currency |
29
|
|
|
*/ |
30
|
|
|
private $currency; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $amount |
34
|
|
|
* @param Currency $currency |
35
|
|
|
*/ |
36
|
18 |
|
public function __construct($amount, Currency $currency) |
37
|
|
|
{ |
38
|
18 |
|
$this->amount = sprintf('%0.'.static::SCALE.'f', $amount); |
39
|
18 |
|
$this->currency = $currency; |
40
|
18 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Currency |
44
|
|
|
*/ |
45
|
11 |
|
public function getCurrency() |
46
|
|
|
{ |
47
|
11 |
|
return $this->currency; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
13 |
|
public function getAmount() |
54
|
|
|
{ |
55
|
13 |
|
return $this->amount; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $amount |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
6 |
|
public function newInstance($amount) |
64
|
|
|
{ |
65
|
6 |
|
return new self($amount, $this->getCurrency()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Money $other |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function equals(Money $other) |
74
|
|
|
{ |
75
|
1 |
|
return (bool) ($this->currency->equals($other->getCurrency()) && $this->getAmount() === $other->getAmount()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Money $other |
80
|
|
|
* @throws InvalidArgumentException |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
6 |
|
private function assertSameCurrency(Money $other) |
85
|
|
|
{ |
86
|
6 |
|
if (!$this->getCurrency()->equals($other->getCurrency())) { |
87
|
3 |
|
throw new InvalidArgumentException('Currencies must be identical'); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Money $other |
95
|
|
|
* @throws InvalidArgumentException |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
2 |
|
public function compare(Money $other) |
100
|
|
|
{ |
101
|
2 |
|
$this->assertSameCurrency($other); |
102
|
|
|
|
103
|
1 |
|
return $this->comparator($this->getAmount(), $other->getAmount(), static::SCALE); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Money $addend |
108
|
|
|
* @throws InvalidArgumentException |
109
|
|
|
* |
110
|
|
|
* @return Money |
111
|
|
|
*/ |
112
|
2 |
|
public function add(Money $addend) |
113
|
|
|
{ |
114
|
2 |
|
$this->assertSameCurrency($addend); |
115
|
|
|
|
116
|
1 |
|
return $this->newInstance($this->sum($this->getAmount(), $addend->getAmount(), static::SCALE)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Percentage $percentage |
121
|
|
|
* |
122
|
|
|
* @return Money |
123
|
|
|
*/ |
124
|
1 |
|
public function addPercent(Percentage $percentage) |
125
|
|
|
{ |
126
|
1 |
|
$newValue = ($this->getAmount() * $percentage->getPercent()); |
127
|
|
|
|
128
|
1 |
|
return $this->newInstance($this->sum($this->getAmount(), $newValue, static::SCALE)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Money $subtrahend |
133
|
|
|
* @throws InvalidArgumentException |
134
|
|
|
* |
135
|
|
|
* @return Money |
136
|
|
|
*/ |
137
|
2 |
|
public function sub(Money $subtrahend) |
138
|
|
|
{ |
139
|
2 |
|
$this->assertSameCurrency($subtrahend); |
140
|
|
|
|
141
|
1 |
|
return $this->newInstance($this->subtract($this->getAmount(), $subtrahend->getAmount(), static::SCALE)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Percentage $percentage |
146
|
|
|
* |
147
|
|
|
* @return Money |
148
|
|
|
*/ |
149
|
1 |
|
public function subPercent(Percentage $percentage) |
150
|
|
|
{ |
151
|
1 |
|
$newValue = ($this->getAmount() * $percentage->getPercent()); |
152
|
|
|
|
153
|
1 |
|
return $this->newInstance($this->subtract($this->getAmount(), $newValue, static::SCALE)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $decimals |
158
|
|
|
* @param string $decPoint |
159
|
|
|
* @param string $thousandsSep |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
2 |
|
public function getFormat($decimals = 2, $decPoint = ',', $thousandsSep = '.') |
164
|
|
|
{ |
165
|
2 |
|
return $this->format($this->getAmount(), $decimals, $decPoint, $thousandsSep); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param int $decimals |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public function getRound($decimals = 2) |
174
|
|
|
{ |
175
|
1 |
|
return $this->round($this->getAmount(), $decimals); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int $decimals |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
1 |
|
public function getTruncate($decimals = 2) |
184
|
|
|
{ |
185
|
1 |
|
return $this->truncate($this->getAmount(), $decimals); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
1 |
|
public function getMicros() |
192
|
|
|
{ |
193
|
1 |
|
return (int) preg_replace('/[^0-9]/', '', $this->getAmount()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
1 |
|
public function __toString() |
200
|
|
|
{ |
201
|
1 |
|
return $this->getFormat(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|