Skip to content

Commit

Permalink
Merge pull request #22 from florianv/dates
Browse files Browse the repository at this point in the history
Added extra rate date checks
  • Loading branch information
florianv authored Jun 3, 2017
2 parents 978fb4c + 8271bd0 commit d298b75
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/Service/CentralBankOfCzechRepublic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Service/CentralBankOfRepublicTurkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Service/CurrencyLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 2 additions & 3 deletions src/Service/EuropeanCentralBank.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Service/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ private function createRate($url, CurrencyPair $currencyPair)

return new ExchangeRate($rate, $date);
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Service/NationalBankOfRomania.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Service/OpenExchangeRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ private function createRate($url, ExchangeRateQuery $exchangeQuery)
) {
return new ExchangeRate((string) $data['rates'][$currencyPair->getQuoteCurrency()], $date);
}

return null;
}
}
4 changes: 2 additions & 2 deletions src/Service/RussianCentralBank.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 19 additions & 14 deletions src/Service/Xignite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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']
);

Expand All @@ -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);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Service/Yahoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d298b75

Please sign in to comment.