00001 <?php
00025 require_once 'Zend/Locale.php';
00026 require_once 'Zend/Locale/Data.php';
00027 require_once 'Zend/Locale/Format.php';
00028
00037 class Zend_Currency
00038 {
00039
00040 const NO_SYMBOL = 1;
00041 const USE_SYMBOL = 2;
00042 const USE_SHORTNAME = 3;
00043 const USE_NAME = 4;
00044
00045
00046 const STANDARD = 8;
00047 const RIGHT = 16;
00048 const LEFT = 32;
00049
00055 private $_locale = null;
00056
00073 protected $_options = array(
00074 'position' => self::STANDARD,
00075 'script' => null,
00076 'format' => null,
00077 'display' => self::NO_SYMBOL,
00078 'precision' => 2,
00079 'name' => null,
00080 'currency' => null,
00081 'symbol' => null
00082 );
00083
00091 public function __construct($currency = null, $locale = null)
00092 {
00093 if (Zend_Locale::isLocale($currency, true, false)) {
00094 $temp = $locale;
00095 $locale = $currency;
00096 $currency = $temp;
00097 }
00098
00099 $this->setLocale($locale);
00100
00101
00102 $this->_options['currency'] = self::getShortName($currency, $this->_locale);
00103 $this->_options['name'] = self::getName($currency, $this->_locale);
00104 $this->_options['symbol'] = self::getSymbol($currency, $this->_locale);
00105
00106 if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
00107 require_once 'Zend/Currency/Exception.php';
00108 throw new Zend_Currency_Exception("Currency '$currency' not found");
00109 }
00110
00111
00112 $this->_options['display'] = self::NO_SYMBOL;
00113 if (empty($this->_options['symbol']) === false) {
00114 $this->_options['display'] = self::USE_SYMBOL;
00115 } else if (empty($this->_options['currency']) === false) {
00116 $this->_options['display'] = self::USE_SHORTNAME;
00117 }
00118 }
00119
00128 public function toCurrency($value, array $options = array())
00129 {
00130
00131 if ((isset($value) === false) or (is_numeric($value) === false)) {
00132 require_once 'Zend/Currency/Exception.php';
00133 throw new Zend_Currency_Exception("Value '$value' has to be numeric");
00134 }
00135
00136 $options = $this->_checkOptions($options) + $this->_options;
00137
00138
00139 $format = $options['format'];
00140 $locale = $this->_locale;
00141 if (empty($format)) {
00142 $format = Zend_Locale_Data::getContent($this->_locale, 'currencynumber');
00143 } else if (Zend_Locale::isLocale($format, true, false)) {
00144 $locale = $format;
00145 $format = Zend_Locale_Data::getContent($format, 'currencynumber');
00146 }
00147
00148 $symbols = Zend_Locale_Data::getList($locale, 'symbols');
00149 $original = $value;
00150 $value = Zend_Locale_Format::toNumber($value, array('locale' => $locale,
00151 'number_format' => $format,
00152 'precision' => $options['precision']));
00153
00154 if ($options['position'] !== self::STANDARD) {
00155 $value = str_replace('¤', '', $value);
00156 $space = '';
00157 if (iconv_strpos($value, ' ') !== false) {
00158 $value = str_replace(' ', '', $value);
00159 $space = ' ';
00160 }
00161
00162 if ($options['position'] == self::LEFT) {
00163 $value = '¤' . $space . $value;
00164 } else {
00165 $value = $value . $space . '¤';
00166 }
00167 }
00168
00169
00170 if (empty($options['script']) === false) {
00171 $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
00172 }
00173
00174
00175 if (is_numeric($options['display']) === false) {
00176 $sign = $options['display'];
00177 } else {
00178 switch($options['display']) {
00179 case self::USE_SYMBOL:
00180 $sign = $this->_extractPattern($options['symbol'], $original);
00181 break;
00182
00183 case self::USE_SHORTNAME:
00184 $sign = $options['currency'];
00185 break;
00186
00187 case self::USE_NAME:
00188 $sign = $options['name'];
00189 break;
00190
00191 default:
00192 $sign = '';
00193 $value = str_replace(' ', '', $value);
00194 break;
00195 }
00196 }
00197
00198 $value = str_replace('¤', $sign, $value);
00199 return $value;
00200 }
00201
00210 private function _extractPattern($pattern, $value)
00211 {
00212 if (strpos($pattern, '|') === false) {
00213 return $pattern;
00214 }
00215
00216 $patterns = explode('|', $pattern);
00217 $token = $pattern;
00218 $value = trim(str_replace('¤', '', $value));
00219 krsort($patterns);
00220 foreach($patterns as $content) {
00221 if (strpos($content, '<') !== false) {
00222 $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
00223 $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
00224 if ($check < $value) {
00225 return $token;
00226 }
00227 } else {
00228 $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
00229 $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
00230 if ($check <= $value) {
00231 return $token;
00232 }
00233 }
00234
00235 }
00236
00237 return $token;
00238 }
00239
00248 public function setFormat(array $options = array())
00249 {
00250 $this->_options = $this->_checkOptions($options) + $this->_options;
00251 return $this;
00252 }
00253
00262 private function _checkParams($currency = null, $locale = null)
00263 {
00264
00265 if ((empty($locale)) and (!empty($currency)) and
00266 (Zend_Locale::isLocale($currency, true, false))) {
00267 $locale = $currency;
00268 $currency = null;
00269 }
00270
00271
00272 $country = null;
00273 if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
00274 $country = substr($locale, (strpos($locale, '_') + 1));
00275 } else {
00276 require_once 'Zend/Currency/Exception.php';
00277 throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
00278 }
00279
00280
00281 $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
00282 if ((empty($currency) === false) and (empty($data) === false)) {
00283 $abbreviation = $currency;
00284 } else {
00285 $abbreviation = $data;
00286 }
00287
00288 return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
00289 }
00290
00299 public function getSymbol($currency = null, $locale = null)
00300 {
00301 if (($currency === null) and ($locale === null)) {
00302 return $this->_options['symbol'];
00303 }
00304
00305 $params = self::_checkParams($currency, $locale);
00306
00307
00308 $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
00309 if (empty($symbol) === true) {
00310 $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
00311 }
00312
00313 if (empty($symbol) === true) {
00314 return null;
00315 }
00316
00317 return $symbol;
00318 }
00319
00327 public function getShortName($currency = null, $locale = null)
00328 {
00329 if (($currency === null) and ($locale === null)) {
00330 return $this->_options['currency'];
00331 }
00332
00333 $params = self::_checkParams($currency, $locale);
00334
00335
00336 if (empty($params['currency']) === true) {
00337 return $params['name'];
00338 }
00339
00340 $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
00341 if (empty($list) === true) {
00342 $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
00343 if (empty($list) === false) {
00344 $list = $params['currency'];
00345 }
00346 }
00347
00348 if (empty($list) === true) {
00349 return null;
00350 }
00351
00352 return $list;
00353 }
00354
00362 public function getName($currency = null, $locale = null)
00363 {
00364 if (($currency === null) and ($locale === null)) {
00365 return $this->_options['name'];
00366 }
00367
00368 $params = self::_checkParams($currency, $locale);
00369
00370
00371 $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
00372 if (empty($name) === true) {
00373 $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
00374 }
00375
00376 if (empty($name) === true) {
00377 return null;
00378 }
00379
00380 return $name;
00381 }
00382
00390 public function getRegionList($currency = null)
00391 {
00392 if ($currency === null) {
00393 $currency = $this->_options['currency'];
00394 }
00395
00396 if (empty($currency) === true) {
00397 require_once 'Zend/Currency/Exception.php';
00398 throw new Zend_Currency_Exception('No currency defined');
00399 }
00400
00401 $data = Zend_Locale_Data::getContent('', 'regiontocurrency', $currency);
00402
00403 $result = explode(' ', $data);
00404 return $result;
00405 }
00406
00415 public function getCurrencyList($region = null)
00416 {
00417 if (empty($region) === true) {
00418 if (strlen($this->_locale) > 4) {
00419 $region = substr($this->_locale, (strpos($this->_locale, '_') + 1));
00420 }
00421 }
00422
00423 return Zend_Locale_Data::getList('', 'regiontocurrency', $region);
00424 }
00425
00431 public function toString()
00432 {
00433 return (empty($this->_options['name']) === false) ? $this->_options['name'] : $this->_options['currency'];
00434 }
00435
00441 public function __toString()
00442 {
00443 return $this->toString();
00444 }
00445
00451 public static function getCache()
00452 {
00453 $cache = Zend_Locale_Data::getCache();
00454 return $cache;
00455 }
00456
00463 public static function setCache(Zend_Cache_Core $cache)
00464 {
00465 Zend_Locale_Data::setCache($cache);
00466 }
00467
00473 public static function hasCache()
00474 {
00475 return Zend_Locale_Data::hasCache();
00476 }
00477
00483 public static function removeCache()
00484 {
00485 Zend_Locale_Data::removeCache();
00486 }
00487
00493 public static function clearCache()
00494 {
00495 Zend_Locale_Data::clearCache();
00496 }
00497
00507 public function setLocale($locale = null)
00508 {
00509 require_once 'Zend/Locale.php';
00510 try {
00511 $this->_locale = Zend_Locale::findLocale($locale);
00512 } catch (Zend_Locale_Exception $e) {
00513 require_once 'Zend/Currency/Exception.php';
00514 throw new Zend_Currency_Exception($e->getMessage());
00515 }
00516
00517
00518 $this->_options['currency'] = $this->getShortName(null, $this->_locale);
00519 $this->_options['name'] = $this->getName(null, $this->_locale);
00520 $this->_options['symbol'] = $this->getSymbol(null, $this->_locale);
00521
00522 return $this;
00523 }
00524
00530 public function getLocale()
00531 {
00532 return $this->_locale;
00533 }
00534
00547 private function _checkOptions(array $options = array())
00548 {
00549 if (count($options) === 0) {
00550 return $this->_options;
00551 }
00552
00553 foreach ($options as $name => $value) {
00554 $name = strtolower($name);
00555 if ($name !== 'format') {
00556 if (gettype($value) === 'string') {
00557 $value = strtolower($value);
00558 }
00559 }
00560
00561 switch($name) {
00562 case 'position':
00563 if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
00564 require_once 'Zend/Currency/Exception.php';
00565 throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
00566 }
00567
00568 break;
00569
00570 case 'format':
00571 if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
00572 require_once 'Zend/Currency/Exception.php';
00573 throw new Zend_Currency_Exception("'" .
00574 ((gettype($value) === 'object') ? get_class($value) : $value)
00575 . "' is not a known locale.");
00576 }
00577 break;
00578
00579 case 'display':
00580 if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
00581 ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
00582 require_once 'Zend/Currency/Exception.php';
00583 throw new Zend_Currency_Exception("Unknown display '$value'");
00584 }
00585 break;
00586
00587 case 'precision':
00588 if ($value === null) {
00589 $value = -1;
00590 }
00591
00592 if (($value < -1) or ($value > 30)) {
00593 require_once 'Zend/Currency/Exception.php';
00594 throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
00595 }
00596 break;
00597
00598 case 'script':
00599 try {
00600 Zend_Locale_Format::convertNumerals(0, $options['script']);
00601 } catch (Zend_Locale_Exception $e) {
00602 require_once 'Zend/Currency/Exception.php';
00603 throw new Zend_Currency_Exception($e->getMessage());
00604 }
00605 break;
00606
00607 case 'name':
00608
00609 case 'currency':
00610
00611 case 'symbol':
00612
00613 break;
00614
00615 default:
00616 require_once 'Zend/Currency/Exception.php';
00617 throw new Zend_Currency_Exception("Unknown option: '$name' = '$value'");
00618 break;
00619 }
00620 }
00621
00622 return $options;
00623 }
00624 }