/** * @return string */ public function getShortName($currency = null, $locale = null) { if ($locale === null) { $locale = $this->getLocale(); } if ($currency === null) { $currency = $this->getCurrency(); } return $this->currencyLib->getShortName($currency, $locale); }
/** * Convert currency value to another currency. Will throw an exception if value cannot be converted. * * @param $ps_value string Currency value with specifier (Ex. $500, USD 500, ��1200, CAD 750) * @param $ps_to string Specifier of currency to convert value to (Ex. USD, CAD, EUR) * @param $pa_options array Options are: * numericValue = return floating point numeric value only, without currency specifier. Default is false. * * @return string Converted value with currency specifier, unless numericValue option is set. */ public static function convert($ps_value, $ps_to, $pa_options = null) { $va_currency_data = WLPlugCurrencyConversionEuroBank::_loadData(); $ps_to = parent::normalizeCurrencySpecifier($ps_to); if (preg_match("!^([^\\d]+)([\\d\\.\\,]+)\$!", trim($ps_value), $va_matches)) { $vs_decimal_value = (double) $va_matches[2]; $vs_currency_specifier = trim($va_matches[1]); // or 1 } else { if (preg_match("!^([\\d\\.\\,]+)([^\\d]+)\$!", trim($ps_value), $va_matches)) { $vs_decimal_value = (double) $va_matches[1]; $vs_currency_specifier = trim($va_matches[2]); // or 2 } else { if (preg_match("!(^[\\d\\,\\.]+\$)!", trim($ps_value), $va_matches)) { $vs_decimal_value = (double) $va_matches[1]; $vs_currency_specifier = null; // derp } else { throw new Exception(_t('%1 is not a valid currency value; be sure to include a currency symbol', $ps_value)); return false; } } } if (!$vs_currency_specifier) { $o_currency = new Zend_Currency(); $vs_currency_specifier = $o_currency->getShortName(); } $vs_currency_specifier = parent::normalizeCurrencySpecifier($vs_currency_specifier); if (!self::canConvert($vs_currency_specifier, $ps_to)) { throw new Exception(_t('Cannot convert %1 to %2', $vs_currency_specifier, $ps_to)); return false; } $vn_value_in_euros = $vs_decimal_value / $va_currency_data[$vs_currency_specifier]; $vn_converted_value = $vn_value_in_euros * $va_currency_data[$ps_to]; if (caGetOption('numericValue', $pa_options, false)) { return (double) sprintf("%01.2f", $vn_converted_value); } if (Zend_Registry::isRegistered("Zend_Locale")) { $o_locale = Zend_Registry::get('Zend_Locale'); } else { $o_locale = new Zend_Locale('en_US'); } $vs_format = Zend_Locale_Data::getContent($o_locale, 'currencynumber'); // this returns a string like '50,00 ��' for locale de_DE $vs_decimal_with_placeholder = Zend_Locale_Format::toNumber($vn_converted_value, array('locale' => $o_locale, 'number_format' => $vs_format, 'precision' => 2)); // if the currency placeholder is the first character, for instance in en_US locale ($10), insert a space. // this has to be done because we don't print "$10" (which is expected in the locale rules) but "USD 10" ... and that looks nicer with an additional space. if (substr($vs_decimal_with_placeholder, 0, 2) == '��') { // for whatever reason '��' has length 2 $vs_decimal_with_placeholder = str_replace('��', '�� ', $vs_decimal_with_placeholder); } // insert currency which is not locale-dependent in our case return str_replace('��', $ps_to, $vs_decimal_with_placeholder); }
/** * Specific controller action for displaying a particular list of links * for a class * * @return mixed */ public function index() { if (GoogleShoppingFeed::enabled()) { Config::inst()->update('SSViewer', 'set_source_file_comments', false); $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); $items = GoogleShoppingFeed::get_items(); $currency = new Zend_Currency(i18n::get_locale()); $this->extend('updateGoogleShoppingFeedItems', $items); return array("SiteConfig" => SiteConfig::current_site_config(), 'Items' => $items, "Currency" => $currency->getShortName()); } else { return new SS_HTTPResponse(_t("GoogleShoppingFeed.PageNotFound", 'Page not found'), 404); } }
/** * Internal method which calculates the exchanges currency * * @param float|integer|Zend_Currency $value Compares the currency with this value * @param string|Zend_Currency $currency The currency to compare this value from * @return unknown */ protected function _exchangeCurrency($value, $currency) { if ($value instanceof Zend_Currency) { $currency = $value->getShortName(); $value = $value->getValue(); } else { $currency = $this->getShortName($currency, $this->getLocale()); } $rate = 1; if ($currency !== $this->getShortName()) { $service = $this->getService(); if (!$service instanceof Zend_Currency_CurrencyInterface) { require_once 'Zend/Currency/Exception.php'; throw new Zend_Currency_Exception('No exchange service applied'); } $rate = $service->getRate($currency, $this->getShortName()); } $value *= $rate; return $value; }
/** * Returns default value for a preference * * @param string $ps_pref Preference code * @param array $pa_options No options supported yet * @return mixed Type returned varies by preference */ public function getPreferenceDefault($ps_pref, $pa_options = null) { if (!is_array($va_pref_info = $this->getPreferenceInfo($ps_pref))) { return null; } switch ($va_pref_info["formatType"]) { # --------------------------------- case 'FT_OBJECT_EDITOR_UI': case 'FT_OBJECT_LOT_EDITOR_UI': case 'FT_ENTITY_EDITOR_UI': case 'FT_PLACE_EDITOR_UI': case 'FT_OCCURRENCE_EDITOR_UI': case 'FT_COLLECTION_EDITOR_UI': case 'FT_STORAGE_LOCATION_EDITOR_UI': case 'FT_OBJECT_REPRESENTATION_EDITOR_UI': case 'FT_REPRESENTATION_ANNOTATION_EDITOR_UI': case 'FT_SET_EDITOR_UI': case 'FT_SET_ITEM_EDITOR_UI': case 'FT_LIST_EDITOR_UI': case 'FT_LIST_ITEM_EDITOR_UI': case 'FT_LOAN_EDITOR_UI': case 'FT_MOVEMENT_EDITOR_UI': case 'FT_TOUR_EDITOR_UI': case 'FT_TOUR_STOP_EDITOR_UI': case 'FT_SEARCH_FORM_EDITOR_UI': case 'FT_BUNDLE_DISPLAY_EDITOR_UI': case 'FT_RELATIONSHIP_TYPE_EDITOR_UI': case 'FT_USER_INTERFACE_EDITOR_UI': case 'FT_USER_INTERFACE_SCREEN_EDITOR_UI': case 'FT_IMPORT_EXPORT_MAPPING_EDITOR_UI': case 'FT_IMPORT_EXPORT_MAPPING_GROUP_EDITOR_UI': $vn_type_id = is_array($pa_options) && isset($pa_options['type_id']) && (int) $pa_options['type_id'] ? (int) $pa_options['type_id'] : null; $vn_table_num = $this->_editorPrefFormatTypeToTableNum($va_pref_info["formatType"]); $va_uis = $this->_getUIListByType($vn_table_num); $va_defaults = array(); foreach ($va_uis as $vn_type_id => $va_editor_info) { foreach ($va_editor_info as $vn_ui_id => $va_editor_labels) { $va_defaults[$vn_type_id] = $vn_ui_id; } } return $va_defaults; break; case 'FT_TEXT': if ($va_pref_info['displayType'] == 'DT_CURRENCIES') { // this respects the global UI locale which is set using Zend_Locale $o_currency = new Zend_Currency(); return ($vs_currency_specifier = $o_currency->getShortName()) ? $vs_currency_specifier : "CAD"; } return $va_pref_info["default"] ? $va_pref_info["default"] : null; break; # --------------------------------- # --------------------------------- default: return $va_pref_info["default"] ? $va_pref_info["default"] : null; break; # --------------------------------- } }
public function parseValue($ps_value, $pa_element_info, $pa_options = null) { $ps_value = trim($ps_value); $va_settings = $this->getSettingValuesFromElementArray($pa_element_info, array('minValue', 'maxValue', 'mustNotBeBlank')); if (strlen($ps_value) == 0) { if ((bool) $va_settings['mustNotBeBlank']) { $this->postError(1970, _t('%1 must not be empty', $pa_element_info['displayLabel']), 'CurrencyAttributeValue->parseValue()'); return false; } return null; } // it's either "<something><decimal>" ($1000) or "<decimal><something>" (1000 EUR) or just "<decimal>" with an implicit <something> // either if (preg_match("!^([^\\d]+)([\\d\\.\\,]+)\$!", trim($ps_value), $va_matches)) { $vs_decimal_value = $va_matches[2]; $vs_currency_specifier = trim($va_matches[1]); // or 1 } else { if (preg_match("!^([\\d\\.\\,]+)([^\\d]+)\$!", trim($ps_value), $va_matches)) { $vs_decimal_value = $va_matches[1]; $vs_currency_specifier = trim($va_matches[2]); // or 2 } else { if (preg_match("!(^[\\d\\,\\.]+\$)!", trim($ps_value), $va_matches)) { $vs_decimal_value = $va_matches[1]; $vs_currency_specifier = null; // derp } else { $this->postError(1970, _t('%1 is not a valid currency value; be sure to include a currency symbol', $pa_element_info['displayLabel']), 'CurrencyAttributeValue->parseValue()'); return false; } } } if (!$vs_currency_specifier) { // this respects the global UI locale which is set using Zend_Locale $o_currency = new Zend_Currency(); $vs_currency_specifier = $o_currency->getShortName(); } // get UI locale from registry and convert string to actual php float // based on rules for this locale (e.g. most non-US locations use 10.000,00 as notation) if (Zend_Registry::isRegistered("Zend_Locale")) { $o_locale = Zend_Registry::get('Zend_Locale'); } else { $o_locale = new Zend_Locale('en_US'); } try { $vn_value = Zend_Locale_Format::getNumber($vs_decimal_value, array('locale' => $o_locale, 'precision' => 2)); } catch (Zend_Locale_Exception $e) { $this->postError(1970, _t('%1 does not use a valid decimal notation for your locale', $pa_element_info['displayLabel']), 'CurrencyAttributeValue->parseValue()'); return false; } switch ($vs_currency_specifier) { case '$': $o_config = Configuration::load(); $vs_currency_specifier = ($vs_dollars_are_this = $o_config->get('default_dollar_currency')) ? $vs_dollars_are_this : 'USD'; break; case '¥': $vs_currency_specifier = 'JPY'; break; case '£': $vs_currency_specifier = 'GBP'; break; case '€': $vs_currency_specifier = 'EUR'; break; default: $vs_currency_specifier = strtoupper($vs_currency_specifier); break; } if (strlen($vs_currency_specifier) != 3) { $this->postError(1970, _t('Currency specified for %1 does not appear to be valid', $pa_element_info['displayLabel']), 'CurrencyAttributeValue->parseValue()'); return false; } if ($vn_value < 0) { $this->postError(1970, _t('%1 must not be negative', $pa_element_info['displayLabel']), 'CurrencyAttributeValue->parseValue()'); return false; } if ($vn_value < floatval($va_settings['minValue'])) { // value is too low $this->postError(1970, _t('%1 must be at least %2', $pa_element_info['displayLabel'], $va_settings['minValue']), 'CurrencyAttributeValue->parseValue()'); return false; } if (floatval($va_settings['maxValue']) > 0 && $vn_value > floatval($va_settings['maxValue'])) { // value is too high $this->postError(1970, _t('%1 must be less than %2', $pa_element_info['displayLabel'], $va_settings['maxValue']), 'CurrencyAttributeValue->parseValue()'); return false; } return array('value_longtext1' => $vs_currency_specifier, 'value_decimal1' => $vn_value); }
/** * @ZF-9519 */ public function testSetValueWithoutLocale() { $currency = new Zend_Currency('RUB', 'ru_RU'); require_once 'Currency/ExchangeTest.php'; $this->assertEquals(null, $currency->getService()); $currency->setService(new ExchangeTest()); $this->assertTrue($currency->getService() instanceof Zend_Currency_CurrencyInterface); $currency->setValue(100, 'USD'); $this->assertEquals(50, $currency->getValue()); $this->assertEquals('RUB', $currency->getShortName()); }
/** * Testing value at initiation */ public function testValueWithConstructor() { $currency = new Zend_Currency(array('currency' => 'EUR', 'locale' => 'de_AT', 'value' => 100)); $this->assertEquals('de_AT', $currency->getLocale()); $this->assertEquals('EUR', $currency->getShortName()); $this->assertEquals('€ 100,00', $currency->toCurrency()); }
/** * test getShortName */ public function testGetShortName() { $locale = new Zend_Locale('de_AT'); $this->assertSame(Zend_Currency::getShortName('EUR','de_AT'), 'Euro'); $this->assertSame(Zend_Currency::getShortName('EUR',$locale), 'Euro'); $this->assertSame(Zend_Currency::getShortName('de_AT'), 'Euro'); try { $this->assertSame(is_string(Zend_Currency::getShortName('EUR')), true); } catch (Zend_Currency_Exception $e) { // Systems without locale are expected to be ok from the testbed $this->assertSame($e->getMessage(), "Locale 'root' is no valid locale"); } try { Zend_Currency::getShortName('EUR', 'xy_ZT'); $this->fail(); } catch (Zend_Currency_Exception $e) { // success } }
/** * test getShortName */ public function testGetShortName() { $locale = new Zend_Locale('de_AT'); $currency = new Zend_Currency('de_AT'); $this->assertSame('EUR', $currency->getShortName('Euro', 'de_AT')); $this->assertSame('EUR', $currency->getShortName('Euro', $locale)); $this->assertSame('USD', $currency->getShortName('US-Dollar','de_AT')); $this->assertSame('EUR', $currency->getShortName('de_AT' )); try { $currency->getShortName('EUR', 'xy_ZT'); $this->fail("exception expected"); } catch (Zend_Currency_Exception $e) { // success } }
/** * Set the locale */ function setLocale($locale = LOCALE) { //detect if local file with language string constants is available and load it $local_constants_file = ONXSHOP_PROJECT_DIR . "locales/{$locale}/constants.php"; if (file_exists($local_constants_file)) { require_once $local_constants_file; } //load global language string constants file with fallback to en_GB $global_constants_file = ONXSHOP_DIR . "locales/{$locale}/constants.php"; if (!file_exists($global_constants_file)) { $global_constants_file = ONXSHOP_DIR . "locales/en_GB.UTF-8/constants.php"; } require_once $global_constants_file; //now set system locale setlocale(LC_ALL, LOCALE); //but for numbers keep english setlocale(LC_NUMERIC, 'en_GB.UTF-8'); require_once 'lib/Zend/Locale.php'; require_once 'lib/Zend/Currency.php'; if (LOCALE == 'cs_CZ.UTF-8') { putenv("TZ=Europe/Prague"); putenv("LANG=cs_CZ.UTF-8"); date_default_timezone_set("Europe/Prague"); } else { putenv("TZ=Europe/London"); putenv("LANG=en_GB.UTF-8"); date_default_timezone_set("Europe/London"); } $Zend_locale = new Zend_Locale(substr($locale, 0, 5)); $Zend_currency = new Zend_Currency($Zend_locale->toString()); define('GLOBAL_LOCALE_CURRENCY', $Zend_currency->getShortName()); }