1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/ExchangeRate |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license https://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\ExchangeRate; |
9
|
|
|
|
10
|
|
|
use Brownie\ExchangeRate\Exception\UnknownCurrencyException; |
11
|
|
|
use Brownie\ExchangeRate\Model\Currency as ModelCurrency; |
12
|
|
|
use Brownie\ExchangeRate\Model\ExchangeRate as ModelExchangeRate; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Service for currency rates. |
16
|
|
|
*/ |
17
|
|
|
class ExchangeRate |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Service to get current currency list. |
22
|
|
|
* |
23
|
|
|
* @var CurrencyInterface |
24
|
|
|
*/ |
25
|
|
|
private $currencyService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Service to get current exchange rates. |
29
|
|
|
* |
30
|
|
|
* @var SourceInterface |
31
|
|
|
*/ |
32
|
|
|
private $sourceService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List of currencies. |
36
|
|
|
* |
37
|
|
|
* @var ModelCurrency[] |
38
|
|
|
*/ |
39
|
|
|
private $currencies; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* List of currency exchange rates to convert. |
43
|
|
|
* |
44
|
|
|
* @var ModelExchangeRate[] |
45
|
|
|
*/ |
46
|
|
|
private $exchangeRates; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets the input values. |
50
|
|
|
* |
51
|
|
|
* @param CurrencyInterface $currencyService Service to get current currency list. |
52
|
|
|
* @param SourceInterface $sourceService Service to get current exchange rates. |
53
|
|
|
*/ |
54
|
6 |
|
public function __construct( |
55
|
|
|
CurrencyInterface $currencyService, |
56
|
|
|
SourceInterface $sourceService |
57
|
|
|
) { |
58
|
|
|
$this |
59
|
6 |
|
->setCurrencyService($currencyService) |
60
|
6 |
|
->setSourceService($sourceService); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets service to get current currency list. |
65
|
|
|
* Returns the current object. |
66
|
|
|
* |
67
|
|
|
* @param CurrencyInterface $currencyService Sets service to get current currency list. |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
6 |
|
private function setCurrencyService(CurrencyInterface $currencyService) |
72
|
|
|
{ |
73
|
6 |
|
$this->currencyService = $currencyService; |
74
|
6 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets service to get current exchange rates. |
79
|
|
|
* Returns the current object. |
80
|
|
|
* |
81
|
|
|
* @param SourceInterface $sourceService Service to get current exchange rates. |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
6 |
|
private function setSourceService(SourceInterface $sourceService) |
86
|
|
|
{ |
87
|
6 |
|
$this->sourceService = $sourceService; |
88
|
6 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets service to get current currency list. |
93
|
|
|
* |
94
|
|
|
* @return CurrencyInterface |
95
|
|
|
*/ |
96
|
1 |
|
private function getCurrencyService() |
97
|
|
|
{ |
98
|
1 |
|
return $this->currencyService; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets service to get current exchange rates. |
103
|
|
|
* |
104
|
|
|
* @return SourceInterface |
105
|
|
|
*/ |
106
|
3 |
|
private function getSourceService() |
107
|
|
|
{ |
108
|
3 |
|
return $this->sourceService; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets an array of currencies. |
113
|
|
|
* Returns the current object. |
114
|
|
|
* |
115
|
|
|
* @param ModelCurrency[] $currencies Array of currencies. |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
2 |
|
public function setCurrencies(array $currencies) |
120
|
|
|
{ |
121
|
2 |
|
$this->currencies = $currencies; |
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets an array of currencies. |
127
|
|
|
* |
128
|
|
|
* @return ModelCurrency[] |
129
|
|
|
*/ |
130
|
2 |
|
public function getCurrencies() |
131
|
|
|
{ |
132
|
2 |
|
if (empty($this->currencies)) { |
133
|
1 |
|
$this->setCurrencies($this->getCurrencyService()->getCurrencies()); |
134
|
|
|
} |
135
|
2 |
|
return $this->currencies; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the current list of currency exchange. |
140
|
|
|
* Returns the current object. |
141
|
|
|
* |
142
|
|
|
* @param ModelExchangeRate[] $exchangeRates Current list of currency exchange. |
143
|
|
|
* |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
4 |
|
public function setExchangeRates(array $exchangeRates) |
147
|
|
|
{ |
148
|
4 |
|
$this->exchangeRates = $exchangeRates; |
149
|
4 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Gets the current list of currency exchange. |
154
|
|
|
* |
155
|
|
|
* @return ModelExchangeRate[] |
156
|
|
|
*/ |
157
|
4 |
|
public function getExchangeRates() |
158
|
|
|
{ |
159
|
4 |
|
if (empty($this->exchangeRates)) { |
160
|
3 |
|
$this->setExchangeRates($this->getSourceService()->getExchangeRates()); |
161
|
|
|
} |
162
|
4 |
|
return $this->exchangeRates; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Converts an amount from one currency to another. |
167
|
|
|
* |
168
|
|
|
* @param float $amount |
169
|
|
|
* @param string $from |
170
|
|
|
* @param string $to |
171
|
|
|
* @param int $precision |
172
|
|
|
* |
173
|
|
|
* @return float |
174
|
|
|
* |
175
|
|
|
* @throws UnknownCurrencyException |
176
|
|
|
*/ |
177
|
2 |
|
public function converter($amount, $from, $to, $precision = 2) |
178
|
|
|
{ |
179
|
2 |
|
$exchangeRates = $this->getExchangeRates(); |
180
|
|
|
|
181
|
2 |
|
if (!isset($exchangeRates[$from]) || !isset($exchangeRates[$to])) { |
182
|
1 |
|
throw new UnknownCurrencyException('Unknown currency covert: ' . $from . ' / ' . $to); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return round($amount / $exchangeRates[$from]->getRate() * $exchangeRates[$to]->getRate(), $precision); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|