|
1
|
|
|
<?php namespace Keios\MoneyRight; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the arbitrary precision arithmetic-based |
|
5
|
|
|
* money value object Keios\MoneyRight package. Keios\MoneyRight |
|
6
|
|
|
* is heavily inspired by Mathias Verroes' Money library and was |
|
7
|
|
|
* designed to be a drop-in replacement (only some use statement |
|
8
|
|
|
* tweaking required) for mentioned library. Public APIs are |
|
9
|
|
|
* identical, functionality is extended with additional methods |
|
10
|
|
|
* and parameters. |
|
11
|
|
|
* |
|
12
|
|
|
* |
|
13
|
|
|
* Copyright (c) 2015 Łukasz Biały |
|
14
|
|
|
* |
|
15
|
|
|
* For the full copyright and license information, please view the |
|
16
|
|
|
* LICENSE file that was distributed with this source code. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Serializable; |
|
20
|
|
|
use JsonSerializable; |
|
21
|
|
|
use Keios\MoneyRight\Exceptions\UnknownCurrencyException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Currency |
|
25
|
|
|
* Currency Value Object |
|
26
|
|
|
* |
|
27
|
|
|
* @package Keios\MoneyRight |
|
28
|
|
|
*/ |
|
29
|
|
|
class Currency implements Serializable, JsonSerializable |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
public static $currencies; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $isoCode; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $name; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $symbol; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var null|array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $alternateSymbols = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $subunit; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $symbolFirst; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $htmlEntity; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $decimalMark; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $thousandsSeparator; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $isoNumeric; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var null|string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $disambiguateSymbol = null; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Currency constructor |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $isoCode |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \Keios\MoneyRight\Exceptions\UnknownCurrencyException |
|
98
|
|
|
*/ |
|
99
|
237 |
|
public function __construct($isoCode) |
|
100
|
|
|
{ |
|
101
|
237 |
|
$currentCurrency = $this->getCurrencyFromIsoCode($isoCode); |
|
102
|
|
|
|
|
103
|
237 |
|
$this->fill($currentCurrency); |
|
104
|
237 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Load currency data from JSON configuration and cache it in static property |
|
108
|
|
|
*/ |
|
109
|
237 |
|
protected function prepareCurrencies() |
|
110
|
|
|
{ |
|
111
|
237 |
|
if (is_null(self::$currencies)) { |
|
112
|
3 |
|
self::$currencies = self::loadCurrencies(); |
|
113
|
3 |
|
} |
|
114
|
237 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Loads currency information from preloaded data by ISO index |
|
118
|
|
|
* |
|
119
|
|
|
* @param $isoCode |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
* @throws \Keios\MoneyRight\Exceptions\UnknownCurrencyException |
|
123
|
|
|
*/ |
|
124
|
237 |
|
protected function getCurrencyFromIsoCode($isoCode) |
|
125
|
|
|
{ |
|
126
|
237 |
|
$this->prepareCurrencies(); |
|
127
|
|
|
|
|
128
|
237 |
|
$isoCode = strtolower($isoCode); |
|
129
|
|
|
|
|
130
|
237 |
|
if (!array_key_exists($isoCode, self::$currencies)) { |
|
131
|
6 |
|
throw new UnknownCurrencyException('Currency with '.$isoCode.' iso code does not exist!'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
237 |
|
return self::$currencies[$isoCode]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Protected setter for all fields |
|
139
|
|
|
* |
|
140
|
|
|
* @param array $data |
|
141
|
|
|
*/ |
|
142
|
237 |
|
protected function fill(array $data) |
|
143
|
|
|
{ |
|
144
|
237 |
|
$this->isoCode = $data['iso_code']; |
|
145
|
237 |
|
$this->name = $data['name']; |
|
146
|
237 |
|
$this->symbol = $data['symbol']; |
|
147
|
237 |
|
$this->subunit = $data['subunit']; |
|
148
|
237 |
|
$this->symbolFirst = $data['symbol_first']; |
|
149
|
237 |
|
$this->htmlEntity = $data['html_entity']; |
|
150
|
237 |
|
$this->decimalMark = $data['decimal_mark']; |
|
151
|
237 |
|
$this->thousandsSeparator = $data['thousands_separator']; |
|
152
|
237 |
|
$this->isoNumeric = $data['iso_numeric']; |
|
153
|
|
|
|
|
154
|
237 |
|
if (array_key_exists('alternate_symbols', $data)) { |
|
155
|
237 |
|
$this->alternateSymbols = $data['alternate_symbols']; |
|
156
|
237 |
|
} |
|
157
|
|
|
|
|
158
|
237 |
|
if (array_key_exists('disambiguate_symbol', $data)) { |
|
159
|
3 |
|
$this->disambiguateSymbol = $data['disambiguate_symbol']; |
|
160
|
3 |
|
} |
|
161
|
237 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Prepares data for serialization |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
3 |
|
protected function aggregateData() |
|
169
|
|
|
{ |
|
170
|
|
|
$data = [ |
|
171
|
3 |
|
'iso_code' => $this->isoCode, |
|
172
|
3 |
|
'name' => $this->name, |
|
173
|
3 |
|
'symbol' => $this->symbol, |
|
174
|
3 |
|
'subunit' => $this->subunit, |
|
175
|
3 |
|
'symbol_first' => $this->symbolFirst, |
|
176
|
3 |
|
'html_entity' => $this->htmlEntity, |
|
177
|
3 |
|
'decimal_mark' => $this->decimalMark, |
|
178
|
3 |
|
'thousands_separator' => $this->thousandsSeparator, |
|
179
|
3 |
|
'iso_numeric' => $this->isoNumeric, |
|
180
|
3 |
|
]; |
|
181
|
|
|
|
|
182
|
3 |
|
if (!is_null($this->alternateSymbols)) { |
|
183
|
3 |
|
$data['alternate_symbols'] = $this->alternateSymbols; |
|
184
|
3 |
|
} |
|
185
|
|
|
|
|
186
|
3 |
|
if (!is_null($this->disambiguateSymbol)) { |
|
187
|
|
|
$data['disambiguate_symbol'] = $this->disambiguateSymbol; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
3 |
|
return $data; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/* |
|
194
|
|
|
* GETTERS |
|
195
|
|
|
*/ |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Getter for currency iso code |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
111 |
|
public function getIsoCode() |
|
203
|
|
|
{ |
|
204
|
111 |
|
return $this->isoCode; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getDecimalMark() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->decimalMark; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return null|array |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getAlternateSymbols() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->alternateSymbols; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return null|string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getDisambiguateSymbol() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->disambiguateSymbol; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getHtmlEntity() |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->htmlEntity; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getIsoNumeric() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->isoNumeric; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getName() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->name; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return string |
|
257
|
|
|
*/ |
|
258
|
|
|
public function getSubunit() |
|
259
|
|
|
{ |
|
260
|
|
|
return $this->subunit; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getSymbol() |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->symbol; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @return bool |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getSymbolFirst() |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->symbolFirst; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getThousandsSeparator() |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->thousandsSeparator; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/* |
|
288
|
|
|
* LOGIC |
|
289
|
|
|
*/ |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Checks if this Currency object is equal to another Currency object |
|
293
|
|
|
* |
|
294
|
|
|
* @param \Keios\MoneyRight\Currency $other |
|
295
|
|
|
* |
|
296
|
|
|
* @return bool |
|
297
|
|
|
*/ |
|
298
|
105 |
|
public function equals(Currency $other) |
|
299
|
|
|
{ |
|
300
|
105 |
|
return $this->isoCode === $other->getIsoCode(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Serialization getter for \Serializable |
|
305
|
|
|
* |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
6 |
|
public function serialize() |
|
309
|
|
|
{ |
|
310
|
6 |
|
return serialize($this->getIsoCode()); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Serialization constructor for \Serializable |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $serialized |
|
317
|
|
|
*/ |
|
318
|
6 |
|
public function unserialize($serialized) |
|
319
|
|
|
{ |
|
320
|
6 |
|
$currentCurrency = $this->getCurrencyFromIsoCode(unserialize($serialized)); |
|
321
|
|
|
|
|
322
|
6 |
|
$this->fill($currentCurrency); |
|
323
|
6 |
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* JSON Serialization getter for \JsonSerializable |
|
327
|
|
|
* |
|
328
|
|
|
* @return string |
|
329
|
|
|
*/ |
|
330
|
3 |
|
public function jsonserialize() |
|
331
|
|
|
{ |
|
332
|
3 |
|
$data = $this->aggregateData(); |
|
333
|
|
|
|
|
334
|
3 |
|
return $data; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Loads currency config from JSON files |
|
339
|
|
|
* @return array |
|
340
|
|
|
*/ |
|
341
|
3 |
|
public static function loadCurrencies() |
|
342
|
|
|
{ |
|
343
|
3 |
|
return require __DIR__.'/config/currencies.php'; |
|
344
|
|
|
} |
|
345
|
|
|
} |