/** * Internal method for checking the options array * * @param array $options Options to check * @throws Zend_Currency_Exception On unknown position * @throws Zend_Currency_Exception On unknown locale * @throws Zend_Currency_Exception On unknown display * @throws Zend_Currency_Exception On precision not between -1 and 30 * @throws Zend_Currency_Exception On problem with script conversion * @throws Zend_Currency_Exception On unknown options * @return array */ protected function _checkOptions(array $options = array()) { if (count($options) === 0) { return $this->_options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'format') { if (gettype($value) === 'string') { $value = strtolower($value); } } switch ($name) { case 'position': if ($value !== self::STANDARD and $value !== self::RIGHT and $value !== self::LEFT) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("Unknown position '" . $value . "'"); } break; case 'format': if (empty($value) === false and Zend_Locale::isLocale($value, null, false) === false) { if (!is_string($value) || strpos($value, '0') === false) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is no format token"); } } break; case 'display': if (is_numeric($value) and $value !== self::NO_SYMBOL and $value !== self::USE_SYMBOL and $value !== self::USE_SHORTNAME and $value !== self::USE_NAME) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("Unknown display '{$value}'"); } break; case 'precision': if ($value === null) { $value = -1; } if ($value < -1 or $value > 30) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("'{$value}' precision has to be between -1 and 30."); } break; case 'script': try { Zend_Locale_Format::convertNumerals(0, $options['script']); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception($e->getMessage()); } break; default: break; } } return $options; }
/** * test toNumberSystem * expected string */ public function testToNumberSystem() { try { $value = Zend_Locale_Format::convertNumerals('١١٠', 'xxxx'); $this->fail("no conversion expected"); } catch (Zend_Locale_Exception $e) { // success } try { $value = Zend_Locale_Format::convertNumerals('١١٠', 'Arab', 'xxxx'); $this->fail("no conversion expected"); } catch (Zend_Locale_Exception $e) { // success } $this->assertEquals('110', Zend_Locale_Format::convertNumerals('١١٠', 'Arab')); $this->assertEquals('११०', Zend_Locale_Format::convertNumerals('١١٠', 'Arab', 'Deva')); $this->assertEquals('११०', Zend_Locale_Format::convertNumerals('١١٠', 'arab', 'dEVa')); $this->assertEquals('١١٠', Zend_Locale_Format::convertNumerals('110', 'Latn', 'Arab')); $this->assertEquals('١١٠', Zend_Locale_Format::convertNumerals('110', 'latn', 'Arab')); }
/** * Returns a localized currency string * * @param int|float $value Currency value * @param string $script OPTIONAL Number script to use for output * @param string|Zend_Locale $locale OPTIONAL Locale for output formatting * @return string */ public function toCurrency($value, $script = NULL, $locale = NULL) { //validate the passed number if (!isset($value) || !is_numeric($value)) { throw new Zend_Currency_Exception('the first param should be a number'); } //format the number if (!empty($locale)) { $value = Zend_Locale_Format::toNumber($value, null, $locale); } else { $value = Zend_Locale_Format::toNumber($value, null, $this->_formatLocale); } //localize the number digits if (!empty($script)) { $value = Zend_Locale_Format::convertNumerals($value, 'Default', $script); } else { if (!empty($this->_numberScript)) { $value = Zend_Locale_Format::convertNumerals($value, 'Default', $this->_numberScript); } } //get the sign to be placed next to the number $sign = ''; if ($this->_useSymbol && !empty($this->_symbol)) { $sign = $this->_symbol; } else { if ($this->_useName) { $sign = $this->_fullName; } else { $sign = $this->_shortName; } } //place the sign next to the number if ($this->_signPosition == self::RIGHT) { $value = $value . ' ' . $sign; } else { if ($this->_signPosition == self::LEFT) { $value = $sign . ' ' . $value; } } return $value; }
/** * Internal method for checking the options array * * @param array $options * @return array * @throws Zend_Currency_Exception */ private function checkOptions(array $options = array()) { if (count($options) == 0) { return $this->_options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'format') { if (gettype($value) === 'string') { $value = strtolower($value); } } if (array_key_exists($name, $this->_options)) { switch ($name) { case 'position': if ($value !== self::STANDARD and $value !== self::RIGHT and $value !== self::LEFT) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("Unknown position '" . $value . "'"); } if ($value === self::STANDARD) { $options['position'] = $this->_updateFormat(); } break; case 'format': if (!empty($value) && !Zend_Locale::isLocale($value)) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is not a known locale."); } break; case 'display': if (is_numeric($value) and $value !== self::NO_SYMBOL and $value !== self::USE_SYMBOL and $value !== self::USE_SHORTNAME and $value !== self::USE_NAME) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("Unknown display '{$value}'"); } break; case 'precision': if ($value === NULL) { $value = -1; } if ($value < -1 || $value > 30) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("'{$value}' precision has to be between -1 and 30."); } break; case 'script': try { Zend_Locale_Format::convertNumerals(0, $options['script']); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception($e->getMessage()); } break; } } else { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception("Unknown option: '{$name}' = '{$value}'"); } } return $options; }
/** * Sets the formating options of the localized currency string * If no parameter is passed, the standard setting of the * actual set locale will be used * * @param const|string $rules OPTIONAL formating rules for currency * - USE_SYMBOL|NOSYMBOL : display currency symbol * - USE_NAME|NONAME : display currency name * - STANDARD|RIGHT|LEFT : where to display currency symbol/name * - string: gives the currency string/name/sign to set * @param string $script OPTIONAL Number script to use for output * @param string|Zend_Locale $locale OPTIONAL Locale for output formatting * @return Zend_Currency */ public function setFormat($rules = null, $script = null, $locale = null) { if (!is_numeric($rules) and $rules !== null) { $this->_usedSign = $rules; } else { if ($rules / self::LEFT >= 1) { $this->_position = self::LEFT; $rules -= self::LEFT; } if ($rules / self::RIGHT >= 1) { $this->_position = self::RIGHT; $rules -= self::RIGHT; } if ($rules / self::STANDARD >= 1) { $this->_updateFormat(); $rules -= self::STANDARD; } if (!empty($rules)) { $this->_usedSign = $rules; } } //set the new number script if (!empty($script)) { try { Zend_Locale_Format::convertNumerals(0, $script); $this->_script = $script; } catch (Zend_Locale_Exception $e) { throw new Zend_Currency_Exception($e->getMessage()); } } //set the locale for the number formating process if (!empty($locale)) { if ($locale instanceof Zend_Locale) { $locale = $locale->toString(); } if ($locale = Zend_Locale::isLocale($locale) and strlen($locale) > 4) { $this->_formatLocale = $locale; } else { throw new Zend_Currency_Exception("Locale '{$locale}' is no valid locale"); } } return $this; }
/** * sets the script name which used for formatting the outputed numbers * * @param string $script script name * @return void * @throws Zend_Currency_Exception */ private function _setNumberScript($script) { try { Zend_Locale_Format::convertNumerals(0, $script); $this->_numberScript = $script; } catch (Zend_Locale_Exception $e) { throw new Zend_Currency_Exception($e->getMessage()); } }
public function convertNumerals($string, $locale = null) { if (!$locale) { $locale = $this->getLocale(); } // Get the numbering system $defaultNumberingSystem = null; try { $defaultNumberingSystem = Zend_Locale_Data::getContent($locale, 'defaultnumberingsystem'); } catch (Zend_Locale_Exception $e) { // Silence } // Convert now if ($defaultNumberingSystem && 'latn' != strtolower($defaultNumberingSystem)) { $string = Zend_Locale_Format::convertNumerals($string, 'Latn', $defaultNumberingSystem); } return $string; }
/** * Convert the digits 0-9 into the local script * * Used for years, etc., where we do not want thousands-separators, decimals, etc. * * @param int $n * * @return string */ public static function digits($n) { if (self::$numbering_system != 'latn') { return Zend_Locale_Format::convertNumerals($n, 'latn', self::$numbering_system); } else { return $n; } }