From 8271bd071ce14805946484ae1ed3b768e189b8c5 Mon Sep 17 00:00:00 2001 From: florianv Date: Sat, 3 Jun 2017 10:51:45 +0200 Subject: [PATCH] Added extra rate date checks --- src/Service/CentralBankOfCzechRepublic.php | 5 +++- src/Service/CentralBankOfRepublicTurkey.php | 2 +- src/Service/CurrencyLayer.php | 2 ++ src/Service/EuropeanCentralBank.php | 5 ++-- src/Service/Fixer.php | 2 ++ src/Service/NationalBankOfRomania.php | 2 +- src/Service/OpenExchangeRates.php | 2 ++ src/Service/RussianCentralBank.php | 4 +-- src/Service/Xignite.php | 33 ++++++++++++--------- src/Service/Yahoo.php | 5 +++- 10 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/Service/CentralBankOfCzechRepublic.php b/src/Service/CentralBankOfCzechRepublic.php index 6a37449..43ffdd0 100755 --- a/src/Service/CentralBankOfCzechRepublic.php +++ b/src/Service/CentralBankOfCzechRepublic.php @@ -36,7 +36,10 @@ public function getExchangeRate(ExchangeRateQuery $exchangeQuery) $lines = explode("\n", $content); - $date = \DateTime::createFromFormat(self::DATE_FORMAT, $this->parseDate($lines[0])); + if (!$date = \DateTime::createFromFormat(self::DATE_FORMAT, $this->parseDate($lines[0]))) { + throw new UnsupportedCurrencyPairException($currencyPair, $this); + } + $date->setTime(0, 0, 0); foreach (array_slice($lines, 2) as $currency) { diff --git a/src/Service/CentralBankOfRepublicTurkey.php b/src/Service/CentralBankOfRepublicTurkey.php index ed280d7..0c0a603 100755 --- a/src/Service/CentralBankOfRepublicTurkey.php +++ b/src/Service/CentralBankOfRepublicTurkey.php @@ -40,7 +40,7 @@ public function getExchangeRate(ExchangeRateQuery $exchangeRateQuery) $date = new \DateTime((string) $element->xpath('//Tarih_Date/@Date')[0]); $elements = $element->xpath('//Currency[@CurrencyCode="'.$currencyPair->getBaseCurrency().'"]/ForexSelling'); - if (!empty($elements)) { + if (!empty($elements) || !$date) { return new ExchangeRate((string) $elements[0], $date); } diff --git a/src/Service/CurrencyLayer.php b/src/Service/CurrencyLayer.php index 61e3976..bed00c0 100755 --- a/src/Service/CurrencyLayer.php +++ b/src/Service/CurrencyLayer.php @@ -127,5 +127,7 @@ private function createRate($url, CurrencyPair $currencyPair) if ($data['source'] === $currencyPair->getBaseCurrency() && isset($data['quotes'][$hash])) { return new ExchangeRate((string) $data['quotes'][$hash], $date); } + + return null; } } diff --git a/src/Service/EuropeanCentralBank.php b/src/Service/EuropeanCentralBank.php index 33da35f..4cf1a3c 100755 --- a/src/Service/EuropeanCentralBank.php +++ b/src/Service/EuropeanCentralBank.php @@ -40,13 +40,12 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery) $quoteCurrency = $exchangeQuery->getCurrencyPair()->getQuoteCurrency(); $elements = $element->xpath('//xmlns:Cube[@currency="'.$quoteCurrency.'"]/@rate'); + $date = new \DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]); - if (empty($elements)) { + if (empty($elements) || !$date) { throw new UnsupportedCurrencyPairException($exchangeQuery->getCurrencyPair(), $this); } - $date = new \DateTime((string) $element->xpath('//xmlns:Cube[@time]/@time')[0]); - return new ExchangeRate((string) $elements[0]['rate'], $date); } diff --git a/src/Service/Fixer.php b/src/Service/Fixer.php index 700b4ca..894db6d 100755 --- a/src/Service/Fixer.php +++ b/src/Service/Fixer.php @@ -89,5 +89,7 @@ private function createRate($url, CurrencyPair $currencyPair) return new ExchangeRate($rate, $date); } + + return null; } } diff --git a/src/Service/NationalBankOfRomania.php b/src/Service/NationalBankOfRomania.php index 337ed98..f92e9e1 100755 --- a/src/Service/NationalBankOfRomania.php +++ b/src/Service/NationalBankOfRomania.php @@ -41,7 +41,7 @@ public function getExchangeRate(ExchangeRateQuery $exchangeQuery) $date = new \DateTime((string) $element->xpath('//xmlns:PublishingDate')[0]); $elements = $element->xpath('//xmlns:Rate[@currency="'.$currencyPair->getBaseCurrency().'"]'); - if (empty($elements)) { + if (empty($elements) || !$date) { throw new UnsupportedCurrencyPairException($currencyPair, $this); } diff --git a/src/Service/OpenExchangeRates.php b/src/Service/OpenExchangeRates.php index 33e3a37..3ef2187 100755 --- a/src/Service/OpenExchangeRates.php +++ b/src/Service/OpenExchangeRates.php @@ -126,5 +126,7 @@ private function createRate($url, ExchangeRateQuery $exchangeQuery) ) { return new ExchangeRate((string) $data['rates'][$currencyPair->getQuoteCurrency()], $date); } + + return null; } } diff --git a/src/Service/RussianCentralBank.php b/src/Service/RussianCentralBank.php index 04de636..14d98a8 100644 --- a/src/Service/RussianCentralBank.php +++ b/src/Service/RussianCentralBank.php @@ -27,12 +27,12 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery) $element = StringUtil::xmlToElement($content); $elements = $element->xpath('./Valute[CharCode="'.$baseCurrency.'"]'); + $date = \DateTime::createFromFormat('!d.m.Y', (string) $element['Date']); - if (empty($elements)) { + if (empty($elements) || !$date) { throw new UnsupportedCurrencyPairException($exchangeQuery->getCurrencyPair(), $this); } - $date = \DateTime::createFromFormat('!d.m.Y', (string) $element['Date']); $rate = str_replace(',', '.', (string) $elements['0']->Value); return new ExchangeRate($rate, $date); diff --git a/src/Service/Xignite.php b/src/Service/Xignite.php index c9331e5..bb606d2 100755 --- a/src/Service/Xignite.php +++ b/src/Service/Xignite.php @@ -14,6 +14,8 @@ use Exchanger\Contract\ExchangeRateQuery; use Exchanger\Contract\HistoricalExchangeRateQuery; use Exchanger\Exception\Exception; +use Exchanger\Exception\UnsupportedCurrencyPairException; +use Exchanger\Exception\UnsupportedDateException; use Exchanger\ExchangeRate; use Exchanger\StringUtil; @@ -55,16 +57,17 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery) $json = StringUtil::jsonToArray($content); $data = $json[0]; - if ('Success' === $data['Outcome']) { - $dateString = $data['Date'].' '.$data['Time']; + if ('Success' !== $data['Outcome']) { + throw new Exception($data['Message']); + } + + $dateString = $data['Date'].' '.$data['Time']; - return new ExchangeRate( - (string) $data['Bid'], - \DateTime::createFromFormat('m/d/Y H:i:s A', $dateString, new \DateTimeZone('UTC')) - ); + if (!$date = \DateTime::createFromFormat('m/d/Y H:i:s A', $dateString, new \DateTimeZone('UTC'))) { + throw new UnsupportedCurrencyPairException($currencyPair, $this); } - throw new Exception($data['Message']); + return new ExchangeRate((string) $data['Bid'], $date); } /** @@ -73,12 +76,13 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery) protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery) { $currencyPair = $exchangeQuery->getCurrencyPair(); + $queryDate = $exchangeQuery->getDate(); $symbol = $currencyPair->getBaseCurrency().$currencyPair->getQuoteCurrency(); $url = sprintf( self::HISTORICAL_URL, $symbol, - $exchangeQuery->getDate()->format('m/d/Y'), + $queryDate->format('m/d/Y'), $this->options['token'] ); @@ -87,14 +91,15 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan $json = StringUtil::jsonToArray($content); $data = $json[0]; - if ('Success' === $data['Outcome']) { - return new ExchangeRate( - (string) $data['Average'], - \DateTime::createFromFormat('m/d/Y', $data['StartDate'], new \DateTimeZone('UTC')) - ); + if ('Success' !== $data['Outcome']) { + throw new Exception($data['Message']); + } + + if (!$date = \DateTime::createFromFormat('m/d/Y', $data['StartDate'], new \DateTimeZone('UTC'))) { + throw new UnsupportedDateException($queryDate, $this); } - throw new Exception($data['Message']); + return new ExchangeRate((string) $data['Average'], $date); } /** diff --git a/src/Service/Yahoo.php b/src/Service/Yahoo.php index c3e710f..618d922 100755 --- a/src/Service/Yahoo.php +++ b/src/Service/Yahoo.php @@ -53,7 +53,10 @@ public function getExchangeRate(ExchangeRateQuery $exchangeQuery) } $dateString = $data['Date'].' '.$data['Time']; - $date = \DateTime::createFromFormat('m/d/Y H:ia', $dateString); + + if (!$date = \DateTime::createFromFormat('m/d/Y H:ia', $dateString)) { + throw new UnsupportedCurrencyPairException($currencyPair, $this); + } return new ExchangeRate($data['Rate'], $date); }