|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: alec |
|
4
|
|
|
* Date: 05.11.18 |
|
5
|
|
|
* Time: 23:51 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace AlecRabbit\Money; |
|
9
|
|
|
|
|
10
|
|
|
use AlecRabbit\Money\CalculatorFactory as Factory; |
|
11
|
|
|
use AlecRabbit\Money\Contracts\MoneyInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Money Value Object. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Mathias Verraes |
|
17
|
|
|
*/ |
|
18
|
|
|
class Money implements MoneyInterface, \JsonSerializable |
|
19
|
|
|
{ |
|
20
|
|
|
use MoneyFactory, |
|
21
|
|
|
MoneyFunctions; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $amount; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Currency */ |
|
27
|
|
|
private $currency; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param null|int|float|string $amount |
|
31
|
|
|
* @param Currency $currency |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \InvalidArgumentException If amount is not integer |
|
34
|
|
|
*/ |
|
35
|
137 |
|
public function __construct($amount, Currency $currency) |
|
36
|
|
|
{ |
|
37
|
137 |
|
if (null === $amount) { |
|
38
|
3 |
|
$amount = 0; |
|
39
|
|
|
} |
|
40
|
137 |
|
if (!\is_numeric($amount)) { |
|
41
|
2 |
|
throw new \InvalidArgumentException('Amount must be int|float|string'); |
|
42
|
|
|
} |
|
43
|
135 |
|
$this->calculator = Factory::getCalculator(); |
|
44
|
|
|
|
|
45
|
135 |
|
$this->setAmount((string)$amount); |
|
46
|
135 |
|
$this->setCurrency($currency); |
|
47
|
135 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $amount |
|
51
|
|
|
*/ |
|
52
|
135 |
|
private function setAmount(string $amount): void |
|
53
|
|
|
{ |
|
54
|
135 |
|
$this->amount = trim_zeros($amount); |
|
55
|
135 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Currency $currency |
|
59
|
|
|
*/ |
|
60
|
135 |
|
private function setCurrency(Currency $currency): void |
|
61
|
|
|
{ |
|
62
|
135 |
|
$this->currency = $currency; |
|
63
|
135 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns an integer less than, equal to, or greater than zero |
|
67
|
|
|
* if the value of this object is considered to be respectively |
|
68
|
|
|
* less than, equal to, or greater than the other. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Money $other |
|
71
|
|
|
* |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function compare(Money $other): int |
|
75
|
|
|
{ |
|
76
|
7 |
|
$this->assertSameCurrency($other); |
|
77
|
|
|
|
|
78
|
7 |
|
return $this->calculator->compare($this->amount, $other->amount); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Asserts that a Money has the same currency as this. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Money $other |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \InvalidArgumentException If $other has a different currency |
|
87
|
|
|
*/ |
|
88
|
42 |
|
private function assertSameCurrency(Money $other): void |
|
89
|
|
|
{ |
|
90
|
42 |
|
if (!$this->isSameCurrency($other)) { |
|
91
|
3 |
|
throw new \InvalidArgumentException('Currencies must be identical.'); |
|
92
|
|
|
} |
|
93
|
42 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Checks whether a Money has the same Currency as this. |
|
97
|
|
|
* |
|
98
|
|
|
* @param Money $other |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
50 |
|
public function isSameCurrency(Money $other): bool |
|
103
|
|
|
{ |
|
104
|
50 |
|
return $this->currency->equals($other->currency); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the value represented by this object. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
32 |
|
public function getAmount(): string |
|
113
|
|
|
{ |
|
114
|
32 |
|
return $this->amount; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the currency of this object. |
|
119
|
|
|
* |
|
120
|
|
|
* @return Currency |
|
121
|
|
|
*/ |
|
122
|
55 |
|
public function getCurrency(): Currency |
|
123
|
|
|
{ |
|
124
|
55 |
|
return $this->currency; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function jsonSerialize(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return [ |
|
135
|
1 |
|
'amount' => $this->amount, |
|
136
|
1 |
|
'currency' => $this->currency, |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Asserts that the operand is integer or float. |
|
142
|
|
|
* |
|
143
|
|
|
* @param float|int|string|object $operand |
|
144
|
|
|
* |
|
145
|
|
|
* @throws \InvalidArgumentException If $operand is neither integer nor float |
|
146
|
|
|
*/ |
|
147
|
25 |
|
private function assertOperand($operand): void |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
25 |
|
if (!\is_numeric($operand)) { |
|
150
|
10 |
|
throw new \InvalidArgumentException(sprintf( |
|
151
|
10 |
|
'Operand should be a numeric value, "%s" given.', |
|
152
|
10 |
|
\is_object($operand) ? \get_class($operand) : \gettype($operand) |
|
153
|
|
|
)); |
|
154
|
|
|
} |
|
155
|
15 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a new Money instance based on the current one using the Currency. |
|
159
|
|
|
* |
|
160
|
|
|
* @param int|string|float|null $amount |
|
161
|
|
|
* |
|
162
|
|
|
* @return Money |
|
163
|
|
|
* |
|
164
|
|
|
* @throws \InvalidArgumentException |
|
165
|
|
|
*/ |
|
166
|
64 |
|
private function newInstance($amount): Money |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
64 |
|
return new Money($amount, $this->currency); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.