Example #1
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $queryPairs = sprintf('"%s%s"', $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $query = sprintf('select * from yahoo.finance.xchange where pair in (%s)', $queryPairs);
     $url = sprintf(self::URL, urlencode($query));
     $content = $this->fetchContent($url);
     $json = StringUtil::jsonToArray($content);
     $data = $json['query']['results']['rate'];
     if ('0.00' === $data['Rate'] || 'N/A' === $data['Date']) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $dateString = $data['Date'] . ' ' . $data['Time'];
     $date = \DateTime::createFromFormat('m/d/Y H:ia', $dateString);
     return new Rate($data['Rate'], $date, "Yahoo Finance");
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     if ('USD' !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $url = sprintf(self::URL, $this->accKey);
     $content = $this->fetchContent($url);
     $data = StringUtil::jsonToArray($content);
     if (isset($data['error'])) {
         throw new Exception($data['info']);
     }
     $date = new \DateTime();
     $date->setTimestamp($data['timestamp']);
     if ($data['source'] === $currencyPair->getBaseCurrency() && isset($data['quotes']['USD' . $currencyPair->getQuoteCurrency()])) {
         return new Rate((string) $data['quotes']['USD' . $currencyPair->getQuoteCurrency()], $date, 'Currencylayer');
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $content = $this->fetchContent($url);
     $document = new \DOMDocument();
     @$document->loadHTML($content);
     $xpath = new \DOMXPath($document);
     $nodes = $xpath->query('//span[@class="bld"]');
     if (0 === $nodes->length) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     $nodeContent = $nodes->item(0)->textContent;
     $bid = strstr($nodeContent, ' ', true);
     if (!is_numeric($bid)) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     return new Rate($bid, new \DateTime(), "Google Finance");
 }
Example #4
0
File: CuEx.php Project: tautiz/cuex
 public function getRates($currencyPair)
 {
     if (is_string($currencyPair)) {
         $currencyPair = CurrencyPair::createFromString($currencyPair);
     } elseif (!$currencyPair instanceof CurrencyPair) {
         throw new \InvalidArgumentException('The currency pair must be either a string or an instance of CurrencyPair');
     }
     /**
      * @TODO: Imti is Cache duomenis
      */
     if (null !== $this->cache && null !== ($rate = $this->cache->fetchRate($currencyPair))) {
         return $rate;
     }
     if (isset($this->chainProviders)) {
         $rate = $this->chainProviders->fetchRates($currencyPair);
     } else {
         $rate = $this->provider->fetchRates($currencyPair);
     }
     if (null !== $this->cache) {
         $this->cache->storeRate($currencyPair, $rate);
     }
     return $rate;
 }
 public function __construct(CurrencyPair $currencyPair)
 {
     parent::__construct(sprintf('The currency pair "%s" is not supported.', $currencyPair->toString()));
     $this->currencyPair = $currencyPair;
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function storeRate(CurrencyPair $currencyPair, Rate $rate)
 {
     $this->cache->save($currencyPair->toString(), $rate, $this->ttl);
 }