/**
  * @param Currency $from
  * @param Currency $to
  * @return string
  * @throws \RuntimeException
  */
 public function getConversionMultiplier(Currency $from, Currency $to)
 {
     $rates = $this->getConversionRatesTable($from);
     if (!isset($rates[$to->getCode()])) {
         throw new \RuntimeException(sprintf('The target currency "%s" is not present in the conversion rates table.', $to->getCode()));
     }
     return $rates[$to->getCode()];
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function contains(Currency $currency)
 {
     if (null === self::$currencies) {
         self::$currencies = $this->requireCurrencies();
     }
     return array_key_exists($currency->getCode(), self::$currencies);
 }
 /**
  * Update existing route
  *
  * @param void
  * @return null
  */
 function edit()
 {
     if ($this->active_currency->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $currency_data = $this->request->post('currency');
     if (!is_array($currency_data)) {
         $currency_data = array('name' => $this->active_currency->getName(), 'code' => $this->active_currency->getCode(), 'default_rate' => $this->active_currency->getDefaultRate());
     }
     // if
     $this->smarty->assign('currency_data', $currency_data);
     if ($this->request->isSubmitted()) {
         $this->active_currency->setAttributes($currency_data);
         $save = $this->active_currency->save();
         if ($save && !is_error($save)) {
             flash_success('Currency ":name" has been updated', array('name' => $this->active_currency->getName()));
             $this->redirectTo('admin_currencies');
         } else {
             $this->smarty->assign('errors', $save);
         }
         // if
     }
     // if
 }
 /**
  * @param Currency $currency
  * @return array
  */
 public function getConversionRatesTable(Currency $currency)
 {
     switch ($currency->getCode()) {
         case 'USD':
             return array('USD' => '1', 'ZAR' => '12.07682');
         default:
             // ZAR
             return array('USD' => '0.082776', 'ZAR' => '1');
     }
 }
Пример #5
0
 /**
  * @param Money|string|int|float $money
  * @return int
  * @throws CurrencyMismatchException
  */
 public function compare($money)
 {
     if ($money instanceof Money) {
         if (!$this->isSameCurrencyAs($money)) {
             throw new CurrencyMismatchException(sprintf('The monetary values being compared must be of the same currency (%s !== %s).', $this->currency->getCode(), $money->getCurrency()->getCode()));
         }
     }
     $amount = Utils::toStringAmount($money);
     return bccomp($this->amount, $amount, $this->scaleFactor);
 }
 /**
  * @param Currency $currency
  * @return array
  * @throws \RuntimeException
  */
 public function getConversionRatesTable(Currency $currency)
 {
     $ch = curl_init();
     $url = sprintf('http://openexchangerates.org/api/latest.json?app_id=%s', $this->appId);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $response = curl_exec($ch);
     if ($response === false) {
         throw new \RuntimeException(sprintf('An error occurred querying %s for conversion rates - (%s - %d)', $url, curl_error($ch), curl_errno($ch)));
     }
     $result = json_decode($response, true);
     if (is_array($result) && isset($result['rates'])) {
         $rates = $result['rates'];
         // the free plan only supports a base currency of 'usd'
         // we therefore need to convert the table to our own base currency
         // first, our own base must be present in the conversion table
         if (!isset($rates[$currency->getCode()])) {
             return array();
             // nothing more to do
         }
         $usd2base = (string) $rates[$currency->getCode()];
         $table = array();
         foreach ($rates as $code => $rate) {
             if ($code === 'USD') {
                 $table[$code] = '1';
             } else {
                 $rate = (string) $rate;
                 $multiplier = bcdiv('1', $usd2base, 6);
                 $table[$code] = bcmul($rate, $multiplier, 6);
             }
         }
         return $table;
     } else {
         return array();
         // response isn't what we expected / missing rates
     }
 }
Пример #7
0
 /**
  * @depends testSetCode
  * @depends testSetCodeWithExceptionThrown
  */
 public function testConstruct()
 {
     $currency = new Currency('usd');
     $this->assertSame('USD', $currency->getCode());
 }
Пример #8
0
 public function getRateFromXML(Currency $from, Currency $to, SimpleXMLElement $xml)
 {
     $item = $xml->xpath('/rss/channel/item[title="' . $to->getCode() . '/' . $from->getCode() . '"]');
     $item = is_array($item) ? current($item) : false;
     if ($item instanceof SimpleXMLElement) {
         preg_match('/= ([0-9]+\\.?[0-9]*) /', $item->description, $matches);
     }
     return isset($matches[1]) ? $matches[1] : false;
 }
Пример #9
0
 /**
  * @param Money $otherMoney
  *
  * @throws CurrencyMismatchException
  */
 private function compareCurrency(Money $otherMoney)
 {
     if ($this->currency->compareTo($otherMoney->getCurrency()) !== 0) {
         throw CurrencyMismatchException::currenciesNotMatching($this->currency->getCode(), $otherMoney->getCurrency()->getCode());
     }
 }
Пример #10
0
 /**
  * Whether this and another currency are the same. Uses only the code to determine identity.
  *
  * @param Currency $currency the currency to compare to
  *
  * @return boolean true if the currency codes match, false otherwise
  */
 public function equals(Currency $currency)
 {
     return $this->code == $currency->getCode();
 }
 public function findOneByFromCodeAndToCode(Currency $from, Currency $to)
 {
     return Doctrine_Query::create()->from('CurrencyRate')->where('from_code = ?', $from->getCode())->andWhere('to_code = ?', $to->getCode())->fetchOne();
 }
Пример #12
0
 /**
  * @param Currency $otherCurrency
  *
  * @return int
  */
 public function compareTo(Currency $otherCurrency)
 {
     return strcmp($this->code, $otherCurrency->getCode());
 }
Пример #13
0
 /**
  * @param Currency $currency
  *
  * @return bool
  */
 public function isEqualTo(Currency $currency)
 {
     return $this->getCode() === $currency->getCode();
 }
Пример #14
0
 /**
  * @param Currency $currency this field is required for partial refunds. 
  *      Do not use this field for full refunds.
  */
 public function setCurrency(Currency $currency)
 {
     $this->collection->setValue('CURRENCYCODE', $currency->getCode());
 }