예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency() . $currencyPair->getQuoteCurrency(), $this->token);
     $content = $this->fetchContent($url);
     $json = StringUtil::jsonToArray($content);
     $data = $json[0];
     if ('Success' === $data['Outcome']) {
         $dateString = $data['Date'] . ' ' . $data['Time'];
         return new Rate((string) $data['Bid'], \DateTime::createFromFormat('m/d/Y H:i:s A', $dateString, new \DateTimeZone('UTC')));
     }
     throw new Exception($data['Message']);
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair, $date = null)
 {
     $getAppend = function ($from, $to, $date = null) {
         $date = isset($date) ? $date : 'latest';
         return "{$date}?symbols={$to}&base={$from}";
     };
     $url = self::URL . $getAppend($currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency(), $date);
     $content = $this->fetchContent($url);
     $return = StringUtil::jsonToArray($content);
     $return['date'] = isset($date) ? new \DateTime($date) : new \DateTime();
     return new Rate(current($return['rates']), $return['date']);
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
예제 #3
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);
 }
 /**
  * {@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);
 }
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     if (!$this->enterprise && 'USD' !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     if ($this->enterprise) {
         $url = sprintf(self::ENTERPRISE_URL, $this->appId, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     } else {
         $url = sprintf(self::FREE_URL, $this->appId);
     }
     $content = $this->fetchContent($url);
     $data = StringUtil::jsonToArray($content);
     if (isset($data['error'])) {
         throw new Exception($data['description']);
     }
     $date = new \DateTime();
     $date->setTimestamp($data['timestamp']);
     if ($data['base'] === $currencyPair->getBaseCurrency() && isset($data['rates'][$currencyPair->getQuoteCurrency()])) {
         return new Rate((string) $data['rates'][$currencyPair->getQuoteCurrency()], $date);
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
예제 #7
0
 /**
  * @test
  * @expectedException \RuntimeException
  */
 function it_throws_an_exception_when_converting_invalid_json()
 {
     StringUtil::jsonToArray('/');
 }
예제 #8
0
 /**
  * {@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());
 }