00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Core_Model_Locale_Currency extends Zend_Currency
00033 {
00034 const XML_PATH_TRIM_CURRENCY_SIGN = 'currency/options/trim_sign';
00035 const US_LOCALE = 'en_US';
00036 protected $_locale;
00037
00038
00039
00040
00041
00042
00043
00044
00045 public function __construct($currency = null, $locale = null)
00046 {
00047 parent::__construct($currency, $locale);
00048 $this->_options['symbol_choice'] = self::getSymbolChoice($currency, $this->_locale);
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058 public function getSymbolChoice($currency = null, $locale = null)
00059 {
00060 if (($currency === null) and ($locale === null)) {
00061 return $this->_options['symbol_choice'];
00062 }
00063
00064 $params = self::_checkParams($currency, $locale);
00065
00066
00067 $symbolChoice = Zend_Locale_Data::getContent($params['locale'], 'currencysymbolchoice', $params['currency']);
00068 if (empty($symbolChoice) === true) {
00069 $symbolChoice = Zend_Locale_Data::getContent($params['locale'], 'currencysymbolchoice', $params['name']);
00070 }
00071 if (empty($symbolChoice) === true) {
00072 return null;
00073 }
00074 return $symbolChoice;
00075 }
00076
00077 public function setLocale($locale = null)
00078 {
00079 $this->_locale = $locale;
00080 parent::setLocale($locale);
00081 return $this;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 protected function _concatSign($value, $sign, $options)
00093 {
00094 $trimSign = $this->getStore()->getConfig(self::XML_PATH_TRIM_CURRENCY_SIGN);
00095 if (is_null($trimSign) && $this->_locale && $this->_locale == self::US_LOCALE) {
00096 $trimSign = true;
00097 }
00098 if ($trimSign) {
00099 $sign = trim($sign);
00100 }
00101
00102
00103 if ($options['position'] === self::RIGHT) {
00104 $result = $value . $sign;
00105 } else if ($options['position'] === self::LEFT) {
00106
00107 if (0 === strpos($value, '-', 0)) {
00108 $result = '-' . ltrim($sign) . substr($value, 1);
00109 } else {
00110 $result = $sign . $value;
00111 }
00112 }
00113 return $result;
00114 }
00115
00116
00117
00118
00119
00120
00121 public function getStore()
00122 {
00123 return Mage::app()->getStore();
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 private function checkOptions(array $options = array())
00134 {
00135 if (count($options) == 0) {
00136 return $this->_options;
00137 }
00138 foreach($options as $name => $value) {
00139 $name = strtolower($name);
00140 if ($name !== 'format') {
00141 if (gettype($value) === 'string') {
00142 $value = strtolower($value);
00143 }
00144 }
00145 if (array_key_exists($name, $this->_options)) {
00146 switch($name) {
00147 case 'position' :
00148 if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
00149 #require_once 'Zend/Currency/Exception.php';
00150 throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
00151 }
00152 if ($value === self::STANDARD) {
00153 $options['position'] = $this->_updateFormat();
00154 }
00155 break;
00156 case 'format' :
00157 if (!empty($value) && (!Zend_Locale::isLocale($value))) {
00158 #require_once 'Zend/Currency/Exception.php';
00159 throw new Zend_Currency_Exception("'" .
00160 (gettype($value) === 'object' ? get_class($value) : $value)
00161 . "' is not a known locale.");
00162 }
00163 break;
00164 case 'display' :
00165 if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
00166 ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
00167 #require_once 'Zend/Currency/Exception.php';
00168 throw new Zend_Currency_Exception("Unknown display '$value'");
00169 }
00170 break;
00171 case 'precision' :
00172 if ($value === NULL) {
00173 $value = -1;
00174 }
00175 if (($value < -1) || ($value > 30)) {
00176 #require_once 'Zend/Currency/Exception.php';
00177 throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
00178 }
00179 break;
00180 case 'script' :
00181 try {
00182 Zend_Locale_Format::convertNumerals(0,$options['script']);
00183 } catch (Zend_Locale_Exception $e) {
00184 #require_once 'Zend/Currency/Exception.php';
00185 throw new Zend_Currency_Exception($e->getMessage());
00186 }
00187 break;
00188 }
00189 }
00190 else {
00191 #require_once 'Zend/Currency/Exception.php';
00192 throw new Zend_Currency_Exception("Unknown option: '$name' = '$value'");
00193 }
00194 }
00195 return $options;
00196 }
00197 }