Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Service/Frankfurter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);


namespace Exchanger\Service;

use Exchanger\Contract\ExchangeRate;
use Exchanger\Contract\ExchangeRateQuery;
use Exchanger\Contract\HistoricalExchangeRateQuery as HistoricalExchangeRateQueryContract;
use Exchanger\Exception\UnsupportedCurrencyPairException;

/**
* Uses the free API at https://frankfurter.dev/
*/
final class Frankfurter extends HttpService
{
private const BASE_URL = "https://api.frankfurter.dev/v2/rates";

#[\Override]
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
{
return true;
}

#[\Override]
public function getName(): string
{
return 'frankfurter';
}

#[\Override]
public function getExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRate
{
$currencyPair = $exchangeQuery->getCurrencyPair();
$base = $currencyPair->getBaseCurrency();
$quote = $currencyPair->getQuoteCurrency();

$url = self::BASE_URL . "?base={$base}&quotes={$quote}";

if ($exchangeQuery instanceof HistoricalExchangeRateQueryContract) {
$url .= '&date=' . $exchangeQuery->getDate()->format('Y-m-d');
}

$content = $this->request($url);
$data = json_decode($content, true);

if (!isset($data[0]['rate'])) {
throw new UnsupportedCurrencyPairException($currencyPair, $this);
}

$rate = (float)$data[0]['rate'];
$date = new \DateTime($data[0]['date']);

return $this->createRate($currencyPair, $rate, $date);
}
}
1 change: 1 addition & 0 deletions src/Service/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function getServices(): array
'apilayer_currency_data' => ApiLayer\CurrencyData::class,
'apilayer_exchange_rates_data' => ApiLayer\ExchangeRatesData::class,
'unirate_api' => UniRateApi::class,
'frankfurter' => Frankfurter::class,
];
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Service/Frankfurter/EUR_USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"date": "2025-09-05",
"base": "EUR",
"quote": "USD",
"rate": 1.1697
}
]
8 changes: 8 additions & 0 deletions tests/Fixtures/Service/Frankfurter/EUR_USD_historic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"date": "1999-04-13",
"base": "EUR",
"quote": "USD",
"rate": 1.0765
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": 422,
"message": "invalid currency: ZZZ"
}
60 changes: 60 additions & 0 deletions tests/Tests/Service/FrankfurterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Exchanger\Tests\Service;

use Exchanger\CurrencyPair;
use Exchanger\Exception\UnsupportedCurrencyPairException;
use Exchanger\ExchangeRateQuery;
use Exchanger\HistoricalExchangeRateQuery;
use Exchanger\Service\Frankfurter;

class FrankfurterTest extends ServiceTestCase
{
public function testItHasAName(): void
{
$service = new Frankfurter($this->createMock('Http\Client\HttpClient'));

$this->assertSame('frankfurter', $service->getName());
}

public function testFetchLatestRate(): void
{
$url = 'https://api.frankfurter.dev/v2/rates?base=EUR&quotes=USD';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Frankfurter/EUR_USD.json');

$pair = CurrencyPair::createFromString('EUR/USD');
$service = new Frankfurter($this->getHttpAdapterMock($url, $content));

$rate = $service->getExchangeRate(new ExchangeRateQuery($pair));

$this->assertSame(1.1697, $rate->getValue());
$this->assertEquals(new \DateTime('2025-09-05'), $rate->getDate());
}

public function testFetchHistoricRate(): void
{
$url = 'https://api.frankfurter.dev/v2/rates?base=EUR&quotes=USD&date=1999-04-13';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Frankfurter/EUR_USD_historic.json');

$pair = CurrencyPair::createFromString('EUR/USD');
$service = new Frankfurter($this->getHttpAdapterMock($url, $content));

$rate = $service->getExchangeRate(new HistoricalExchangeRateQuery($pair, new \DateTime("1999-04-13")));

$this->assertSame(1.0765, $rate->getValue());
$this->assertEquals(new \DateTime('1999-04-13'), $rate->getDate());
}

public function testUnsupportedCurrencyPair(): void
{
$this->expectException(UnsupportedCurrencyPairException::class);

$url = 'https://api.frankfurter.dev/v2/rates?base=EUR&quotes=ZZZ';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Frankfurter/error_invalid_currency.json');

$pair = CurrencyPair::createFromString('EUR/ZZZ');
$service = new Frankfurter($this->getHttpAdapterMock($url, $content, 422));

$service->getExchangeRate(new ExchangeRateQuery($pair));
}
}