1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SteffenBrand\CurrCurr; |
6
|
|
|
|
7
|
|
|
use SteffenBrand\CurrCurr\Client\EcbClient; |
8
|
|
|
use SteffenBrand\CurrCurr\Client\EcbClientInterface; |
9
|
|
|
use SteffenBrand\CurrCurr\Exception\CurrencyNotSupportedException; |
10
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException; |
11
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException; |
12
|
|
|
use SteffenBrand\CurrCurr\Model\Currency; |
13
|
|
|
use SteffenBrand\CurrCurr\Model\ExchangeRate; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CurrCurr |
17
|
|
|
* @package SteffenBrand\CurrCurr |
18
|
|
|
*/ |
19
|
|
|
class CurrCurr |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EcbClientInterface |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* CurrCurr constructor. |
28
|
|
|
* |
29
|
|
|
* @param EcbClientInterface $client The ECB Client to use, leave blank for default ECB Client |
30
|
|
|
*/ |
31
|
|
|
public function __construct(EcbClientInterface $client = null) |
32
|
|
|
{ |
33
|
|
|
if (null === $client) { |
34
|
|
|
$client = new EcbClient(); |
35
|
|
|
} |
36
|
|
|
$this->client = $client; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Delivers the current exchange rates based on EUR from the ECB. |
41
|
|
|
* |
42
|
|
|
* @throws ExchangeRatesMappingFailedException |
43
|
|
|
* @throws ExchangeRatesRequestFailedException |
44
|
|
|
* @return ExchangeRate[] |
45
|
|
|
*/ |
46
|
|
|
public function getExchangeRates(): array |
47
|
|
|
{ |
48
|
|
|
return $this->client->getExchangeRates(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Delivers the current exchange rates for a specific currency based on EUR from the ECB. |
53
|
|
|
* |
54
|
|
|
* @param string $currencyAbbr The currency to be requested. Use constants from Currency. |
55
|
|
|
* @return ExchangeRate |
56
|
|
|
* @throws ExchangeRatesMappingFailedException |
57
|
|
|
* @throws ExchangeRatesRequestFailedException |
58
|
|
|
* @throws CurrencyNotSupportedException |
59
|
|
|
*/ |
60
|
|
|
public function getExchangeRateByCurrency(string $currencyAbbr): ExchangeRate |
61
|
|
|
{ |
62
|
|
|
$exchangeRates = $this->getExchangeRates(); |
63
|
|
|
|
64
|
|
|
if (false === \array_key_exists($currencyAbbr, $exchangeRates) || |
65
|
|
|
false === \in_array($currencyAbbr, Currency::ALLOWED_CURRENCIES, true)) |
66
|
|
|
{ |
67
|
|
|
throw new CurrencyNotSupportedException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $exchangeRates[$currencyAbbr]; |
71
|
|
|
} |
72
|
|
|
} |