/** * {@inheritdoc} */ public function fetchRate(CurrencyPair $currencyPair) { if ('EUR' !== $currencyPair->getBaseCurrency()) { throw new UnsupportedCurrencyPairException($currencyPair); } $content = $this->fetchContent(self::URL); $xmlElement = StringUtil::xmlToElement($content); $cube = $xmlElement->Cube->Cube; $cubeAttributes = $cube->attributes(); $date = new \DateTime((string) $cubeAttributes['time']); foreach ($cube->Cube as $cube) { $cubeAttributes = $cube->attributes(); $cubeQuoteCurrency = (string) $cubeAttributes['currency']; if ($cubeQuoteCurrency === $currencyPair->getQuoteCurrency()) { return new Rate((string) $cubeAttributes['rate'], $date); } } throw new UnsupportedCurrencyPairException($currencyPair); }
/** * {@inheritdoc} */ public function fetchRate(CurrencyPair $currencyPair) { $content = $this->fetchContent(self::URL); $xmlElement = StringUtil::xmlToElement($content); $baseCurrency = (string) $xmlElement->Body->OrigCurrency; if ($baseCurrency !== $currencyPair->getBaseCurrency()) { throw new UnsupportedCurrencyPairException($currencyPair); } $cube = $xmlElement->Body->Cube; $cubeAttributes = $cube->attributes(); $date = new \DateTime((string) $cubeAttributes['date']); foreach ($cube->Rate as $rate) { $rateAttributes = $rate->attributes(); $rateQuoteCurrency = (string) $rateAttributes['currency']; if ($rateQuoteCurrency === $currencyPair->getQuoteCurrency()) { $rateValue = !empty($rateAttributes['multiplier']) ? (double) $rate / (int) $rateAttributes['multiplier'] : (double) $rate; return new Rate((string) $rateValue, $date); } } throw new UnsupportedCurrencyPairException($currencyPair); }
/** * @test * @expectedException \RuntimeException */ function it_throws_an_exception_when_converting_invalid_xml() { StringUtil::xmlToElement('/'); }
/** * {@inheritdoc} */ public function fetchRate(CurrencyPair $currencyPair) { $url = sprintf(self::URL, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency()); $content = $this->fetchContent($url); return new Rate((string) StringUtil::xmlToElement($content), new \DateTime()); }