7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Currency
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
19
* @version $Id: Currency.php 6137 2007-08-19 14:55:27Z shreef $
23
* include needed classes
25
require_once 'Zend/Locale.php';
26
require_once 'Zend/Locale/Data.php';
27
require_once 'Zend/Locale/Format.php';
30
* Class for handling currency notations
33
* @package Zend_Currency
34
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
35
* @license http://framework.zend.com/license/new-bsd New BSD License
39
// Constants for defining what currency symbol should be displayed
42
const USE_SHORTNAME = 3;
45
// Constants for defining the position of the currencysign
51
* Locale for this currency
55
private $_locale = null;
60
* The following options are available
61
* 'position' => Position for the currency sign
62
* 'script' => Script for the output
63
* 'format' => Locale for numeric output
64
* 'display' => Currency detail to show
65
* 'precision' => Precision for the currency
66
* 'name' => Name for this currency
67
* 'currency' => 3 lettered international abbreviation
68
* 'symbol' => Currency symbol
73
protected $_options = array(
74
'position' => self::STANDARD,
77
'display' => self::NO_SYMBOL,
85
* Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
87
* @param string $currency OPTIONAL currency short name
88
* @param string|Zend_Locale $locale OPTIONAL locale name
89
* @throws Zend_Currency_Exception When currency is invalid
91
public function __construct($currency = null, $locale = null)
93
if (Zend_Locale::isLocale($currency, true, false)) {
100
require_once 'Zend/Registry.php';
101
if (Zend_Registry::isRegistered('Zend_Locale') === true) {
102
$locale = Zend_Registry::get('Zend_Locale');
106
$this->setLocale($locale);
108
// Get currency details
109
$this->_options['currency'] = self::getShortName($currency, $this->_locale);
110
$this->_options['name'] = self::getName($currency, $this->_locale);
111
$this->_options['symbol'] = self::getSymbol($currency, $this->_locale);
113
if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
114
require_once 'Zend/Currency/Exception.php';
115
throw new Zend_Currency_Exception("Currency '$currency' not found");
119
$this->_options['position'] = $this->_updateFormat();
120
$this->_options['display'] = self::NO_SYMBOL;
121
if (empty($this->_options['symbol']) === false) {
122
$this->_options['display'] = self::USE_SYMBOL;
123
} else if (empty($this->_options['currency']) === false) {
124
$this->_options['display'] = self::USE_SHORTNAME;
129
* Gets the information required for formating the currency from Zend_Locale
131
* @return Zend_Currency
133
protected function _updateFormat()
135
$locale = (empty($this->_options['format']) === true) ? $this->_locale : $this->_options['format'];
137
// Getting the format information of the currency
138
$format = Zend_Locale_Data::getContent($locale, 'currencynumber');
140
iconv_set_encoding('internal_encoding', 'UTF-8');
141
if (iconv_strpos($format, ';') !== false) {
142
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
145
// Knowing the sign positioning information
146
if (iconv_strpos($format, '¤') === 0) {
147
$position = self::LEFT;
148
} else if (iconv_strpos($format, '¤') === (iconv_strlen($format) - 1)) {
149
$position = self::RIGHT;
156
* Returns a localized currency string
158
* @param integer|float $value Currency value
159
* @param array $options OPTIONAL options to set temporary
160
* @throws Zend_Currency_Exception When the value is not a number
163
public function toCurrency($value, array $options = array())
165
// Validate the passed number
166
if ((isset($value) === false) or (is_numeric($value) === false)) {
167
require_once 'Zend/Currency/Exception.php';
168
throw new Zend_Currency_Exception("Value '$value' has to be numeric");
171
$options = $this->_checkOptions($options) + $this->_options;
174
if (empty($options['format']) === true) {
175
$options['format'] = $this->_locale;
178
$value = Zend_Locale_Format::toNumber($value, array('locale' => $options['format'],
179
'precision' => $options['precision']));
181
// Localize the number digits
182
if (empty($options['script']) === false) {
183
$value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
186
// Get the sign to be placed next to the number
187
if (is_numeric($options['display']) === false) {
188
$sign = ' ' . $options['display'] . ' ';
190
switch($options['display']) {
191
case self::USE_SYMBOL:
192
$sign = ' ' . $options['symbol'] . ' ';
195
case self::USE_SHORTNAME:
196
$sign = ' ' . $options['currency'] . ' ';
200
$sign = ' ' . $options['name'] . ' ';
209
// Place the sign next to the number
210
if ($options['position'] === self::RIGHT) {
211
$value = $value . $sign;
212
} else if ($options['position'] === self::LEFT) {
213
$value = $sign . $value;
220
* Sets the formating options of the localized currency string
221
* If no parameter is passed, the standard setting of the
222
* actual set locale will be used
224
* @param array $options (Optional) Options to set
225
* @return Zend_Currency
227
public function setFormat(array $options = array())
229
$this->_options = $this->_checkOptions($options) + $this->_options;
234
* Internal function for checking static given locale parameter
236
* @param string $currency (Optional) Currency name
237
* @param string|Zend_Locale $locale (Optional) Locale to display informations
238
* @throws Zend_Currency_Exception When locale contains no region
239
* @return string The extracted locale representation as string
241
private function _checkParams($currency = null, $locale = null)
244
if ((empty($locale)) and (!empty($currency)) and
245
(Zend_Locale::isLocale($currency, true, false))) {
250
// Validate the locale and get the country short name
252
if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
253
$country = substr($locale, (strpos($locale, '_') + 1));
255
require_once 'Zend/Currency/Exception.php';
256
throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
259
// Get the available currencies for this country
260
$data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
261
if ((empty($currency) === false) and (empty($data) === false)) {
262
$abbreviation = $currency;
264
$abbreviation = $data;
267
return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
271
* Returns the actual or details of other currency symbols,
272
* when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
274
* @param string $currency (Optional) Currency name
275
* @param string|Zend_Locale $locale (Optional) Locale to display informations
278
public function getSymbol($currency = null, $locale = null)
280
if (($currency === null) and ($locale === null)) {
281
return $this->_options['symbol'];
284
$params = self::_checkParams($currency, $locale);
287
$symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
288
if (empty($symbol) === true) {
289
$symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
292
if (empty($symbol) === true) {
300
* Returns the actual or details of other currency shortnames
302
* @param string $currency OPTIONAL Currency's name
303
* @param string|Zend_Locale $locale OPTIONAL The locale
306
public function getShortName($currency = null, $locale = null)
308
if (($currency === null) and ($locale === null)) {
309
return $this->_options['currency'];
312
$params = self::_checkParams($currency, $locale);
315
if (empty($params['currency']) === true) {
316
return $params['name'];
319
$list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
320
if (empty($list) === true) {
321
$list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
322
if (empty($list) === false) {
323
$list = $params['currency'];
327
if (empty($list) === true) {
335
* Returns the actual or details of other currency names
337
* @param string $currency (Optional) Currency's short name
338
* @param string|Zend_Locale $locale (Optional) The locale
341
public function getName($currency = null, $locale = null)
343
if (($currency === null) and ($locale === null)) {
344
return $this->_options['name'];
347
$params = self::_checkParams($currency, $locale);
350
$name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
351
if (empty($name) === true) {
352
$name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
355
if (empty($name) === true) {
363
* Returns a list of regions where this currency is or was known
365
* @param string $currency OPTIONAL Currency's short name
366
* @throws Zend_Currency_Exception When no currency was defined
367
* @return array List of regions
369
public function getRegionList($currency = null)
371
if ($currency === null) {
372
$currency = $this->_options['currency'];
375
if (empty($currency) === true) {
376
require_once 'Zend/Currency/Exception.php';
377
throw new Zend_Currency_Exception('No currency defined');
380
$data = Zend_Locale_Data::getContent('', 'regiontocurrency', $currency);
382
$result = explode(' ', $data);
387
* Returns a list of currencies which are used in this region
388
* a region name should be 2 charachters only (f.e. EG, DE, US)
389
* If no region is given, the actual region is used
391
* @param string $region OPTIONAL Region to return the currencies for
392
* @return array List of currencies
394
public function getCurrencyList($region = null)
396
if (empty($region) === true) {
397
if (strlen($this->_locale) > 4) {
398
$region = substr($this->_locale, (strpos($this->_locale, '_') + 1));
402
return Zend_Locale_Data::getList('', 'regiontocurrency', $region);
406
* Returns the actual currency name
410
public function toString()
412
return (empty($this->_options['name']) === false) ? $this->_options['name'] : $this->_options['currency'];
416
* Returns the currency name
420
public function __toString()
422
return $this->toString();
426
* Returns the set cache
428
* @return Zend_Cache_Core The set cache
430
public static function getCache()
432
$cache = Zend_Locale_Data::getCache();
437
* Sets a cache for Zend_Currency
439
* @param Zend_Cache_Core $cache Cache to set
442
public static function setCache(Zend_Cache_Core $cache)
444
Zend_Locale_Data::setCache($cache);
448
* Returns true when a cache is set
452
public static function hasCache()
454
return Zend_Locale_Data::hasCache();
458
* Removes any set cache
462
public static function removeCache()
464
Zend_Locale_Data::removeCache();
468
* Clears all set cache data
472
public static function clearCache()
474
Zend_Locale_Data::clearCache();
478
* Sets a new locale for data retreivement
479
* Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
480
* 'xx_YY' will be set to 'root' because 'xx' does not exist
482
* @param string|Zend_Locale $locale (Optional) Locale for parsing input
483
* @throws Zend_Currency_Exception When the given locale does not exist
484
* @return Zend_Currency Provides fluent interface
486
public function setLocale($locale = null)
488
if (!Zend_Locale::isLocale($locale, false, false)) {
489
if (!Zend_Locale::isLocale($locale, true, false)) {
490
require_once 'Zend/Currency/Exception.php';
491
throw new Zend_Currency_Exception("Given locale (" . (string) $locale . ") does not exist");
493
$locale = new Zend_Locale();
497
$this->_locale = (string) $locale;
499
// Get currency details
500
$this->_options['currency'] = $this->getShortName(null, $this->_locale);
501
$this->_options['name'] = $this->getName(null, $this->_locale);
502
$this->_options['symbol'] = $this->getSymbol(null, $this->_locale);
508
* Returns the actual set locale
512
public function getLocale()
514
return $this->_locale;
518
* Internal method for checking the options array
520
* @param array $options Options to check
521
* @throws Zend_Currency_Exception On unknown position
522
* @throws Zend_Currency_Exception On unknown locale
523
* @throws Zend_Currency_Exception On unknown display
524
* @throws Zend_Currency_Exception On precision not between -1 and 30
525
* @throws Zend_Currency_Exception On problem with script conversion
526
* @throws Zend_Currency_Exception On unknown options
529
private function _checkOptions(array $options = array())
531
if (count($options) === 0) {
532
return $this->_options;
535
foreach ($options as $name => $value) {
536
$name = strtolower($name);
537
if ($name !== 'format') {
538
if (gettype($value) === 'string') {
539
$value = strtolower($value);
545
if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
546
require_once 'Zend/Currency/Exception.php';
547
throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
550
if ($value === self::STANDARD) {
551
$options['position'] = $this->_updateFormat();
556
if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
557
require_once 'Zend/Currency/Exception.php';
558
throw new Zend_Currency_Exception("'" .
559
((gettype($value) === 'object') ? get_class($value) : $value)
560
. "' is not a known locale.");
565
if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
566
($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
567
require_once 'Zend/Currency/Exception.php';
568
throw new Zend_Currency_Exception("Unknown display '$value'");
573
if ($value === null) {
577
if (($value < -1) or ($value > 30)) {
578
require_once 'Zend/Currency/Exception.php';
579
throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
585
Zend_Locale_Format::convertNumerals(0, $options['script']);
586
} catch (Zend_Locale_Exception $e) {
587
require_once 'Zend/Currency/Exception.php';
588
throw new Zend_Currency_Exception($e->getMessage());
593
// Break intentionally omitted
595
// Break intentionally omitted
601
require_once 'Zend/Currency/Exception.php';
602
throw new Zend_Currency_Exception("Unknown option: '$name' = '$value'");