public function getRates(Currency $base) { $rates = (array) Request::get(sprintf('http://api.fixer.io/latest?base=%s', $base->getName()))->send()->body->rates; $ret = []; foreach ($rates as $key => $rate) { $ret[$key] = $rate; } return $ret; }
public function getRate(Currency $from, Currency $to, DateTime $date = null) { $rates = $this->getRatesFromStore($date); $dollarFrom = $from->getName() == 'USD' ? 1.0 : $rates[$from->getName()]; $dollarTo = $to->getName() == 'USD' ? 1.0 : $rates[$to->getName()]; $rate = $dollarFrom / $dollarTo; return new CurrencyPair($from, $to, $rate); }
/** * Retrieves exchange rates from http://fixer.io * * @param Currency $currency */ private function retrieveExchangeRateFor(Currency $currency) { $response = $this->client->request('GET', self::EXCHANGE_RATE_API_URL . '/latest', ['query' => ['base' => $this->baseCurrency->getName()]]); Assert::same($response->getStatusCode(), 200); $rawExchangeRates = $response->getBody(); $exchangeRates = json_decode($rawExchangeRates, true); Assert::isArray($exchangeRates); Assert::keyExists($exchangeRates, 'rates'); Assert::keyExists($exchangeRates['rates'], $currency->getName()); Assert::numeric($exchangeRates['rates'][$currency->getName()]); $this->exchangeRates[$currency->getName()] = $exchangeRates['rates'][$currency->getName()]; }
/** * Retrieves exchange rates from http://free.currencyconverterapi.com * * @param Currency $currency */ private function retrieveExchangeRateFor(Currency $currency) { $conversion = sprintf('%s_%s', $currency->getName(), $this->baseCurrency->getName()); $response = $this->client->request('GET', self::EXCHANGE_RATE_API_URL . '/api/v3/convert', ['query' => ['q' => $conversion]]); Assert::same($response->getStatusCode(), 200); $rawExchangeRates = $response->getBody(); $exchangeRates = json_decode($rawExchangeRates, true); Assert::isArray($exchangeRates); Assert::keyExists($exchangeRates, 'results'); Assert::keyExists($exchangeRates['results'], $conversion); Assert::keyExists($exchangeRates['results'][$conversion], 'val'); Assert::numeric($exchangeRates['results'][$conversion]['val']); $this->exchangeRates[$currency->getName()] = (double) $exchangeRates['results'][$conversion]['val']; }
/** * @inheritdoc */ public function getRelativeRatio($referenceCurrencyCode, $currencyCode) { $currency = new Currency($currencyCode); $referenceCurrency = new Currency($referenceCurrencyCode); if ($currencyCode === $referenceCurrencyCode) { return (double) 1; } $ratioList = $this->storage->loadRatioList(); if (!array_key_exists($currency->getName(), $ratioList)) { throw new MoneyException("unknown ratio for currency {$currencyCode}"); } if (!array_key_exists($referenceCurrency->getName(), $ratioList)) { throw new MoneyException("unknown ratio for currency {$referenceCurrencyCode}"); } return $ratioList[$currency->getName()] / $ratioList[$referenceCurrency->getName()]; }
public function getCurrencyPair(Currency $counterCurrency, Currency $baseCurrency) { $from = $counterCurrency->getName(); $to = $baseCurrency->getName(); $pair = null; $url = self::URL . sprintf(self::QUERY, $from, $to); $html = @file_get_contents($url); if ($html == false) { throw new CurrencyRateException('Rate not avaiable, resource not found'); } $doc = new \DOMDocument(); @$doc->loadHTML($html); $el = $doc->getElementById('currency_converter_result'); if ($el && $el->hasChildNodes()) { foreach ($el->childNodes as $node) { if ($node->nodeName == 'span' && preg_match('/[0-9\\.]+ ' . $to . '/i', $node->textContent, $matches)) { $ratio = (double) $matches[0]; $pair = new CurrencyPair($counterCurrency, $baseCurrency, $ratio); } } } if (!$pair) { throw new CurrencyRateException('Rate not avaiable, can not parse result: ' . $html); } return $pair; }
private function getRates(Currency $currency) { $rates = $this->pool->getItem($currency->getName()); if ($rates->isMiss()) { $newRates = $this->rates->getRates($currency); $rates->set($newRates, $this->ttl); } return $newRates; }
public function getCurrencyPair(Currency $counterCurrency, Currency $baseCurrency) { $from = $counterCurrency->getName(); $to = $baseCurrency->getName(); $pair = null; $result = file_get_contents(sprintf(self::URL, $from, $to, $this->key)); $result = json_decode($result); if ($result && isset($result->rate)) { $pair = new CurrencyPair($counterCurrency, $baseCurrency, $result->rate); } if ($pair == null) { throw new CurrencyRateException('Rate not avaiable, missing in result'); } return $pair; }
public function getCurrencyPair(Currency $counterCurrency, Currency $baseCurrency) { $from = $counterCurrency->getName(); $to = $baseCurrency->getName(); $pair = null; $url = self::URL . sprintf(self::QUERY, $from, $to); $csv = @file_get_contents($url); if ($csv == false) { throw new CurrencyRateException('Rate not avaiable, resource not found'); } if (preg_match('/"' . $to . '",([0-9\\.]+)/i', $csv, $matches)) { if ($matches && isset($matches[1])) { $ratio = (double) $matches[1]; } else { throw new CurrencyRateException('Rate not avaiable, can not parse result: ' . $csv); } $pair = new CurrencyPair($counterCurrency, $baseCurrency, $ratio); } else { throw new CurrencyRateException('Rate not avaiable, can not parse result: ' . $csv); } return $pair; }
/** * @param $units * @param Currency $referenceCurrency * @param Currency $currency * @return string The endpoint to get Currency conversion */ protected function getEndpoint($units, Currency $referenceCurrency, Currency $currency) { return sprintf('https://www.google.com/finance/converter?a=%s&from=%s&to=%s', $units, $referenceCurrency->getName(), $currency->getName()); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['currency' => $this->currency->getName()]); }
/** * @param Currency $referenceCurrency * @param Currency $currency * * @return string The yahoo finance endpoint to get Currency exchange rate */ protected function getEndpoint(Currency $referenceCurrency, Currency $currency) { return 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22' . $referenceCurrency->getName() . $currency->getName() . '%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'; }
public function getRates(Currency $base) { return $this->rates[$base->getName()]; }
/** * @return string */ public function getCurrencySymbol() { return Intl::getCurrencyBundle()->getCurrencySymbol($this->currency->getName(), $this->locale); }
/** * Returns the name as string of the given currency * * @param Currency $currency * @return string */ public function formatCurrencyAsName(Currency $currency) { return $currency->getName(); }
/** * @param Currency $referenceCurrency * @param Currency $currency * * @return string The yahoo finance endpoint to get Currency exchange rate */ protected function getEndpoint(Currency $referenceCurrency, Currency $currency) { $q = sprintf('select * from yahoo.finance.xchange where pair in ("%s%s")', $referenceCurrency->getName(), $currency->getName()); $queryString = http_build_query(array('q' => $q, 'format' => 'json', 'env' => 'store://datatables.org/alltableswithkeys')); return 'https://query.yahooapis.com/v1/public/yql?' . $queryString; }