1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Converter; |
4
|
|
|
|
5
|
|
|
use BenTools\Currency\Model\CurrencyInterface; |
6
|
|
|
use BenTools\Currency\Model\ExchangeRateInterface; |
7
|
|
|
use BenTools\Currency\Model\ExchangeRateNotFoundException; |
8
|
|
|
|
9
|
|
|
final class CurrencyConverter implements CurrencyConverterInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ExchangeRateInterface[] |
13
|
|
|
*/ |
14
|
|
|
private $exchangeRates = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* CurrencyConverter constructor. |
18
|
|
|
* @param ExchangeRateInterface[] ...$exchangeRates |
19
|
|
|
*/ |
20
|
|
|
public function __construct(ExchangeRateInterface ...$exchangeRates) |
21
|
|
|
{ |
22
|
|
|
foreach ($exchangeRates as $exchangeRate) { |
23
|
|
|
$this->registerExchangeRate($exchangeRate); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ExchangeRateInterface $exchangeRate |
29
|
|
|
*/ |
30
|
|
|
private function registerExchangeRate(ExchangeRateInterface $exchangeRate): void |
31
|
|
|
{ |
32
|
|
|
$source = $exchangeRate->getSourceCurrency()->getCode(); |
33
|
|
|
$target = $exchangeRate->getTargetCurrency()->getCode(); |
34
|
|
|
$this->exchangeRates[$source][$target] = $exchangeRate->getRatio(); |
35
|
|
|
|
36
|
|
|
if (!$this->hasExchangeRate($exchangeRate->getTargetCurrency()->getCode(), $exchangeRate->getSourceCurrency()->getCode())) { |
37
|
|
|
$source = $exchangeRate->getTargetCurrency()->getCode(); |
38
|
|
|
$target = $exchangeRate->getSourceCurrency()->getCode(); |
39
|
|
|
$this->exchangeRates[$source][$target] = 1 / $exchangeRate->getRatio(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $sourceCurrencyCode |
45
|
|
|
* @param string $targetCurrencyCode |
46
|
|
|
* @return float|null |
47
|
|
|
*/ |
48
|
|
|
private function getExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode): ?float |
49
|
|
|
{ |
50
|
|
|
return $this->exchangeRates[$sourceCurrencyCode][$targetCurrencyCode] ?? null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $sourceCurrencyCode |
55
|
|
|
* @param string $targetCurrencyCode |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
private function hasExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode): bool |
59
|
|
|
{ |
60
|
|
|
return isset($this->exchangeRates[$sourceCurrencyCode][$targetCurrencyCode]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
|
|
public function convert(float $amount, CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency): float |
67
|
|
|
{ |
68
|
|
|
$ratio = $this->getExchangeRate($sourceCurrency->getCode(), $targetCurrency->getCode()); |
69
|
|
|
|
70
|
|
|
if (null === $ratio) { |
71
|
|
|
throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency, sprintf("No exchange rate registered for converting %s to %s", $sourceCurrency->getCode(), $targetCurrency->getCode())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $amount * $ratio; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: