|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Awin\ReportTask\Bundle\ReportBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The currency service which pull the (remote) currency webservice and (local) converter class. |
|
9
|
|
|
* It also converts the currency symbol from/to currency ISO code for more standard process |
|
10
|
|
|
* |
|
11
|
|
|
* @date 24/06/2017 |
|
12
|
|
|
* @time 12:47 |
|
13
|
|
|
* @author Peng Yue <[email protected]> |
|
14
|
|
|
* @copyright 2004-2017 Peng Yue |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
class CurrencyConverter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Predefined currencies list supported |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $currencies = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The currency converted from |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $originalCurrency; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The currency converted to |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $targetCurrency; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CurrencyWebservice |
|
42
|
|
|
*/ |
|
43
|
|
|
private $currencyWebservice; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* CurrencyConverter constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param CurrencyWebservice $currencyWebservice |
|
49
|
|
|
* @param array $availableCurrencies |
|
50
|
|
|
*/ |
|
51
|
27 |
|
public function __construct(CurrencyWebservice $currencyWebservice, array $availableCurrencies) |
|
52
|
|
|
{ |
|
53
|
27 |
|
$this->currencyWebservice = $currencyWebservice; |
|
54
|
27 |
|
$this->currencies = $availableCurrencies; |
|
55
|
27 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Convert the certain amount in one currency into the amount in another currency |
|
59
|
|
|
* |
|
60
|
|
|
* @param float $amount |
|
61
|
|
|
* |
|
62
|
|
|
* @return float |
|
63
|
|
|
*/ |
|
64
|
10 |
|
public function convert($amount) |
|
65
|
|
|
{ |
|
66
|
10 |
|
return money_format('%+n', |
|
|
|
|
|
|
67
|
|
|
$amount * |
|
68
|
10 |
|
$this->currencyWebservice->getExchangeRate( |
|
69
|
10 |
|
$this->getOriginalCurrency(), |
|
70
|
10 |
|
$this->getTargetCurrency() |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the original currency converted from |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $currency |
|
79
|
|
|
* |
|
80
|
|
|
* @throw \Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
13 |
|
public function setOriginalCurrency($currency) |
|
84
|
|
|
{ |
|
85
|
13 |
|
if (!array_key_exists($currency, $this->currencies)) { |
|
86
|
1 |
|
throw new CurrencyNotFoundException($currency); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
12 |
|
$this->originalCurrency = $currency; |
|
90
|
|
|
|
|
91
|
12 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the original currency converted from |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
11 |
|
public function getOriginalCurrency() |
|
100
|
|
|
{ |
|
101
|
11 |
|
return $this->originalCurrency; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the target currency converted to |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $currency |
|
108
|
|
|
* |
|
109
|
|
|
* @throw \Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
13 |
|
public function setTargetCurrency($currency) |
|
113
|
|
|
{ |
|
114
|
13 |
|
if (!array_key_exists($currency, $this->currencies)) { |
|
115
|
1 |
|
throw new CurrencyNotFoundException($currency); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
12 |
|
$this->targetCurrency = $currency; |
|
119
|
|
|
|
|
120
|
12 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set the target currency converted to |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
11 |
|
public function getTargetCurrency() |
|
129
|
|
|
{ |
|
130
|
11 |
|
return $this->targetCurrency; |
|
131
|
|
|
} |
|
132
|
|
|
} |