コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
 public function testLoadWithIncorrectUrl()
 {
     $loader = new XMLLoader();
     try {
         $loader->load('http://incorrect_url');
     } catch (\Exception $e) {
         self::assertInstanceOf('\\Exception', $e);
     }
 }