/** * 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 array $options (Optional) Options to set * @return Zend_Currency */ public function setFormat(array $options = array()) { if (isset($options['id'])) { $this->id = (int) $options['id']; } return parent::setFormat($options); }
/** * переводит значение заданного типа в строку * * @param mixed $data значение, которое нужно сконвертировать * @param string $type тип данных ('bool','date') * @param int $precision_default задание точного количества знаков после запятой * @return string полученная строка, представляющая данные */ function type_to_str($data, $type, $precision_default = -1) { switch ($type) { case 'encode': return htmlentities($data, ENT_QUOTES, 'utf-8'); case 'translate': return __($data); case "integer": /** * Локально пустые значения спокойно форматируется зендом в 0, * но на сервере, на orbitscipts.com? после форматирования пустого значение, * оно так и остаётся пустое. Я не знаю почему. */ if (is_null($data) || $data == '') { $data = 0; } $res = Zend_Locale_Format::toInteger($data, array('locale' => get_instance()->locale)); return $res; case "float": $precision = 2; /** * Определние нужной точности для денег */ if ($precision_default < 0) { $vals = explode('.', strval((double) $data)); if (isset($vals[1])) { $len = strlen($vals[1]); if ($len > 3) { $precision = 4; } elseif ($len > 2) { $precision = 3; } } } else { $precision = $precision_default; } return number_format($data, $precision, '.', ''); //return Zend_Locale_Format::toFloat($data, array('locale'=> get_instance()->locale)); //return Zend_Locale_Format::toFloat($data, array('locale'=> get_instance()->locale)); case 'procent': return type_to_str($data, 'float', 2) . ' %'; case "money": //return str_replace('%', type_to_str($data, 'float'), get_format('money')); if (is_null($data)) { $data = 0; } /** * @todo Возможно прийдётся определять тип валюты как-то глобально, * когда понадобится использовать что-нибудь отличное от доллара. */ try { $currency = new Zend_Currency(get_instance()->locale, 'USD'); } catch (Zend_Currency_Exception $e) { $currency = new Zend_Currency(get_instance()->default_locale, 'USD'); } $precision = 2; /** * Определние нужной точности для денег */ $vals = explode('.', strval((double) $data)); if (isset($vals[1])) { $len = strlen($vals[1]); } $currency->setFormat(array('precision' => $precision)); try { $value = trim($currency->toCurrency($data)); /** * проверка preg_matchем нужна потмоу что, например в китайском валюта отображается * как: US$0.00 */ if (get_instance()->locale != 'en_US' && preg_match('|^[0-9,\\.\\$\\s\\xc2\\xa0]+$|', $value, $matches)) { $value = '$' . trim(str_replace('$', '', $value)); } } catch (Exception $e) { $value = '0.00'; } return $value; case "nonzeromoney": if ($data == 0) { return "—"; } return type_to_str($data, "money"); case "mysqldate": $data = mktime(0, 0, 0, substr($data, 5, 2), substr($data, 8, 2), substr($data, 0, 4)); return date(get_date_format(), $data); case "mysqlfloat": //return str_replace(',','.',$data); return number_format($data, 8, '.', ''); case 'databasedate': return date("Y-m-d", $data); case 'databasedatetime': return date("Y-m-d H:i:s", $data); case "date": if ($data == 0 || is_null($data)) { return ''; } return date(get_date_format(), $data); case "datetime": $res = date(get_date_format(), $data) . ' ' . date(get_time_format(), $data); return $res; case "bool": return $data ? "true" : "false"; case 'textcode': $num = (int) ($data ^ 67894523); $text = ''; while ($num) { $text .= chr(ord('a') + $num % 26); $num = (int) ($num / 26); } return $text; case "impressions": if ($data < 1000) { return __('< 1 K'); } if ($data < 5000) { return __('1~5 K'); } if ($data < 10000) { return __('5~10 K'); } if ($data < 50000) { return __('10~50 K'); } if ($data < 100000) { return __('50~100 K'); } else { return __('> 100 K'); } case "clicks": if ($data < 100) { return __('< 100'); } if ($data < 900) { return '~ ' . round($data / 100) * 100; } else { return '~ ' . round($data / 1000) . ' K'; } case "mime": return '=?UTF-8?B?' . base64_encode($data) . '?='; default: return $data; } }
/** * Create Zend_Currency object for current locale * * @param string $currency * @return Zend_Currency */ public function currency($currency) { Varien_Profiler::start('locale/currency'); if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) { try { $currencyObject = new Zend_Currency($currency, $this->getLocale()); } catch (Exception $e) { $currencyObject = new Zend_Currency($this->getCurrency(), $this->getLocale()); $options = array('name' => $currency, 'currency' => $currency, 'symbol' => $currency); $currencyObject->setFormat($options); } self::$_currencyCache[$this->getLocaleCode()][$currency] = $currencyObject; } Varien_Profiler::stop('locale/currency'); return self::$_currencyCache[$this->getLocaleCode()][$currency]; }
/** * Create Zend_Currency object for current locale * * @param string $currency * @return Zend_Currency */ public function currency($currency) { Varien_Profiler::start('locale/currency'); if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) { $options = array(); try { $currencyObject = new Zend_Currency($currency, $this->getLocale()); } catch (Exception $e) { $currencyObject = new Zend_Currency($this->getCurrency(), $this->getLocale()); $options['name'] = $currency; $options['currency'] = $currency; $options['symbol'] = $currency; } $options = new Varien_Object($options); Mage::dispatchEvent('currency_display_options_forming', array('currency_options' => $options, 'base_code' => $currency)); $currencyObject->setFormat($options->toArray()); self::$_currencyCache[$this->getLocaleCode()][$currency] = $currencyObject; } Varien_Profiler::stop('locale/currency'); return self::$_currencyCache[$this->getLocaleCode()][$currency]; }
/** * testing getRegionList */ public function testGetRegionList() { // look if locale is detectable try { $locale = new Zend_Locale(); } catch (Zend_Locale_Exception $e) { $this->markTestSkipped('Autodetection of locale failed'); return; } try { $currency = new Zend_Currency(array('currency' => 'USD')); $this->assertTrue(in_array('US', $currency->getRegionList())); } catch (Zend_Currency_Exception $e) { $this->assertContains('No region found within the locale', $e->getMessage()); } $currency = new Zend_Currency(array('currency' => 'USD'), 'en_US'); $currency->setFormat(array('currency' => null)); try { $this->assertTrue(in_array('US', $currency->getRegionList())); $this->fail("Exception expected"); } catch (Zend_Currency_Exception $e) { $this->assertContains("No currency defined", $e->getMessage()); } $currency = new Zend_Currency(array('currency' => 'USD'), 'en_US'); $this->assertEquals(array(0 => 'AS', 1 => 'EC', 2 => 'FM', 3 => 'GU', 4 => 'IO', 5 => 'MH', 6 => 'MP', 7 => 'PR', 8 => 'PW', 9 => "SV", 10 => 'TC', 11 => 'TL', 12 => 'UM', 13 => 'US', 14 => 'VG', 15 => 'VI'), $currency->getRegionList()); }
/** * Return Zend_Currency object * * @param string $code * @return Zend_Currency */ public function getCurrency($code = '') { if (empty($code)) { $code = $this->getCode(); } if (!isset($this->_currency[$code])) { $options = $this->_getCurrencyOptions($code); Zend_Currency::setCache(Axis::cache()); try { $currency = new Zend_Currency($options['currency'], $options['format'] === null ? Axis_Locale::getLocale() : $options['format']); } catch (Zend_Currency_Exception $e) { Axis::message()->addError($e->getMessage() . ": " . Axis::translate('locale')->__("Try to change the format of this currency to English (United States) - en_US")); $options = $this->_getSystemSafeCurrencyOptions(); $currency = new Zend_Currency($options['currency'], $options['format']); } $currency->setFormat($options); $this->_currency[$code] = $currency; } return $this->_currency[$code]; }
/** * testing setFormat * */ public function testSetFormat() { $USD = new Zend_Currency('USD', 'en_US'); $USD->setFormat(null, 'Arab'); $this->assertSame($USD->toCurrency(253292.1832), '$ ٢٥٣,٢٩٢.١٨٣٢'); $USD->setFormat(null, 'Arab', 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '$ ٢٥٣.٢٩٢,١٨٣٢'); $USD->setFormat(null, 'Default', 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '$ 253.292,1832'); // allignment of currency signs $USD->setFormat(Zend_Currency::RIGHT, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '253.292,1832 $'); $USD->setFormat(Zend_Currency::LEFT, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '$ 253.292,1832'); $USD->setFormat(Zend_Currency::STANDARD, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '$ 253.292,1832'); // enable/disable currency symbols & currency names $USD->setFormat(Zend_Currency::NO_SYMBOL, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), 'US Dollar 253.292,1832'); $USD->setFormat(Zend_Currency::USE_SHORTNAME, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), 'USD 253.292,1832'); $USD->setFormat(Zend_Currency::USE_NAME, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), 'US Dollar 253.292,1832'); $USD->setFormat(Zend_Currency::USE_SYMBOL, null, 'de_AT'); $this->assertSame($USD->toCurrency(253292.1832), '$ 253.292,1832'); }
/** * Create Zend_Currency object for current locale * * @param string $currency * @return Zend_Currency */ public function currency($currency) { Varien_Profiler::start('locale/currency'); if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) { $options = array(); try { $currencyObject = new Zend_Currency($currency, $this->getLocale()); } catch (Exception $e) { /** * catch specific exceptions like "Currency 'USD' not found" * - back end falls with specific locals as Malaysia and etc. * * as we can see from Zend framework ticket * http://framework.zend.com/issues/browse/ZF-10038 * zend team is not going to change it behaviour in the near time */ $currencyObject = new Zend_Currency($currency); $options['name'] = $currency; $options['currency'] = $currency; $options['symbol'] = $currency; } $options = new Varien_Object($options); Mage::dispatchEvent('currency_display_options_forming', array('currency_options' => $options, 'base_code' => $currency)); $currencyObject->setFormat($options->toArray()); self::$_currencyCache[$this->getLocaleCode()][$currency] = $currencyObject; } Varien_Profiler::stop('locale/currency'); return self::$_currencyCache[$this->getLocaleCode()][$currency]; }
/** * Muestra el dato de la columna * @param /Model/Entity/xxxx $item * @return array */ protected function _getDataColumn($item) { $this->load->library(array("core/fecha/fecha_conversion")); $salida = array(); foreach ($this->_data['columns'] as $key => $value) { if (count($value) > 0) { if (is_array($value['column_table'])) { $obj = $item; foreach ($value['column_table'] as $i => $metodo) { if ((string) $i == "json") { //fb($value['column_json']); $sha = sha1($obj[$metodo]); if (Zend_Registry::isRegistered($sha)) { $json = Zend_Registry::get($sha); } else { $json = Zend_Json::decode($obj[$metodo]); Zend_Registry::set($sha, $json); } $obj = $json[$value['column_json']]; } else { $obj = $obj[$metodo]; } } $valor = $obj; } else { if (isset($value['column_table']) && isset($item[$value['column_table']])) { $valor = $item[$value['column_table']]; } else { $valor = ""; } } switch ($value['column_type']) { case "date": $fecha = $this->fecha_conversion->fechaToDateTime($valor, $value['column_formato']); if ($fecha instanceof DateTime) { $salida[] = $fecha->format($value['column_formato_salida']); } else { $salida[] = ""; } break; case "html": $salida[] = str_replace("?", $valor, $value["column_html"]); break; case "helper": $this->load->helper($value["column_helper_path"]); $parametros = array(); foreach ($value["column_helper_params"] as $h => $param) { if ((string) $h == "json") { $sha = sha1($valor); if (Zend_Registry::isRegistered($sha)) { $json = Zend_Registry::get($sha); } else { $json = Zend_Json::decode($valor); Zend_Registry::set($sha, $json); } if (isset($json[$param])) { $parametros[] = $json[$param]; } else { $parametros[] = ""; } } else { $parametros[] = $item[$param]; } } $salida[] = call_user_func_array($value["column_helper"], $parametros); break; case "money": if (!is_numeric($valor)) { $valor = 0; } $currency = new Zend_Currency(array('value' => $valor)); $currency->setFormat(array("precision" => 0)); $salida[] = $currency->toCurrency(); break; default: $salida[] = $valor; break; } } } return $salida; }
/** * testing setFormat * */ public function testSetFormat() { $locale = new Zend_Locale('en_US'); $USD = new Zend_Currency('USD','en_US'); $USD->setFormat(array('script' => 'Arab')); $this->assertSame('$ ٥٣,٢٩٢.١٨', $USD->toCurrency(53292.18)); $USD->setFormat(array('script' => 'Arab', 'format' => 'de_AT')); $this->assertSame('$ ٥٣.٢٩٢,١٨', $USD->toCurrency(53292.18)); $USD->setFormat(array('script' => 'Latn', 'format' => 'de_AT')); $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('script' => 'Latn', 'format' => $locale)); $this->assertSame('$ 53,292.18', $USD->toCurrency(53292.18)); // allignment of currency signs $USD->setFormat(array('position' => Zend_Currency::RIGHT, 'format' => 'de_AT')); $this->assertSame('53.292,18 $', $USD->toCurrency(53292.18)); $USD->setFormat(array('position' => Zend_Currency::RIGHT, 'format' => $locale)); $this->assertSame('53,292.18 $', $USD->toCurrency(53292.18)); $USD->setFormat(array('position' => Zend_Currency::LEFT, 'format' => 'de_AT')); $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('position' => Zend_Currency::LEFT, 'format' => $locale)); $this->assertSame('$ 53,292.18', $USD->toCurrency(53292.18)); $USD->setFormat(array('position' => Zend_Currency::STANDARD, 'format' => 'de_AT')); $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('position' => Zend_Currency::STANDARD, 'format' => $locale)); $this->assertSame('$ 53,292.18', $USD->toCurrency(53292.18)); // enable/disable currency symbols & currency names $USD->setFormat(array('display' => Zend_Currency::NO_SYMBOL, 'format' => 'de_AT')); $this->assertSame('53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::NO_SYMBOL, 'format' => $locale)); $this->assertSame('53,292.18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_SHORTNAME, 'format' => 'de_AT')); $this->assertSame('USD 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_SHORTNAME, 'format' => $locale)); $this->assertSame('USD 53,292.18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_NAME, 'format' => 'de_AT')); $this->assertSame('US Dollar 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_NAME, 'format' => $locale)); $this->assertSame('US Dollar 53,292.18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_SYMBOL, 'format' => 'de_AT')); $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18)); $USD->setFormat(array('display' => Zend_Currency::USE_SYMBOL, 'format' => $locale)); $this->assertSame('$ 53,292.18', $USD->toCurrency(53292.18)); }
public function getFormPrice() { $zc = new Zend_Currency(array('value' => $this->_get('price')), strtoupper(Model_Lang::getCurrent())); $zc->setFormat(array('display' => Zend_Currency::NO_SYMBOL)); return $zc->toString(); }