diff --git a/src/Service/AbstractApi.php b/src/Service/AbstractApi.php index d0df143..73ab113 100644 --- a/src/Service/AbstractApi.php +++ b/src/Service/AbstractApi.php @@ -98,9 +98,16 @@ private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate if (isset($data['exchange_rates'][$currencyPair->getQuoteCurrency()])) { $date = new \DateTime(); - $date->setTimestamp($data['last_updated']); $date->setTimezone(new \DateTimeZone('UTC')); + if (isset($data['last_updated'])) { + $date->setTimestamp($data['last_updated']); + } + + if (isset($data['date'])) { + $date = \DateTime::createFromFormat('Y-m-d', $data['date'], new \DateTimeZone('UTC')); + } + $rate = $data['exchange_rates'][$currencyPair->getQuoteCurrency()]; return $this->createRate($currencyPair, (float) $rate, $date); diff --git a/tests/Fixtures/Service/AbstractApi/historical.json b/tests/Fixtures/Service/AbstractApi/historical.json new file mode 100644 index 0000000..419837f --- /dev/null +++ b/tests/Fixtures/Service/AbstractApi/historical.json @@ -0,0 +1,38 @@ +{ + "base": "USD", + "date": "2021-05-14", + "exchange_rates": { + "EUR": 0.824878, + "JPY": 109.28813, + "BGN": 1.613297, + "CZK": 21.027798, + "DKK": 6.13396, + "GBP": 0.71008, + "HUF": 293.260744, + "PLN": 3.729935, + "RON": 4.063268, + "SEK": 8.35313, + "CHF": 0.903407, + "ISK": 124.47414, + "NOK": 8.254887, + "HRK": 6.20226, + "RUB": 73.926916, + "TRY": 8.428112, + "AUD": 1.289615, + "BRL": 5.274437, + "CAD": 1.212571, + "CNY": 6.436031, + "HKD": 7.766312, + "IDR": 14296.749979, + "ILS": 3.279469, + "INR": 73.25497, + "KRW": 1128.491298, + "MXN": 19.816217, + "MYR": 4.125464, + "NZD": 1.383403, + "PHP": 47.733234, + "SGD": 1.333086, + "THB": 31.355275, + "ZAR": 14.082488 + } +} diff --git a/tests/Tests/Service/AbstractApiTest.php b/tests/Tests/Service/AbstractApiTest.php index c86c85e..c37459e 100644 --- a/tests/Tests/Service/AbstractApiTest.php +++ b/tests/Tests/Service/AbstractApiTest.php @@ -70,7 +70,7 @@ public function it_fetches_a_historical_rate() { $pair = CurrencyPair::createFromString('USD/GBP'); $url = 'https://exchange-rates.abstractapi.com/v1/historical?api_key=secret&base=USD&date=2000-01-03'; - $content = file_get_contents(__DIR__.'/../../Fixtures/Service/AbstractApi/success.json'); + $content = file_get_contents(__DIR__.'/../../Fixtures/Service/AbstractApi/historical.json'); $date = new \DateTime('2000-01-03'); $service = new AbstractApi($this->getHttpAdapterMock($url, $content), null, ['api_key' => 'secret']);