/** * Load rates by date * * @param ICurrency[] $currencies * @param \DateTime $date * @throws \Exception * @return ICurrencyRate[] */ public function getRates($currencies, \DateTime $date = null) { if ($date === null) { $date = new \DateTime('now'); } if ($date->format('Y-m-d') !== date('Y-m-d')) { throw new NoRatesAvailableForDateException($date, $this); } $ratesXml = $this->xmlLoader->load(self::BASE_URL); if (false === $ratesXml) { throw new BadXMLQueryException(self::BASE_URL, $this); } $result = array(); foreach ($currencies as $currency) { $rate = null; foreach ($ratesXml->Cube->Cube->Cube as $ecbRate) { if ((string) $ecbRate['currency'] === $currency->getCode()) { $rate = (double) $ecbRate['rate']; break; } } if (!$rate) { continue; } $rate = $this->currencyRateManager->getNewInstance($this->currencyManager->getCurrency($currency->getCode()), $this, $date, $rate, 1); $result[$currency->getCode()] = $rate; } return $result; }
/** * Load rates by date * * @param ICurrency[] $currencies * @param \DateTime|null $date * * @return ICurrencyRate[] * * @throws NoRatesAvailableForDateException * @throws BadXMLQueryException */ public function getRates($currencies, \DateTime $date = null) { $currencyCodes = []; foreach ($currencies as $currency) { $currencyCodes[] = $currency->getCode() . '=X'; } if (null === $date) { $date = new \DateTime(); } $queryData = ['q' => 'select * from yahoo.finance.historicaldata where symbol in ("' . implode('","', $currencyCodes) . '") and startDate = "' . $date->format('Y-m-d') . '" and endDate = "' . $date->format('Y-m-d') . '"', 'env' => 'store://datatables.org/alltableswithkeys']; $query = self::BASE_URL . '?' . http_build_query($queryData); $ratesXml = $this->xmlLoader->load($query); if (false === $ratesXml) { throw new BadXMLQueryException($query, $this); } if (0 === count($ratesXml->results->quote)) { throw new NoRatesAvailableForDateException($date, $this); } $rates = []; /** @var \SimpleXMLElement $rate */ foreach ($ratesXml->results->quote as $quote) { $quote = (array) $quote; $code = (string) $quote['@attributes']['Symbol']; $code = str_replace('%3dX', '', $code); $rate = $quote['Close']; $rates[$code] = $this->currencyRateManager->getNewInstance($this->currencyManager->getCurrency($code), $this, $date, (double) $rate, 1); } return $rates; }
/** * Load rates by date * * @param ICurrency[] $currencies * @param \DateTime $date * @return ICurrencyRate[] */ public function getRates($currencies, \DateTime $date = null) { if ($date === null) { $date = new \DateTime('now'); } $client = new \SoapClient("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL"); $curs = $client->GetCursOnDate(array("On_date" => $date->format('Y-m-d'))); $ratesXml = new \SimpleXMLElement($curs->GetCursOnDateResult->any); $result = array(); foreach ($currencies as $currency) { $rateCbr = $ratesXml->xpath('ValuteData/ValuteCursOnDate/VchCode[.="' . $currency->getCode() . '"]/parent::*'); if (empty($rateCbr)) { continue; } $rate = $this->currencyRateManager->getNewInstance($this->currencyManager->getCurrency($currency->getCode()), $this, $date, (double) $rateCbr[0]->Vcurs, (int) $rateCbr[0]->Vnom); $result[$currency->getCode()] = $rate; } return $result; }