File indexing completed on 2024-04-28 06:00:00

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Currency
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * include needed classes
0024  */
0025 // require_once 'Zend/Locale.php';
0026 // require_once 'Zend/Locale/Data.php';
0027 // require_once 'Zend/Locale/Format.php';
0028 
0029 /**
0030  * Class for handling currency notations
0031  *
0032  * @category  Zend
0033  * @package   Zend_Currency
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Currency
0038 {
0039     // Constants for defining what currency symbol should be displayed
0040     const NO_SYMBOL     = 1;
0041     const USE_SYMBOL    = 2;
0042     const USE_SHORTNAME = 3;
0043     const USE_NAME      = 4;
0044 
0045     // Constants for defining the position of the currencysign
0046     const STANDARD = 8;
0047     const RIGHT    = 16;
0048     const LEFT     = 32;
0049 
0050     /**
0051      * Options array
0052      *
0053      * The following options are available
0054      * 'position'  => Position for the currency sign
0055      * 'script'    => Script for the output
0056      * 'format'    => Locale for numeric output
0057      * 'display'   => Currency detail to show
0058      * 'precision' => Precision for the currency
0059      * 'name'      => Name for this currency
0060      * 'currency'  => 3 lettered international abbreviation
0061      * 'symbol'    => Currency symbol
0062      * 'locale'    => Locale for this currency
0063      * 'value'     => Money value
0064      * 'service'   => Exchange service to use
0065      *
0066      * @var array
0067      * @see Zend_Locale
0068      */
0069     protected $_options = array(
0070         'position'  => self::STANDARD,
0071         'script'    => null,
0072         'format'    => null,
0073         'display'   => self::NO_SYMBOL,
0074         'precision' => 2,
0075         'name'      => null,
0076         'currency'  => null,
0077         'symbol'    => null,
0078         'locale'    => null,
0079         'value'     => 0,
0080         'service'   => null,
0081         'tag'       => 'Zend_Locale'
0082     );
0083 
0084     /**
0085      * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
0086      *
0087      * @param  string|array       $options OPTIONAL Options array or currency short name
0088      *                                              when string is given
0089      * @param  string|Zend_Locale $locale  OPTIONAL locale name
0090      * @throws Zend_Currency_Exception When currency is invalid
0091      */
0092     public function __construct($options = null, $locale = null)
0093     {
0094         $calloptions = $options;
0095         if (is_array($options) && isset($options['display'])) {
0096             $this->_options['display'] = $options['display'];
0097         }
0098 
0099         if (is_array($options)) {
0100             $this->setLocale($locale);
0101             $this->setFormat($options);
0102         } else if (Zend_Locale::isLocale($options, false, false)) {
0103             $this->setLocale($options);
0104             $options = $locale;
0105         } else {
0106             $this->setLocale($locale);
0107         }
0108 
0109         // Get currency details
0110         if (!isset($this->_options['currency']) || !is_array($options)) {
0111             $this->_options['currency'] = self::getShortName($options, $this->_options['locale']);
0112         }
0113 
0114         if (!isset($this->_options['name']) || !is_array($options)) {
0115             $this->_options['name']     = self::getName($options, $this->_options['locale']);
0116         }
0117 
0118         if (!isset($this->_options['symbol']) || !is_array($options)) {
0119             $this->_options['symbol']   = self::getSymbol($options, $this->_options['locale']);
0120         }
0121 
0122         if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
0123             // require_once 'Zend/Currency/Exception.php';
0124             throw new Zend_Currency_Exception("Currency '$options' not found");
0125         }
0126 
0127         // Get the format
0128         if ((is_array($calloptions) && !isset($calloptions['display']))
0129                 || (!is_array($calloptions) && $this->_options['display'] == self::NO_SYMBOL)) {
0130             if (!empty($this->_options['symbol'])) {
0131                 $this->_options['display'] = self::USE_SYMBOL;
0132             } else if (!empty($this->_options['currency'])) {
0133                 $this->_options['display'] = self::USE_SHORTNAME;
0134             }
0135         }
0136     }
0137 
0138     /**
0139      * Returns a localized currency string
0140      *
0141      * @param  integer|float $value   OPTIONAL Currency value
0142      * @param  array         $options OPTIONAL options to set temporary
0143      * @throws Zend_Currency_Exception When the value is not a number
0144      * @return string
0145      */
0146     public function toCurrency($value = null, array $options = array())
0147     {
0148         if ($value === null) {
0149             if (is_array($options) && isset($options['value'])) {
0150                 $value = $options['value'];
0151             } else {
0152                 $value = $this->_options['value'];
0153             }
0154         }
0155 
0156         if (is_array($value)) {
0157             $options += $value;
0158             if (isset($options['value'])) {
0159                 $value = $options['value'];
0160             }
0161         }
0162 
0163         // Validate the passed number
0164         if (!(isset($value)) or (is_numeric($value) === false)) {
0165             // require_once 'Zend/Currency/Exception.php';
0166             throw new Zend_Currency_Exception("Value '$value' has to be numeric");
0167         }
0168 
0169         if (isset($options['currency'])) {
0170             if (!isset($options['locale'])) {
0171                 $options['locale'] = $this->_options['locale'];
0172             }
0173 
0174             $options['currency'] = self::getShortName($options['currency'], $options['locale']);
0175             $options['name']     = self::getName($options['currency'], $options['locale']);
0176             $options['symbol']   = self::getSymbol($options['currency'], $options['locale']);
0177         }
0178 
0179         $options = $this->_checkOptions($options) + $this->_options;
0180 
0181         // Format the number
0182         $format = $options['format'];
0183         $locale = $options['locale'];
0184         if (empty($format)) {
0185             $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
0186         } else if (Zend_Locale::isLocale($format, true, false)) {
0187             $locale = $format;
0188             $format = Zend_Locale_Data::getContent($format, 'currencynumber');
0189         }
0190 
0191         $original = $value;
0192         $value    = Zend_Locale_Format::toNumber($value, array('locale'        => $locale,
0193                                                                'number_format' => $format,
0194                                                                'precision'     => $options['precision']));
0195 
0196         if ($options['position'] !== self::STANDARD) {
0197             $value = str_replace('¤', '', $value);
0198             $space = '';
0199             if (iconv_strpos($value, ' ') !== false) {
0200                 $value = str_replace(' ', '', $value);
0201                 $space = ' ';
0202             }
0203 
0204             if ($options['position'] == self::LEFT) {
0205                 $value = '¤' . $space . $value;
0206             } else {
0207                 $value = $value . $space . '¤';
0208             }
0209         }
0210 
0211         // Localize the number digits
0212         if (empty($options['script']) === false) {
0213             $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
0214         }
0215 
0216         // Get the sign to be placed next to the number
0217         if (is_numeric($options['display']) === false) {
0218             $sign = $options['display'];
0219         } else {
0220             switch($options['display']) {
0221                 case self::USE_SYMBOL:
0222                     $sign = $this->_extractPattern($options['symbol'], $original);
0223                     break;
0224 
0225                 case self::USE_SHORTNAME:
0226                     $sign = $options['currency'];
0227                     break;
0228 
0229                 case self::USE_NAME:
0230                     $sign = $options['name'];
0231                     break;
0232 
0233                 default:
0234                     $sign = '';
0235                     $value = str_replace(' ', '', $value);
0236                     break;
0237             }
0238         }
0239 
0240         $value = str_replace('¤', $sign, $value);
0241         return $value;
0242     }
0243 
0244     /**
0245      * Internal method to extract the currency pattern
0246      * when a choice is given based on the given value
0247      *
0248      * @param  string $pattern
0249      * @param  float|integer $value
0250      * @return string
0251      */
0252     private function _extractPattern($pattern, $value)
0253     {
0254         if (strpos($pattern, '|') === false) {
0255             return $pattern;
0256         }
0257 
0258         $patterns = explode('|', $pattern);
0259         $token    = $pattern;
0260         $value    = trim(str_replace('¤', '', $value));
0261         krsort($patterns);
0262         foreach($patterns as $content) {
0263             if (strpos($content, '<') !== false) {
0264                 $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
0265                 $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
0266                 if ($check < $value) {
0267                     return $token;
0268                 }
0269             } else {
0270                 $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
0271                 $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
0272                 if ($check <= $value) {
0273                     return $token;
0274                 }
0275             }
0276 
0277         }
0278 
0279         return $token;
0280     }
0281 
0282     /**
0283      * Sets the formating options of the localized currency string
0284      * If no parameter is passed, the standard setting of the
0285      * actual set locale will be used
0286      *
0287      * @param  array $options (Optional) Options to set
0288      * @return Zend_Currency
0289      */
0290     public function setFormat(array $options = array())
0291     {
0292         $this->_options = $this->_checkOptions($options) + $this->_options;
0293         return $this;
0294     }
0295 
0296     /**
0297      * Internal function for checking static given locale parameter
0298      *
0299      * @param  string             $currency (Optional) Currency name
0300      * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
0301      * @throws Zend_Currency_Exception When locale contains no region
0302      * @return string The extracted locale representation as string
0303      */
0304     private function _checkParams($currency = null, $locale = null)
0305     {
0306         // Manage the params
0307         if ((empty($locale)) and (!empty($currency)) and
0308             (Zend_Locale::isLocale($currency, true, false))) {
0309             $locale   = $currency;
0310             $currency = null;
0311         }
0312 
0313         // Validate the locale and get the country short name
0314         $country = null;
0315         if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
0316             $country = substr($locale, (strpos($locale, '_') + 1));
0317         } else {
0318             // require_once 'Zend/Currency/Exception.php';
0319             throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
0320         }
0321 
0322         // Get the available currencies for this country
0323         $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
0324         if ((empty($currency) === false) and (empty($data) === false)) {
0325             $abbreviation = $currency;
0326         } else {
0327             $abbreviation = $data;
0328         }
0329 
0330         return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
0331     }
0332 
0333     /**
0334      * Returns the actual or details of other currency symbols,
0335      * when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
0336      *
0337      * @param  string             $currency (Optional) Currency name
0338      * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
0339      * @return string
0340      */
0341     public function getSymbol($currency = null, $locale = null)
0342     {
0343         if (($currency === null) and ($locale === null)) {
0344             return $this->_options['symbol'];
0345         }
0346 
0347         $params = self::_checkParams($currency, $locale);
0348 
0349         // Get the symbol
0350         $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
0351         if (empty($symbol) === true) {
0352             $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
0353         }
0354 
0355         if (empty($symbol) === true) {
0356             return null;
0357         }
0358 
0359         return $symbol;
0360     }
0361 
0362     /**
0363      * Returns the actual or details of other currency shortnames
0364      *
0365      * @param  string             $currency OPTIONAL Currency's name
0366      * @param  string|Zend_Locale $locale   OPTIONAL The locale
0367      * @return string
0368      */
0369     public function getShortName($currency = null, $locale = null)
0370     {
0371         if (($currency === null) and ($locale === null)) {
0372             return $this->_options['currency'];
0373         }
0374 
0375         $params = self::_checkParams($currency, $locale);
0376 
0377         // Get the shortname
0378         if (empty($params['currency']) === true) {
0379             return $params['name'];
0380         }
0381 
0382         $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
0383         if (empty($list) === true) {
0384             $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
0385             if (empty($list) === false) {
0386                 $list = $params['currency'];
0387             }
0388         }
0389 
0390         if (empty($list) === true) {
0391             return null;
0392         }
0393 
0394         return $list;
0395     }
0396 
0397     /**
0398      * Returns the actual or details of other currency names
0399      *
0400      * @param  string             $currency (Optional) Currency's short name
0401      * @param  string|Zend_Locale $locale   (Optional) The locale
0402      * @return string
0403      */
0404     public function getName($currency = null, $locale = null)
0405     {
0406         if (($currency === null) and ($locale === null)) {
0407             return $this->_options['name'];
0408         }
0409 
0410         $params = self::_checkParams($currency, $locale);
0411 
0412         // Get the name
0413         $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
0414         if (empty($name) === true) {
0415             $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
0416         }
0417 
0418         if (empty($name) === true) {
0419             return null;
0420         }
0421 
0422         return $name;
0423     }
0424 
0425     /**
0426      * Returns a list of regions where this currency is or was known
0427      *
0428      * @param  string $currency OPTIONAL Currency's short name
0429      * @throws Zend_Currency_Exception When no currency was defined
0430      * @return array List of regions
0431      */
0432     public function getRegionList($currency = null)
0433     {
0434         if ($currency === null) {
0435             $currency = $this->_options['currency'];
0436         }
0437 
0438         if (empty($currency) === true) {
0439             // require_once 'Zend/Currency/Exception.php';
0440             throw new Zend_Currency_Exception('No currency defined');
0441         }
0442 
0443         $data = Zend_Locale_Data::getContent($this->_options['locale'], 'regiontocurrency', $currency);
0444 
0445         $result = explode(' ', $data);
0446         return $result;
0447     }
0448 
0449     /**
0450      * Returns a list of currencies which are used in this region
0451      * a region name should be 2 charachters only (f.e. EG, DE, US)
0452      * If no region is given, the actual region is used
0453      *
0454      * @param  string $region OPTIONAL Region to return the currencies for
0455      * @return array List of currencies
0456      */
0457     public function getCurrencyList($region = null)
0458     {
0459         if (empty($region) === true) {
0460             if (strlen($this->_options['locale']) > 4) {
0461                 $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
0462             }
0463         }
0464 
0465         $data = Zend_Locale_Data::getContent($this->_options['locale'], 'currencytoregion', $region);
0466 
0467         $result = explode(' ', $data);
0468         return $result;
0469     }
0470 
0471     /**
0472      * Returns the actual currency name
0473      *
0474      * @return string
0475      */
0476     public function toString()
0477     {
0478         return $this->toCurrency();
0479     }
0480 
0481     /**
0482      * Returns the currency name
0483      *
0484      * @return string
0485      */
0486     public function __toString()
0487     {
0488         return $this->toString();
0489     }
0490 
0491     /**
0492      * Returns the set cache
0493      *
0494      * @return Zend_Cache_Core The set cache
0495      */
0496     public static function getCache()
0497     {
0498         return Zend_Locale_Data::getCache();
0499     }
0500 
0501     /**
0502      * Sets a cache for Zend_Currency
0503      *
0504      * @param  Zend_Cache_Core $cache Cache to set
0505      * @return void
0506      */
0507     public static function setCache(Zend_Cache_Core $cache)
0508     {
0509         Zend_Locale_Data::setCache($cache);
0510     }
0511 
0512     /**
0513      * Returns true when a cache is set
0514      *
0515      * @return boolean
0516      */
0517     public static function hasCache()
0518     {
0519         return Zend_Locale_Data::hasCache();
0520     }
0521 
0522     /**
0523      * Removes any set cache
0524      *
0525      * @return void
0526      */
0527     public static function removeCache()
0528     {
0529         Zend_Locale_Data::removeCache();
0530     }
0531 
0532     /**
0533      * Clears all set cache data
0534      *
0535      * @param string $tag Tag to clear when the default tag name is not used
0536      * @return void
0537      */
0538     public static function clearCache($tag = null)
0539     {
0540         Zend_Locale_Data::clearCache($tag);
0541     }
0542 
0543     /**
0544      * Sets a new locale for data retreivement
0545      * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
0546      * 'xx_YY' will be set to 'root' because 'xx' does not exist
0547      *
0548      * @param  string|Zend_Locale $locale (Optional) Locale for parsing input
0549      * @throws Zend_Currency_Exception When the given locale does not exist
0550      * @return Zend_Currency Provides fluent interface
0551      */
0552     public function setLocale($locale = null)
0553     {
0554         // require_once 'Zend/Locale.php';
0555         try {
0556             $locale = Zend_Locale::findLocale($locale);
0557             if (strlen($locale) > 4) {
0558                 $this->_options['locale'] = $locale;
0559             } else {
0560                 // require_once 'Zend/Currency/Exception.php';
0561                 throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
0562             }
0563         } catch (Zend_Locale_Exception $e) {
0564             // require_once 'Zend/Currency/Exception.php';
0565             throw new Zend_Currency_Exception($e->getMessage());
0566         }
0567 
0568         // Get currency details
0569         $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
0570         $this->_options['name']     = $this->getName(null, $this->_options['locale']);
0571         $this->_options['symbol']   = $this->getSymbol(null, $this->_options['locale']);
0572 
0573         return $this;
0574     }
0575 
0576     /**
0577      * Returns the actual set locale
0578      *
0579      * @return string
0580      */
0581     public function getLocale()
0582     {
0583         return $this->_options['locale'];
0584     }
0585 
0586     /**
0587      * Returns the value
0588      *
0589      * @return float
0590      */
0591     public function getValue()
0592     {
0593         return $this->_options['value'];
0594     }
0595 
0596     /**
0597      * Adds a currency
0598      *
0599      * @param float|integer|Zend_Currency $value    Add this value to currency
0600      * @param string|Zend_Currency        $currency The currency to add
0601      * @return Zend_Currency
0602      */
0603     public function setValue($value, $currency = null)
0604     {
0605         $this->_options['value'] = $this->_exchangeCurrency($value, $currency);
0606         return $this;
0607     }
0608 
0609     /**
0610      * Adds a currency
0611      *
0612      * @param float|integer|Zend_Currency $value    Add this value to currency
0613      * @param string|Zend_Currency        $currency The currency to add
0614      * @return Zend_Currency
0615      */
0616     public function add($value, $currency = null)
0617     {
0618         $value = $this->_exchangeCurrency($value, $currency);
0619         $this->_options['value'] += (float) $value;
0620         return $this;
0621     }
0622 
0623     /**
0624      * Substracts a currency
0625      *
0626      * @param float|integer|Zend_Currency $value    Substracts this value from currency
0627      * @param string|Zend_Currency        $currency The currency to substract
0628      * @return Zend_Currency
0629      */
0630     public function sub($value, $currency = null)
0631     {
0632         $value = $this->_exchangeCurrency($value, $currency);
0633         $this->_options['value'] -= (float) $value;
0634         return $this;
0635     }
0636 
0637     /**
0638      * Divides a currency
0639      *
0640      * @param float|integer|Zend_Currency $value    Divides this value from currency
0641      * @param string|Zend_Currency        $currency The currency to divide
0642      * @return Zend_Currency
0643      */
0644     public function div($value, $currency = null)
0645     {
0646         $value = $this->_exchangeCurrency($value, $currency);
0647         $this->_options['value'] /= (float) $value;
0648         return $this;
0649     }
0650 
0651     /**
0652      * Multiplies a currency
0653      *
0654      * @param float|integer|Zend_Currency $value    Multiplies this value from currency
0655      * @param string|Zend_Currency        $currency The currency to multiply
0656      * @return Zend_Currency
0657      */
0658     public function mul($value, $currency = null)
0659     {
0660         $value = $this->_exchangeCurrency($value, $currency);
0661         $this->_options['value'] *= (float) $value;
0662         return $this;
0663     }
0664 
0665     /**
0666      * Calculates the modulo from a currency
0667      *
0668      * @param float|integer|Zend_Currency $value    Calculate modulo from this value
0669      * @param string|Zend_Currency        $currency The currency to calculate the modulo
0670      * @return Zend_Currency
0671      */
0672     public function mod($value, $currency = null)
0673     {
0674         $value = $this->_exchangeCurrency($value, $currency);
0675         $this->_options['value'] %= (float) $value;
0676         return $this;
0677     }
0678 
0679     /**
0680      * Compares two currencies
0681      *
0682      * @param float|integer|Zend_Currency $value    Compares the currency with this value
0683      * @param string|Zend_Currency        $currency The currency to compare this value from
0684      * @return Zend_Currency
0685      */
0686     public function compare($value, $currency = null)
0687     {
0688         $value = $this->_exchangeCurrency($value, $currency);
0689         $value = $this->_options['value'] - $value;
0690         if ($value < 0) {
0691             return -1;
0692         } else if ($value > 0) {
0693             return 1;
0694         }
0695 
0696         return 0;
0697     }
0698 
0699     /**
0700      * Returns true when the two currencies are equal
0701      *
0702      * @param float|integer|Zend_Currency $value    Compares the currency with this value
0703      * @param string|Zend_Currency        $currency The currency to compare this value from
0704      * @return boolean
0705      */
0706     public function equals($value, $currency = null)
0707     {
0708         $value = $this->_exchangeCurrency($value, $currency);
0709         if ($this->_options['value'] == $value) {
0710             return true;
0711         }
0712 
0713         return false;
0714     }
0715 
0716     /**
0717      * Returns true when the currency is more than the given value
0718      *
0719      * @param float|integer|Zend_Currency $value    Compares the currency with this value
0720      * @param string|Zend_Currency        $currency The currency to compare this value from
0721      * @return boolean
0722      */
0723     public function isMore($value, $currency = null)
0724     {
0725         $value = $this->_exchangeCurrency($value, $currency);
0726         if ($this->_options['value'] > $value) {
0727             return true;
0728         }
0729 
0730         return false;
0731     }
0732 
0733     /**
0734      * Returns true when the currency is less than the given value
0735      *
0736      * @param float|integer|Zend_Currency $value    Compares the currency with this value
0737      * @param string|Zend_Currency        $currency The currency to compare this value from
0738      * @return boolean
0739      */
0740     public function isLess($value, $currency = null)
0741     {
0742         $value = $this->_exchangeCurrency($value, $currency);
0743         if ($this->_options['value'] < $value) {
0744             return true;
0745         }
0746 
0747         return false;
0748 
0749     }
0750 
0751     /**
0752      * Internal method which calculates the exchanges currency
0753      *
0754      * @param float|integer|Zend_Currency $value    Compares the currency with this value
0755      * @param string|Zend_Currency        $currency The currency to compare this value from
0756      * @return unknown
0757      */
0758     protected function _exchangeCurrency($value, $currency)
0759     {
0760         if ($value instanceof Zend_Currency) {
0761             $currency = $value->getShortName();
0762             $value    = $value->getValue();
0763         } else {
0764             $currency = $this->getShortName($currency, $this->getLocale());
0765         }
0766 
0767         $rate = 1;
0768         if ($currency !== $this->getShortName()) {
0769             $service = $this->getService();
0770             if (!($service instanceof Zend_Currency_CurrencyInterface)) {
0771                 // require_once 'Zend/Currency/Exception.php';
0772                 throw new Zend_Currency_Exception('No exchange service applied');
0773             }
0774 
0775             $rate = $service->getRate($currency, $this->getShortName());
0776         }
0777 
0778         $value *= $rate;
0779         return $value;
0780     }
0781 
0782     /**
0783      * Returns the set service class
0784      *
0785      * @return Zend_Service
0786      */
0787     public function getService()
0788     {
0789         return $this->_options['service'];
0790     }
0791 
0792     /**
0793      * Sets a new exchange service
0794      *
0795      * @param string|Zend_Currency_CurrencyInterface $service Service class
0796      * @return Zend_Currency
0797      */
0798     public function setService($service)
0799     {
0800         if (is_string($service)) {
0801             // require_once 'Zend/Loader.php';
0802             if (!class_exists($service)) {
0803                 $file = str_replace('_', DIRECTORY_SEPARATOR, $service) . '.php';
0804                 if (Zend_Loader::isReadable($file)) {
0805                     Zend_Loader::loadClass($service);
0806                 }
0807             }
0808 
0809             $service = new $service;
0810         }
0811 
0812         if (!($service instanceof Zend_Currency_CurrencyInterface)) {
0813             // require_once 'Zend/Currency/Exception.php';
0814             throw new Zend_Currency_Exception('A currency service must implement Zend_Currency_CurrencyInterface');
0815         }
0816 
0817         $this->_options['service'] = $service;
0818         return $this;
0819     }
0820 
0821     /**
0822      * Internal method for checking the options array
0823      *
0824      * @param  array $options Options to check
0825      * @throws Zend_Currency_Exception On unknown position
0826      * @throws Zend_Currency_Exception On unknown locale
0827      * @throws Zend_Currency_Exception On unknown display
0828      * @throws Zend_Currency_Exception On precision not between -1 and 30
0829      * @throws Zend_Currency_Exception On problem with script conversion
0830      * @throws Zend_Currency_Exception On unknown options
0831      * @return array
0832      */
0833     protected function _checkOptions(array $options = array())
0834     {
0835         if (count($options) === 0) {
0836             return $this->_options;
0837         }
0838 
0839         foreach ($options as $name => $value) {
0840             $name = strtolower($name);
0841             if ($name !== 'format') {
0842                 if (gettype($value) === 'string') {
0843                     $value = strtolower($value);
0844                 }
0845             }
0846 
0847             switch($name) {
0848                 case 'position':
0849                     if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
0850                         // require_once 'Zend/Currency/Exception.php';
0851                         throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
0852                     }
0853 
0854                     break;
0855 
0856                 case 'format':
0857                     if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
0858                         if (!is_string($value) || (strpos($value, '0') === false)) {
0859                             // require_once 'Zend/Currency/Exception.php';
0860                             throw new Zend_Currency_Exception("'" .
0861                                 ((gettype($value) === 'object') ? get_class($value) : $value)
0862                                 . "' is no format token");
0863                         }
0864                     }
0865                     break;
0866 
0867                 case 'display':
0868                     if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
0869                         ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
0870                         // require_once 'Zend/Currency/Exception.php';
0871                         throw new Zend_Currency_Exception("Unknown display '$value'");
0872                     }
0873                     break;
0874 
0875                 case 'precision':
0876                     if ($value === null) {
0877                         $value = -1;
0878                     }
0879 
0880                     if (($value < -1) or ($value > 30)) {
0881                         // require_once 'Zend/Currency/Exception.php';
0882                         throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
0883                     }
0884                     break;
0885 
0886                 case 'script':
0887                     try {
0888                         Zend_Locale_Format::convertNumerals(0, $options['script']);
0889                     } catch (Zend_Locale_Exception $e) {
0890                         // require_once 'Zend/Currency/Exception.php';
0891                         throw new Zend_Currency_Exception($e->getMessage());
0892                     }
0893                     break;
0894 
0895                 default:
0896                     break;
0897             }
0898         }
0899 
0900         return $options;
0901     }
0902 }