Skip to content

Commit 330216f

Browse files
authored
Merge pull request florianv#150 from uguurozkan/implement-the-central-bank-of-the-republic-of-uzbekistan-service
Implement the Central Bank of the Republic of Uzbekistan Service
2 parents a9f4c50 + 6604778 commit 330216f

File tree

6 files changed

+2213
-1
lines changed

6 files changed

+2213
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Here is the complete list of the currently implemented services:
4949
| [European Central Bank](https://www.ecb.europa.eu/home/html/index.en.html) | EUR | * | Yes |
5050
| [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 |
5151
| [Central Bank of the Republic of Turkey](http://www.tcmb.gov.tr) | * | TRY | Yes |
52+
| [Central Bank of the Republic of Uzbekistan](https://cbu.uz) | * | UZS | Yes |
5253
| [Central Bank of the Czech Republic](https://www.cnb.cz) | * | CZK | Yes |
5354
| [Central Bank of Russia](https://cbr.ru) | * | RUB | Yes |
5455
| [Bulgarian National Bank](http://bnb.bg) | * | BGN | 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+
* The Central Bank of the Republic of Uzbekistan Service.
27+
*
28+
* @author Uğur Özkan
29+
*/
30+
final class CentralBankOfRepublicUzbekistan extends HttpService
31+
{
32+
use SupportsHistoricalQueries;
33+
34+
private const BASE_URL = 'https://cbu.uz/common/json';
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function getName(): string
40+
{
41+
return 'central_bank_of_republic_uzbekistan';
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
48+
{
49+
return 'UZS' === $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);
86+
87+
$currencyInfo = array_values(array_filter($element, function ($currency) use ($currencyPair) {
88+
return $currency['Ccy'] === $currencyPair->getBaseCurrency();
89+
}));
90+
if (!empty($currencyInfo)) {
91+
$rate = (float) $currencyInfo[0]['Rate'];
92+
$unit = (int) $currencyInfo[0]['Nominal'];
93+
94+
$date = new DateTime((string) $currencyInfo[0]['Date']);
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('d.m.Y');
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
@@ -21,14 +21,15 @@
2121
final class Registry
2222
{
2323
/**
24-
* Returns a map of all supports services.
24+
* Returns a map of all supported services.
2525
*/
2626
public static function getServices(): array
2727
{
2828
return [
2929
'bulgarian_national_bank' => BulgarianNationalBank::class,
3030
'central_bank_of_czech_republic' => CentralBankOfCzechRepublic::class,
3131
'central_bank_of_republic_turkey' => CentralBankOfRepublicTurkey::class,
32+
'central_bank_of_republic_uzbekistan' => CentralBankOfRepublicUzbekistan::class,
3233
'cryptonator' => Cryptonator::class,
3334
'currency_converter' => CurrencyConverter::class,
3435
'currency_data_feed' => CurrencyDataFeed::class,

0 commit comments

Comments
 (0)