Skip to content

Commit a9f4c50

Browse files
authored
Merge pull request florianv#145 from goaround/exchangerate
Add exchangerate.host
2 parents 4e3b1db + 093c433 commit a9f4c50

File tree

8 files changed

+439
-0
lines changed

8 files changed

+439
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Here is the complete list of the currently implemented services:
6161
| [Currency Converter API](https://www.currencyconverterapi.com) | * | * | Yes (free but limited or paid) |
6262
| [xChangeApi.com](https://xchangeapi.com) | * | * | Yes |
6363
| [fastFOREX.io](https://www.fastforex.io) | USD (free), * (paid) | * | No |
64+
| [exchangerate.host](https://www.exchangerate.host) | * | * | Yes |
6465
| Array | * | * | Yes |
6566

6667
## Credits

doc/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ use Exchanger\Service\Cryptonator;
414414
use Exchanger\Service\CoinLayer;
415415
use Exchanger\Service\XchangeApi;
416416
use Exchanger\Service\AbstractApi;
417+
use Exchanger\Service\ExchangerateHost;
417418
use Exchanger\Service\FixerApiLayer;
418419

419420
$service = new Chain([
@@ -434,6 +435,7 @@ $service = new Chain([
434435
new CurrencyDataFeed($client, null, ['api_key' => 'api_key']),
435436
new OpenExchangeRates($client, null, ['app_id' => 'app_id', 'enterprise' => false]),
436437
new Xignite($client, null, ['token' => 'token']),
438+
new ExchangerateHost(),
437439
new PhpArray(
438440
[
439441
'EUR/USD' => 1.1,

src/Service/ExchangerateHost.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 Exchanger\Contract\CurrencyPair;
17+
use Exchanger\Contract\ExchangeRateQuery;
18+
use Exchanger\Contract\HistoricalExchangeRateQuery;
19+
use Exchanger\Exception\Exception;
20+
use Exchanger\Exception\UnsupportedCurrencyPairException;
21+
use Exchanger\ExchangeRate;
22+
use Exchanger\StringUtil;
23+
use Exchanger\Contract\ExchangeRate as ExchangeRateContract;
24+
25+
/**
26+
* Exchangerate Service.
27+
*
28+
* @author Florian Voutzinos <[email protected]>
29+
*/
30+
final class ExchangerateHost extends HttpService
31+
{
32+
use SupportsHistoricalQueries;
33+
34+
const LATEST_URL = 'https://api.exchangerate.host/latest?base=%s&v=%s';
35+
36+
const HISTORICAL_URL = 'https://api.exchangerate.host/%s?base=%s';
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRateContract
42+
{
43+
$currencyPair = $exchangeQuery->getCurrencyPair();
44+
45+
$url = sprintf(
46+
self::LATEST_URL,
47+
$currencyPair->getBaseCurrency(),
48+
date('Y-m-d')
49+
);
50+
51+
return $this->doCreateRate($url, $currencyPair);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRateContract
58+
{
59+
$currencyPair = $exchangeQuery->getCurrencyPair();
60+
61+
$url = sprintf(
62+
self::HISTORICAL_URL,
63+
$exchangeQuery->getDate()->format('Y-m-d'),
64+
$exchangeQuery->getCurrencyPair()->getBaseCurrency()
65+
);
66+
67+
return $this->doCreateRate($url, $currencyPair);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
74+
{
75+
return true;
76+
}
77+
78+
/**
79+
* Creates a rate.
80+
*
81+
* @param string $url
82+
* @param CurrencyPair $currencyPair
83+
*
84+
* @return ExchangeRate
85+
*
86+
* @throws Exception
87+
*/
88+
private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate
89+
{
90+
$content = $this->request($url);
91+
$data = StringUtil::jsonToArray($content);
92+
93+
if (false === $data['success']) {
94+
throw new Exception();
95+
}
96+
97+
if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) {
98+
$date = new \DateTime($data['date']);
99+
$rate = $data['rates'][$currencyPair->getQuoteCurrency()];
100+
101+
return $this->createRate($currencyPair, (float) $rate, $date);
102+
}
103+
104+
throw new UnsupportedCurrencyPairException($currencyPair, $this);
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function getName(): string
111+
{
112+
return 'exchangeratehost';
113+
}
114+
}

src/Service/Registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function getServices(): array
4949
'xchangeapi' => XchangeApi::class,
5050
'fastforex' => FastForex::class,
5151
'abstract_api' => AbstractApi::class,
52+
'exchangeratehost' => ExchangerateHost::class,
5253
];
5354
}
5455
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"success": false
3+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"motd": {
3+
"msg": "If you or your company use this project or like what we doing, please consider backing us so we can continue maintaining and evolving this project.",
4+
"url": "https://exchangerate.host/#/donate"
5+
},
6+
"success": true,
7+
"historical": true,
8+
"base": "EUR",
9+
"date": "2000-01-03",
10+
"rates": {
11+
"USD": 1.009,
12+
"JPY": 102.75,
13+
"CYP": 0.5767,
14+
"CZK": 36.063,
15+
"DKK": 7.4404,
16+
"EEK": 15.6466,
17+
"GBP": 0.6246,
18+
"HUF": 254.53,
19+
"LTL": 4.0454,
20+
"LVL": 0.5916,
21+
"MTL": 0.4151,
22+
"PLN": 4.1835,
23+
"ROL": 18273,
24+
"SEK": 8.552,
25+
"SIT": 198.8925,
26+
"SKK": 42.317,
27+
"CHF": 1.6043,
28+
"ISK": 73.03,
29+
"NOK": 8.062,
30+
"TRL": 546131,
31+
"AUD": 1.5346,
32+
"CAD": 1.4577,
33+
"HKD": 7.8624,
34+
"KRW": 1140.02,
35+
"NZD": 1.9331,
36+
"SGD": 1.6769,
37+
"ZAR": 6.2013,
38+
"EUR": 1
39+
}
40+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
"motd": {
3+
"msg": "If you or your company use this project or like what we doing, please consider backing us so we can continue maintaining and evolving this project.",
4+
"url": "https://exchangerate.host/#/donate"
5+
},
6+
"success": true,
7+
"base": "EUR",
8+
"date": "2022-04-29",
9+
"rates": {
10+
"AED": 3.865872,
11+
"AFN": 89.969025,
12+
"ALL": 121.235589,
13+
"AMD": 484.288186,
14+
"ANG": 1.893457,
15+
"AOA": 429.029403,
16+
"ARS": 121.207841,
17+
"AUD": 1.469691,
18+
"AWG": 1.895006,
19+
"AZN": 1.789468,
20+
"BAM": 1.957715,
21+
"BBD": 2.104445,
22+
"BDT": 90.844466,
23+
"BGN": 1.958051,
24+
"BHD": 0.397529,
25+
"BIF": 2157.445311,
26+
"BMD": 1.053064,
27+
"BND": 1.457922,
28+
"BOB": 7.223019,
29+
"BRL": 5.197897,
30+
"BSD": 1.052947,
31+
"BTC": 0.000027,
32+
"BTN": 80.326669,
33+
"BWP": 12.757849,
34+
"BYN": 3.534337,
35+
"BZD": 2.117805,
36+
"CAD": 1.342341,
37+
"CDF": 2108.6172,
38+
"CHF": 1.021468,
39+
"CLF": 0.033798,
40+
"CLP": 903.245274,
41+
"CNH": 6.985448,
42+
"CNY": 6.963063,
43+
"COP": 4150.302654,
44+
"CRC": 694.997591,
45+
"CUC": 1.053143,
46+
"CUP": 27.097053,
47+
"CVE": 110.908464,
48+
"CZK": 24.551003,
49+
"DJF": 187.023086,
50+
"DKK": 7.438138,
51+
"DOP": 57.841255,
52+
"DZD": 152.250561,
53+
"EGP": 19.454636,
54+
"ERN": 15.783828,
55+
"ETB": 54.468888,
56+
"EUR": 1,
57+
"FJD": 2.269886,
58+
"FKP": 0.842468,
59+
"GBP": 0.842218,
60+
"GEL": 3.209843,
61+
"GGP": 0.842395,
62+
"GHS": 7.905807,
63+
"GIP": 0.842392,
64+
"GMD": 56.665076,
65+
"GNF": 9328.143356,
66+
"GTQ": 8.046967,
67+
"GYD": 219.783532,
68+
"HKD": 8.25791,
69+
"HNL": 25.785301,
70+
"HRK": 7.56964,
71+
"HTG": 112.419006,
72+
"HUF": 378.153666,
73+
"IDR": 15191.987548,
74+
"ILS": 3.482014,
75+
"IMP": 0.84248,
76+
"INR": 80.491096,
77+
"IQD": 1533.245559,
78+
"IRR": 44510.605079,
79+
"ISK": 137.741725,
80+
"JEP": 0.842211,
81+
"JMD": 162.468931,
82+
"JOD": 0.746078,
83+
"JPY": 137.241222,
84+
"KES": 121.599609,
85+
"KGS": 89.520618,
86+
"KHR": 4248.352776,
87+
"KMF": 493.378287,
88+
"KPW": 947.035017,
89+
"KRW": 1325.967747,
90+
"KWD": 0.323092,
91+
"KYD": 0.876598,
92+
"KZT": 469.423848,
93+
"LAK": 12648.279216,
94+
"LBP": 1588.655795,
95+
"LKR": 367.68829,
96+
"LRD": 159.891645,
97+
"LSL": 16.81458,
98+
"LYD": 5.005199,
99+
"MAD": 10.500451,
100+
"MDL": 19.509362,
101+
"MGA": 4244.100747,
102+
"MKD": 61.659052,
103+
"MMK": 1945.044301,
104+
"MNT": 3171.734767,
105+
"MOP": 8.490796,
106+
"MRU": 38.371568,
107+
"MUR": 45.248445,
108+
"MVR": 16.258093,
109+
"MWK": 857.901365,
110+
"MXN": 21.430748,
111+
"MYR": 4.583859,
112+
"MZN": 67.187428,
113+
"NAD": 16.8465,
114+
"NGN": 436.49558,
115+
"NIO": 37.624111,
116+
"NOK": 9.844793,
117+
"NPR": 128.521671,
118+
"NZD": 1.61334,
119+
"OMR": 0.40496,
120+
"PAB": 1.053441,
121+
"PEN": 4.021937,
122+
"PGK": 3.703499,
123+
"PHP": 55.023561,
124+
"PKR": 195.135888,
125+
"PLN": 4.679294,
126+
"PYG": 7185.228198,
127+
"QAR": 3.831892,
128+
"RON": 4.946873,
129+
"RSD": 117.555329,
130+
"RUB": 76.357842,
131+
"RWF": 1070.066289,
132+
"SAR": 3.947075,
133+
"SBD": 8.448919,
134+
"SCR": 13.613,
135+
"SDG": 470.360978,
136+
"SEK": 10.335707,
137+
"SGD": 1.455141,
138+
"SHP": 0.841994,
139+
"SLL": 12934.013562,
140+
"SOS": 607.725225,
141+
"SRD": 21.841819,
142+
"SSP": 137.067579,
143+
"STD": 22818.462406,
144+
"STN": 24.360577,
145+
"SVC": 9.192525,
146+
"SYP": 2643.751392,
147+
"SZL": 16.737671,
148+
"THB": 36.120243,
149+
"TJS": 13.084253,
150+
"TMT": 3.694332,
151+
"TND": 3.227662,
152+
"TOP": 2.427466,
153+
"TRY": 15.566195,
154+
"TTD": 7.130605,
155+
"TWD": 30.949213,
156+
"TZS": 2445.452634,
157+
"UAH": 31.778726,
158+
"UGX": 3730.424391,
159+
"USD": 1.053007,
160+
"UYU": 43.633115,
161+
"UZS": 11791.452596,
162+
"VES": 4.695096,
163+
"VND": 24164.629013,
164+
"VUV": 118.196588,
165+
"WST": 2.714356,
166+
"XAF": 655.69554,
167+
"XAG": 0.045365,
168+
"XAU": 0.001408,
169+
"XCD": 2.844199,
170+
"XDR": 0.766485,
171+
"XOF": 655.695496,
172+
"XPD": 0.000616,
173+
"XPF": 119.28456,
174+
"XPT": 0.001452,
175+
"YER": 263.328796,
176+
"ZAR": 16.713833,
177+
"ZMW": 17.89469,
178+
"ZWL": 338.82794
179+
}
180+
}

0 commit comments

Comments
 (0)