Skip to content

Commit 1d17196

Browse files
authored
Merge pull request florianv#151 from uguurozkan/implement-national-bank-of-georgia-service
Implement National Bank of Georgia Service
2 parents 4def80d + f906f56 commit 1d17196

File tree

6 files changed

+1233
-1
lines changed

6 files changed

+1233
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ Here is the complete list of the currently implemented services:
4747
| [Abstract](https://www.abstractapi.com) | * | * | Yes |
4848
| [coinlayer](https://coinlayer.com) | * Crypto (Limited standard currencies) | * Crypto (Limited standard currencies) | Yes |
4949
| [European Central Bank](https://www.ecb.europa.eu/home/html/index.en.html) | EUR | * | Yes |
50+
| [National Bank of Georgia](https://nbg.gov.ge) | * | GEL | Yes |
5051
| [National Bank of the Republic of Belarus](https://www.nbrb.by) | * | BYN (from 01-07-2016),<br>BYR (01-01-2000 - 30-06-2016),<br>BYB (25-05-1992 - 31-12-1999) | Yes |
5152
| [National Bank of Romania](http://www.bnr.ro) | RON, AED, AUD, BGN, BRL, CAD, CHF, CNY, CZK, DKK, EGP, EUR, GBP, HRK, HUF, INR, JPY, KRW, MDL, MXN, NOK, NZD, PLN, RSD, RUB, SEK, TRY, UAH, USD, XAU, XDR, ZAR | RON, AED, AUD, BGN, BRL, CAD, CHF, CNY, CZK, DKK, EGP, EUR, GBP, HRK, HUF, INR, JPY, KRW, MDL, MXN, NOK, NZD, PLN, RSD, RUB, SEK, TRY, UAH, USD, XAU, XDR, ZAR | Yes |
53+
| [National Bank of Ukranie](https://bank.gov.ua) | * | UAH | Yes |
5254
| [Central Bank of the Republic of Turkey](http://www.tcmb.gov.tr) | * | TRY | Yes |
5355
| [Central Bank of the Republic of Uzbekistan](https://cbu.uz) | * | UZS | Yes |
5456
| [Central Bank of the Czech Republic](https://www.cnb.cz) | * | CZK | Yes |
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Exchanger.
7+
*
8+
* (c) Florian Voutzinos <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Exchanger\Service;
15+
16+
use DateTime;
17+
use DateTimeInterface;
18+
use Exception;
19+
use Exchanger\Contract\ExchangeRate;
20+
use Exchanger\Contract\ExchangeRateQuery;
21+
use Exchanger\Contract\HistoricalExchangeRateQuery;
22+
use Exchanger\Exception\UnsupportedCurrencyPairException;
23+
use Exchanger\StringUtil;
24+
25+
/**
26+
* National Bank of Georgia Service.
27+
*
28+
* @author Uğur Özkan
29+
*/
30+
final class NationalBankOfGeorgia extends HttpService
31+
{
32+
use SupportsHistoricalQueries;
33+
34+
private const BASE_URL = 'https://nbg.gov.ge/gw/api/ct/monetarypolicy/currencies/en/json';
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function getName(): string
40+
{
41+
return 'national_bank_of_georgia';
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
48+
{
49+
return 'GEL' === $exchangeQuery->getCurrencyPair()->getQuoteCurrency();
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
* @throws UnsupportedCurrencyPairException
55+
*/
56+
protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRate
57+
{
58+
return $this->doCreateRate($exchangeQuery);
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
* @throws UnsupportedCurrencyPairException
64+
*/
65+
protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRate
66+
{
67+
return $this->doCreateRate($exchangeQuery, $exchangeQuery->getDate());
68+
}
69+
70+
/**
71+
* Creates the rate.
72+
*
73+
* @param ExchangeRateQuery $exchangeQuery
74+
* @param DateTimeInterface|null $requestedDate
75+
*
76+
* @return ExchangeRate
77+
* @throws UnsupportedCurrencyPairException
78+
* @throws Exception
79+
*/
80+
private function doCreateRate(ExchangeRateQuery $exchangeQuery, DateTimeInterface $requestedDate = null): ExchangeRate
81+
{
82+
$currencyPair = $exchangeQuery->getCurrencyPair();
83+
84+
$content = $this->request($this->buildUrl($requestedDate));
85+
$element = StringUtil::jsonToArray($content)[0];
86+
87+
$date = new DateTime((string) $element['date']);
88+
89+
$currencyInfo = array_values(array_filter($element['currencies'], function ($currency) use ($currencyPair) {
90+
return $currency['code'] === $currencyPair->getBaseCurrency();
91+
}));
92+
if (!empty($currencyInfo)) {
93+
$rate = (float) $currencyInfo[0]['rate'];
94+
$unit = (int) $currencyInfo[0]['quantity'];
95+
96+
return $this->createRate($currencyPair, ($rate / $unit), $date);
97+
}
98+
99+
throw new UnsupportedCurrencyPairException($currencyPair, $this);
100+
}
101+
102+
/**
103+
* Builds the url.
104+
*
105+
* @param DateTimeInterface|null $requestedDate
106+
*
107+
* @return string
108+
*/
109+
private function buildUrl(DateTimeInterface $requestedDate = null): string
110+
{
111+
$date = '';
112+
if (!is_null($requestedDate)) {
113+
$date = '?date=' . $requestedDate->format('Y-m-d');
114+
}
115+
116+
return self::BASE_URL . $date;
117+
}
118+
}

src/Service/Registry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public static function getServices(): array
3939
'fixer' => Fixer::class,
4040
'fixer_apilayer' => FixerApiLayer::class,
4141
'forge' => Forge::class,
42+
'national_bank_of_georgia' => NationalBankOfGeorgia::class,
4243
'national_bank_of_republic_belarus' => NationalBankOfRepublicBelarus::class,
4344
'national_bank_of_romania' => NationalBankOfRomania::class,
45+
'national_bank_of_ukraine' => NationalBankOfUkraine::class,
4446
'open_exchange_rates' => OpenExchangeRates::class,
4547
'array' => PhpArray::class,
4648
'russian_central_bank' => RussianCentralBank::class,
4749
'webservicex' => WebserviceX::class,
4850
'xignite' => Xignite::class,
49-
'national_bank_of_ukraine' => NationalBankOfUkraine::class,
5051
'coin_layer' => CoinLayer::class,
5152
'xchangeapi' => XchangeApi::class,
5253
'fastforex' => FastForex::class,

0 commit comments

Comments
 (0)