From fb87bf7d4bf7940adf17bf6b86400fb7ee61da6c Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 13 May 2024 23:09:32 +0300 Subject: [PATCH 1/2] EuropeanCentralBank search previous days rate, if no exist requested rate Signed-off-by: uldisn --- src/Service/EuropeanCentralBank.php | 35 +++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Service/EuropeanCentralBank.php b/src/Service/EuropeanCentralBank.php index 1588cd6..6a90c55 100755 --- a/src/Service/EuropeanCentralBank.php +++ b/src/Service/EuropeanCentralBank.php @@ -13,6 +13,10 @@ namespace Exchanger\Service; +use DateInterval; +use DateTime; +use DateTimeInterface; +use Exception; use Exchanger\Contract\ExchangeRateQuery; use Exchanger\Contract\HistoricalExchangeRateQuery; use Exchanger\Exception\UnsupportedCurrencyPairException; @@ -29,11 +33,11 @@ final class EuropeanCentralBank extends HttpService { use SupportsHistoricalQueries; - const DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; + private const DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; - const HISTORICAL_URL_LIMITED_TO_90_DAYS_BACK = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'; + private const HISTORICAL_URL_LIMITED_TO_90_DAYS_BACK = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'; - const HISTORICAL_URL_OLDER_THAN_90_DAYS = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml'; + private const HISTORICAL_URL_OLDER_THAN_90_DAYS = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml'; /** * {@inheritdoc} @@ -48,7 +52,7 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): Exch $quoteCurrency = $currencyPair->getQuoteCurrency(); $elements = $element->xpath('//xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate'); - $date = new \DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]); + $date = new DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]); if (empty($elements) || !$date) { throw new UnsupportedCurrencyPairException($currencyPair, $this); @@ -72,13 +76,20 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan $formattedDate = $exchangeQuery->getDate()->format('Y-m-d'); $quoteCurrency = $currencyPair->getQuoteCurrency(); - $elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate'); - - if (empty($elements)) { - if (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) { + $prevDays = 0; + while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) { + $prevDays ++; + if ($prevDays > 7) { throw new UnsupportedDateException($exchangeQuery->getDate(), $this); } + $formattedDate = $exchangeQuery + ->getDate() + ->sub(new DateInterval('P'.$prevDays.'D')) + ->format('Y-m-d'); + } + $elements = $element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]/xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate'); + if (empty($elements)) { throw new UnsupportedCurrencyPairException($currencyPair, $this); } @@ -102,15 +113,15 @@ public function getName(): string } /** - * @param \DateTimeInterface $date + * @param DateTimeInterface $date * * @return string * - * @throws \Exception + * @throws Exception */ - private function getHistoricalUrl(\DateTimeInterface $date): string + private function getHistoricalUrl(DateTimeInterface $date): string { - $dateDiffInDays = $date->diff(new \DateTime('now'))->days; + $dateDiffInDays = $date->diff(new DateTime('now'))->days; if ($dateDiffInDays > 90) { return self::HISTORICAL_URL_OLDER_THAN_90_DAYS; } From b5636e21db3b2915d14e209eeafee284d482806f Mon Sep 17 00:00:00 2001 From: uldis Date: Wed, 2 Apr 2025 20:48:25 +0300 Subject: [PATCH 2/2] Handle missing rates by fetching previous day's data Modified logic to retrieve rates by iterating over previous days if data for the requested date is unavailable. Limits the search to 7 days, ensuring fallback behavior without indefinite looping. --- src/Service/EuropeanCentralBank.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Service/EuropeanCentralBank.php b/src/Service/EuropeanCentralBank.php index 6a90c55..6d7d404 100755 --- a/src/Service/EuropeanCentralBank.php +++ b/src/Service/EuropeanCentralBank.php @@ -76,14 +76,16 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan $formattedDate = $exchangeQuery->getDate()->format('Y-m-d'); $quoteCurrency = $currencyPair->getQuoteCurrency(); + /** + * if rate do not exist for actual date, try get prev day rate, while ge it + */ $prevDays = 0; while (empty($element->xpath('//xmlns:Cube[@time="'.$formattedDate.'"]'))) { $prevDays ++; if ($prevDays > 7) { throw new UnsupportedDateException($exchangeQuery->getDate(), $this); } - $formattedDate = $exchangeQuery - ->getDate() + $formattedDate = (clone $exchangeQuery->getDate()) ->sub(new DateInterval('P'.$prevDays.'D')) ->format('Y-m-d'); }