diff --git a/src/Utility/Number.php b/src/Utility/Number.php index 40a7f909f7c..af01398b920 100644 --- a/src/Utility/Number.php +++ b/src/Utility/Number.php @@ -206,7 +206,6 @@ public static function format($value, array $options = []) { * @param float $value A floating point number * @param array $options Options list. * @return string formatted delta - * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::formatDelta */ public static function formatDelta($value, array $options = array()) { $options += ['places' => 0]; diff --git a/src/View/Helper/NumberHelper.php b/src/View/Helper/NumberHelper.php index 70179489d16..840ba52e669 100644 --- a/src/View/Helper/NumberHelper.php +++ b/src/View/Helper/NumberHelper.php @@ -125,16 +125,26 @@ public function toPercentage($number, $precision = 2, array $options = array()) } /** - * Formats a number into a currency format. + * Formats a number into the correct locale format * - * @param float $number A floating point number. - * @param array $options Array of options. + * Options: + * + * - `places` - Minimim number or decimals to use, e.g 0 + * - `precision` - Maximum Number of decimal places to use, e.g. 2 + * - `locale` - The locale name to use for formatting the number, e.g. fr_FR + * - `before` - The string to place before whole numbers, e.g. '[' + * - `after` - The string to place after decimal numbers, e.g. ']' + * - `escape` - Whether or not to escape html in resulting string + * + * @param float $value A floating point number. + * @param array $options An array with options. * @return string Formatted number - * @see \Cake\Utility\Number::format() * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format */ public function format($number, array $options = []) { - return $this->_engine->format($number, $options); + $formatted = $this->_engine->format($number, $options); + $options += ['escape' => true]; + return $options['escape'] ? h($formatted): $formatted; } /** @@ -142,73 +152,36 @@ public function format($number, array $options = []) { * * ### Options * - * - `wholeSymbol` - The currency symbol to use for whole numbers, - * greater than 1, or less than -1. - * - `wholePosition` - The position the whole symbol should be placed - * valid options are 'before' & 'after'. + * - `locale` - The locale name to use for formatting the number, e.g. fr_FR * - `fractionSymbol` - The currency symbol to use for fractional numbers. * - `fractionPosition` - The position the fraction symbol should be placed - * valid options are 'before' & 'after'. - * - `before` - The currency symbol to place before whole numbers - * ie. '$'. `before` is an alias for `wholeSymbol`. - * - `after` - The currency symbol to place after decimal numbers - * ie. 'c'. Set to boolean false to use no decimal symbol. - * eg. 0.35 => $0.35. `after` is an alias for `fractionSymbol` - * - `zero` - The text to use for zero values, can be a - * string or a number. ie. 0, 'Free!' - * - `places` - Number of decimal places to use. ie. 2 - * - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2. - * - `thousands` - Thousands separator ie. ',' - * - `decimals` - Decimal separator symbol ie. '.' - * - `negative` - Symbol for negative numbers. If equal to '()', - * the number will be wrapped with ( and ) - * - `escape` - Should the output be escaped for html special characters. - * The default value for this option is controlled by the currency settings. - * By default all currencies contain utf-8 symbols and don't need this changed. If you require - * non HTML encoded symbols you will need to update the settings with the correct bytes. - * - * @param float $number Number to format. - * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise - * set at least 'before' and 'after' options. - * 'USD' is the default currency, use Number::defaultCurrency() to change this default. + * valid options are 'before' & 'after'. + * - `before` - Text to display before the rendered number + * - `after` - Text to display after the rendered number + * - `zero` - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!' + * - `places` - Number of decimal places to use. e.g. 2 + * - `precision` - Maximum Number of decimal places to use, e.g. 2 + * - `pattern` - An ICU number patter to use for formatting the number. e.g #,###.00 + * - `escape` - Whether or not to escape html in resulting string + * + * @param float $value Value to format. + * @param string $currency International currency name such as 'USD', 'EUR', 'JPY', 'CAD' * @param array $options Options list. * @return string Number formatted as a currency. - * @see Cake\Utility\Number::currency() - * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency */ public function currency($number, $currency = null, array $options = array()) { - return $this->_engine->currency($number, $currency, $options); - } - -/** - * Add a currency format to the Number helper. Makes reusing - * currency formats easier. - * - * {{{ $this->Number->addFormat('NOK', array('before' => 'Kr. ')); }}} - * - * You can now use `NOK` as a shortform when formatting currency amounts. - * - * {{{ $this->Number->currency($value, 'NOK'); }}} - * - * Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults - * See Cake\Utility\Number::currency() for more information on the various options and their function. - * - * @param string $formatName The format name to be used in the future. - * @param array $options The array of options for this format. - * @return void - * @see \Cake\Utility\Number::addFormat() - * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat - */ - public function addFormat($formatName, array $options) { - return $this->_engine->addFormat($formatName, $options); + $formatted = $this->_engine->currency($number, $currency, $options); + $options += ['escape' => true]; + return $options['escape'] ? h($formatted): $formatted; } /** * Getter/setter for default currency * - * @param string $currency The currency to be used in the future. + * @param string|boolean $currency Default currency string to be used by currency() + * if $currency argument is not provided. If boolean false is passed, it will clear the + * currently stored value * @return string Currency - * @see \Cake\Utility\Number::defaultCurrency() */ public function defaultCurrency($currency) { return $this->_engine->defaultCurrency($currency);