|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the unicorn project |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Philipp Braeutigam <[email protected]>, Steffen Brand <[email protected]> and contributors |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Xynnn\Unicorn\Converter; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use SteffenBrand\CurrCurr\Client\EcbClientInterface; |
|
15
|
|
|
use SteffenBrand\CurrCurr\CurrCurr; |
|
16
|
|
|
use SteffenBrand\CurrCurr\Exception\ConversionFailedException; |
|
17
|
|
|
use SteffenBrand\CurrCurr\Exception\CurrencyNotSupportedException; |
|
18
|
|
|
use SteffenBrand\CurrCurr\Model\Currency; |
|
19
|
|
|
use Xynnn\Unicorn\Model\Unit; |
|
20
|
|
|
use Xynnn\Unicorn\Model\ConvertibleValue; |
|
21
|
|
|
|
|
22
|
|
|
class CurrencyConverter extends AbstractMathematicalConverter |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Unit $eur Static instance for conversions |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $eur; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Unit $usd Static instance for conversions |
|
31
|
|
|
*/ |
|
32
|
|
|
public static $usd; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Unit $jpy Static instance for conversions |
|
36
|
|
|
*/ |
|
37
|
|
|
public static $jpy; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Unit $bgn Static instance for conversions |
|
41
|
|
|
*/ |
|
42
|
|
|
public static $bgn; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Unit $czk Static instance for conversions |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $czk; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Unit $dkk Static instance for conversions |
|
51
|
|
|
*/ |
|
52
|
|
|
public static $dkk; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Unit $gbp Static instance for conversions |
|
56
|
|
|
*/ |
|
57
|
|
|
public static $gbp; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Unit $huf Static instance for conversions |
|
61
|
|
|
*/ |
|
62
|
|
|
public static $huf; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var Unit $pln Static instance for conversions |
|
66
|
|
|
*/ |
|
67
|
|
|
public static $pln; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var Unit $ron Static instance for conversions |
|
71
|
|
|
*/ |
|
72
|
|
|
public static $ron; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var Unit $sek Static instance for conversions |
|
76
|
|
|
*/ |
|
77
|
|
|
public static $sek; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var Unit $chf Static instance for conversions |
|
81
|
|
|
*/ |
|
82
|
|
|
public static $chf; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var Unit $nok Static instance for conversions |
|
86
|
|
|
*/ |
|
87
|
|
|
public static $nok; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var Unit $hrk Static instance for conversions |
|
91
|
|
|
*/ |
|
92
|
|
|
public static $hrk; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var Unit $rub Static instance for conversions |
|
96
|
|
|
*/ |
|
97
|
|
|
public static $rub; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var Unit $try Static instance for conversions |
|
101
|
|
|
*/ |
|
102
|
|
|
public static $try; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var Unit $aud Static instance for conversions |
|
106
|
|
|
*/ |
|
107
|
|
|
public static $aud; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @var Unit $brl Static instance for conversions |
|
111
|
|
|
*/ |
|
112
|
|
|
public static $brl; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @var Unit $cad Static instance for conversions |
|
116
|
|
|
*/ |
|
117
|
|
|
public static $cad; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @var Unit $cny Static instance for conversions |
|
121
|
|
|
*/ |
|
122
|
|
|
public static $cny; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @var Unit $hkd Static instance for conversions |
|
126
|
|
|
*/ |
|
127
|
|
|
public static $hkd; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @var Unit $idr Static instance for conversions |
|
131
|
|
|
*/ |
|
132
|
|
|
public static $idr; |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @var Unit $ils Static instance for conversions |
|
136
|
|
|
*/ |
|
137
|
|
|
public static $ils; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @var Unit $inr Static instance for conversions |
|
141
|
|
|
*/ |
|
142
|
|
|
public static $inr; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @var Unit $krw Static instance for conversions |
|
146
|
|
|
*/ |
|
147
|
|
|
public static $krw; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @var Unit $mxn Static instance for conversions |
|
151
|
|
|
*/ |
|
152
|
|
|
public static $mxn; |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @var Unit $myr Static instance for conversions |
|
156
|
|
|
*/ |
|
157
|
|
|
public static $myr; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @var Unit $nzd Static instance for conversions |
|
161
|
|
|
*/ |
|
162
|
|
|
public static $nzd; |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @var Unit $php Static instance for conversions |
|
166
|
|
|
*/ |
|
167
|
|
|
public static $php; |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @var Unit $sgd Static instance for conversions |
|
171
|
|
|
*/ |
|
172
|
|
|
public static $sgd; |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @var Unit $thb Static instance for conversions |
|
176
|
|
|
*/ |
|
177
|
|
|
public static $thb; |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @var Unit $zar Static instance for conversions |
|
181
|
|
|
*/ |
|
182
|
|
|
public static $zar; |
|
183
|
|
|
|
|
184
|
|
|
private $exchangeRatesClient; |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* CurrencyConverter constructor. |
|
188
|
|
|
* @param EcbClientInterface $ecbClient leave blank for default ECB client |
|
189
|
|
|
*/ |
|
190
|
10 |
|
public function __construct(EcbClientInterface $ecbClient = null) |
|
191
|
|
|
{ |
|
192
|
10 |
|
$this->exchangeRatesClient = new CurrCurr($ecbClient); |
|
193
|
|
|
|
|
194
|
10 |
|
$this->units[] = self::$eur = new Unit('EUR', '€', 1); |
|
195
|
10 |
|
$this->units[] = self::$usd = new Unit(Currency::USD, '$'); |
|
196
|
10 |
|
$this->units[] = self::$jpy = new Unit(Currency::JPY, '¥'); |
|
197
|
10 |
|
$this->units[] = self::$bgn = new Unit(Currency::BGN, 'лв'); |
|
198
|
10 |
|
$this->units[] = self::$czk = new Unit(Currency::CZK, 'Kč'); |
|
199
|
10 |
|
$this->units[] = self::$dkk = new Unit(Currency::DKK, 'kr'); |
|
200
|
10 |
|
$this->units[] = self::$gbp = new Unit(Currency::GBP, '£'); |
|
201
|
10 |
|
$this->units[] = self::$huf = new Unit(Currency::HUF, 'Ft'); |
|
202
|
10 |
|
$this->units[] = self::$pln = new Unit(Currency::PLN, 'zł'); |
|
203
|
10 |
|
$this->units[] = self::$ron = new Unit(Currency::RON, 'lei'); |
|
204
|
10 |
|
$this->units[] = self::$sek = new Unit(Currency::SEK, 'kr'); |
|
205
|
10 |
|
$this->units[] = self::$chf = new Unit(Currency::CHF, 'CHF'); |
|
206
|
10 |
|
$this->units[] = self::$nok = new Unit(Currency::NOK, 'kr'); |
|
207
|
10 |
|
$this->units[] = self::$hrk = new Unit(Currency::HRK, 'kn'); |
|
208
|
10 |
|
$this->units[] = self::$rub = new Unit(Currency::RUB, 'руб'); |
|
209
|
10 |
|
$this->units[] = self::$try = new Unit(Currency::TRY, '₺'); |
|
210
|
10 |
|
$this->units[] = self::$aud = new Unit(Currency::AUD, '$'); |
|
211
|
10 |
|
$this->units[] = self::$brl = new Unit(Currency::BRL, 'R$'); |
|
212
|
10 |
|
$this->units[] = self::$cad = new Unit(Currency::CAD, '$'); |
|
213
|
10 |
|
$this->units[] = self::$cny = new Unit(Currency::CNY, '¥'); |
|
214
|
10 |
|
$this->units[] = self::$hkd = new Unit(Currency::HKD, '$'); |
|
215
|
10 |
|
$this->units[] = self::$idr = new Unit(Currency::IDR, 'Rp'); |
|
216
|
10 |
|
$this->units[] = self::$ils = new Unit(Currency::ILS, '₪'); |
|
217
|
10 |
|
$this->units[] = self::$inr = new Unit(Currency::INR, '₹'); |
|
218
|
10 |
|
$this->units[] = self::$krw = new Unit(Currency::KRW, '₩'); |
|
219
|
10 |
|
$this->units[] = self::$mxn = new Unit(Currency::MXN, '$'); |
|
220
|
10 |
|
$this->units[] = self::$myr = new Unit(Currency::MYR, 'RM'); |
|
221
|
10 |
|
$this->units[] = self::$nzd = new Unit(Currency::NZD, '$'); |
|
222
|
10 |
|
$this->units[] = self::$php = new Unit(Currency::PHP, '₱'); |
|
223
|
10 |
|
$this->units[] = self::$sgd = new Unit(Currency::SGD, '$'); |
|
224
|
10 |
|
$this->units[] = self::$thb = new Unit(Currency::THB, '฿'); |
|
225
|
10 |
|
$this->units[] = self::$zar = new Unit(Currency::ZAR, 'R'); |
|
226
|
10 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return string Name of the converter |
|
230
|
|
|
*/ |
|
231
|
4 |
|
public function getName(): string |
|
232
|
|
|
{ |
|
233
|
4 |
|
return 'unicorn.converter.currency'; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param ConvertibleValue $from |
|
238
|
|
|
* @param Unit $to |
|
239
|
|
|
* @return ConvertibleValue |
|
240
|
|
|
*/ |
|
241
|
6 |
|
public function convert(ConvertibleValue $from, Unit $to): ConvertibleValue |
|
242
|
|
|
{ |
|
243
|
6 |
|
if (false === $from->getUnit()->isFactorSet() || false === $to->isFactorSet()) { |
|
244
|
1 |
|
$this->loadExchangeRates(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
6 |
|
return parent::convert($from, $to); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param ConvertibleValue $cv The Convertible to be normalized |
|
252
|
|
|
*/ |
|
253
|
5 |
|
protected function normalize(ConvertibleValue $cv) |
|
254
|
|
|
{ |
|
255
|
5 |
|
if (false === $cv->getUnit()->isFactorSet()) { |
|
256
|
1 |
|
$this->loadExchangeRates(); |
|
257
|
1 |
|
if (false === $cv->getUnit()->isFactorSet()) { |
|
258
|
|
|
throw new ConversionFailedException(new CurrencyNotSupportedException()); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
5 |
|
parent::normalize($cv); |
|
262
|
5 |
|
} |
|
263
|
|
|
|
|
264
|
2 |
|
public function loadExchangeRates() |
|
265
|
|
|
{ |
|
266
|
|
|
try { |
|
267
|
2 |
|
$exchangeRates = $this->exchangeRatesClient->getExchangeRates(); |
|
268
|
|
|
|
|
269
|
2 |
|
foreach ($this->units as $unit) { |
|
270
|
2 |
|
if (false === $unit->isFactorSet()) { |
|
271
|
2 |
|
$unit->setFactor(strval($exchangeRates[$unit->getName()]->getRate())); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} catch (Exception $e) { |
|
275
|
|
|
throw new ConversionFailedException($e); |
|
276
|
|
|
} |
|
277
|
2 |
|
} |
|
278
|
|
|
|
|
279
|
5 |
|
public function getBaseUnit(): Unit |
|
280
|
|
|
{ |
|
281
|
5 |
|
return self::$eur; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param array $units |
|
286
|
|
|
*/ |
|
287
|
|
|
public function setUnits(array $units) |
|
288
|
|
|
{ |
|
289
|
|
|
$this->units = $units; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param Unit $unit |
|
294
|
|
|
*/ |
|
295
|
1 |
|
public function addUnit(Unit $unit) |
|
296
|
|
|
{ |
|
297
|
1 |
|
$this->units[] = $unit; |
|
298
|
1 |
|
} |
|
299
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
|