Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving NumberHelper to CakeNumber

  • Loading branch information...
commit 6fdaef10c898fde85ebaf4385a5814a9eb8171c6 1 parent 04cdef9
@rchavik rchavik authored
Showing with 287 additions and 157 deletions.
  1. +254 −0 lib/Cake/Utility/CakeNumber.php
  2. +33 −157 lib/Cake/View/Helper/NumberHelper.php
View
254 lib/Cake/Utility/CakeNumber.php
@@ -0,0 +1,254 @@
+<?php
+/**
+ * CakeNumber Utility.
+ *
+ * Methods to make numbers more readable.
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package Cake.Utility
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+/**
+ * Number helper library.
+ *
+ * Methods to make numbers more readable.
+ *
+ * @package Cake.Utility
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
+ */
+class CakeNumber {
+
+/**
+ * Currencies supported by the helper. You can add additional currency formats
+ * with CakeNumber::addFormat
+ *
+ * @var array
+ */
+ protected $_currencies = array(
+ 'USD' => array(
+ 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
+ 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
+ ),
+ 'GBP' => array(
+ 'wholeSymbol' => '&#163;', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
+ 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false
+ ),
+ 'EUR' => array(
+ 'wholeSymbol' => '&#8364;', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
+ 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false
+ )
+ );
+
+/**
+ * Default options for currency formats
+ *
+ * @var array
+ */
+ protected $_currencyDefaults = array(
+ 'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
+ 'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
+ );
+
+/**
+ * Formats a number with a level of precision.
+ *
+ * @param float $number A floating point number.
+ * @param integer $precision The precision of the returned number.
+ * @return float Formatted float.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
+ */
+ public function precision($number, $precision = 3) {
+ return sprintf("%01.{$precision}f", $number);
+ }
+
+/**
+ * Returns a formatted-for-humans file size.
+ *
+ * @param integer $size Size in bytes
+ * @return string Human readable size
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
+ */
+ public function toReadableSize($size) {
+ switch (true) {
+ case $size < 1024:
+ return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
+ case round($size / 1024) < 1024:
+ return __d('cake', '%d KB', $this->precision($size / 1024, 0));
+ case round($size / 1024 / 1024, 2) < 1024:
+ return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
+ case round($size / 1024 / 1024 / 1024, 2) < 1024:
+ return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
+ default:
+ return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
+ }
+ }
+
+/**
+ * Formats a number into a percentage string.
+ *
+ * @param float $number A floating point number
+ * @param integer $precision The precision of the returned number
+ * @return string Percentage string
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
+ */
+ public function toPercentage($number, $precision = 2) {
+ return $this->precision($number, $precision) . '%';
+ }
+
+/**
+ * Formats a number into a currency format.
+ *
+ * @param float $number A floating point number
+ * @param integer $options if int then places, if string then before, if (,.-) then use it
+ * or array with places and before keys
+ * @return string formatted number
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
+ */
+ public function format($number, $options = false) {
+ $places = 0;
+ if (is_int($options)) {
+ $places = $options;
+ }
+
+ $separators = array(',', '.', '-', ':');
+
+ $before = $after = null;
+ if (is_string($options) && !in_array($options, $separators)) {
+ $before = $options;
+ }
+ $thousands = ',';
+ if (!is_array($options) && in_array($options, $separators)) {
+ $thousands = $options;
+ }
+ $decimals = '.';
+ if (!is_array($options) && in_array($options, $separators)) {
+ $decimals = $options;
+ }
+
+ $escape = true;
+ if (is_array($options)) {
+ $options = array_merge(array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
+ extract($options);
+ }
+
+ $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
+
+ if ($escape) {
+ return h($out);
+ }
+ return $out;
+ }
+
+/**
+ * Formats a number into a currency format.
+ *
+ * ### Options
+ *
+ * - `before` - The currency symbol to place before whole numbers ie. '$'
+ * - `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.
+ * - `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
+ * - `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 htmlentity escaped? Defaults to true
+ *
+ * @param float $number
+ * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
+ * set at least 'before' and 'after' options.
+ * @param array $options
+ * @return string Number formatted as a currency.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
+ */
+ public function currency($number, $currency = 'USD', $options = array()) {
+ $default = $this->_currencyDefaults;
+
+ if (isset($this->_currencies[$currency])) {
+ $default = $this->_currencies[$currency];
+ } elseif (is_string($currency)) {
+ $options['before'] = $currency;
+ }
+
+ $options = array_merge($default, $options);
+
+ if (isset($options['before']) && $options['before'] !== '') {
+ $options['wholeSymbol'] = $options['before'];
+ }
+ if (isset($options['after']) && !$options['after'] !== '') {
+ $options['fractionSymbol'] = $options['after'];
+ }
+
+ $result = $options['before'] = $options['after'] = null;
+
+ $symbolKey = 'whole';
+ if ($number == 0 ) {
+ if ($options['zero'] !== 0 ) {
+ return $options['zero'];
+ }
+ } elseif ($number < 1 && $number > -1 ) {
+ if ($options['fractionSymbol'] !== false) {
+ $multiply = intval('1' . str_pad('', $options['places'], '0'));
+ $number = $number * $multiply;
+ $options['places'] = null;
+ $symbolKey = 'fraction';
+ }
+ }
+
+ $position = $options[$symbolKey.'Position'] != 'after' ? 'before' : 'after';
+ $options[$position] = $options[$symbolKey.'Symbol'];
+
+ $abs = abs($number);
+ $result = $this->format($abs, $options);
+
+ if ($number < 0 ) {
+ if ($options['negative'] == '()') {
+ $result = '(' . $result .')';
+ } else {
+ $result = $options['negative'] . $result;
+ }
+ }
+ return $result;
+ }
+
+/**
+ * Add a currency format to the Number helper. Makes reusing
+ * currency formats easier.
+ *
+ * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
+ *
+ * You can now use `NOK` as a shortform when formatting currency amounts.
+ *
+ * {{{ $number->currency($value, 'NOK'); }}}
+ *
+ * Added formats are merged with the following defaults.
+ *
+ * {{{
+ * array(
+ * 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
+ * 'decimals' => '.', 'negative' => '()', 'escape' => true
+ * )
+ * }}}
+ *
+ * @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 NumberHelper::currency()
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
+ */
+ public function addFormat($formatName, $options) {
+ $this->_currencies[$formatName] = $options + $this->_currencyDefaults;
+ }
+
+}
View
190 lib/Cake/View/Helper/NumberHelper.php
@@ -19,6 +19,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
+App::uses('CakeNumber', 'Utility');
App::uses('AppHelper', 'View/Helper');
/**
@@ -28,42 +29,33 @@
*
* @package Cake.View.Helper
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
+ * @see CakeNumber
*/
class NumberHelper extends AppHelper {
-/**
- * Currencies supported by the helper. You can add additional currency formats
- * with NumberHelper::addFormat
- *
- * @var array
- */
- protected $_currencies = array(
- 'USD' => array(
- 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
- 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
- ),
- 'GBP' => array(
- 'wholeSymbol' => '&#163;', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
- 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false
- ),
- 'EUR' => array(
- 'wholeSymbol' => '&#8364;', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
- 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false
- )
- );
+ /** CakeNumber instance
+ */
+ protected $_CakeNumber = null;
+
+ /* Default Constructor
+ *
+ * @param View $View The View this helper is being attached to.
+ * @param array $settings Configuration settings for the helper
+ */
+ function __construct(View $View, $settings = array()) {
+ parent::__construct($View, $settings);
+ $this->_CakeNumber = new CakeNumber();
+ }
-/**
- * Default options for currency formats
- *
- * @var array
- */
- protected $_currencyDefaults = array(
- 'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
- 'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
- );
+ /**
+ * Call methods from CakeNumber utility class
+ */
+ function __call($method, $params) {
+ return call_user_func_array(array($this->_CakeNumber, $method), $params);
+ }
/**
- * Formats a number with a level of precision.
+ * @see: CakeNumber::precision()
*
* @param float $number A floating point number.
* @param integer $precision The precision of the returned number.
@@ -71,33 +63,22 @@ class NumberHelper extends AppHelper {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
*/
public function precision($number, $precision = 3) {
- return sprintf("%01.{$precision}f", $number);
+ return $this->_CakeNumber->precision($number, $precision);
}
/**
- * Returns a formatted-for-humans file size.
+ * @see: CakeNumber::toReadableSize()
*
* @param integer $size Size in bytes
* @return string Human readable size
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
*/
public function toReadableSize($size) {
- switch (true) {
- case $size < 1024:
- return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
- case round($size / 1024) < 1024:
- return __d('cake', '%d KB', $this->precision($size / 1024, 0));
- case round($size / 1024 / 1024, 2) < 1024:
- return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
- case round($size / 1024 / 1024 / 1024, 2) < 1024:
- return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
- default:
- return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
- }
+ return $this->_CakeNumber->toReadableSize($size);
}
/**
- * Formats a number into a percentage string.
+ * @see: CakeNumber::toPercentage()
*
* @param float $number A floating point number
* @param integer $precision The precision of the returned number
@@ -105,11 +86,11 @@ public function toReadableSize($size) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
*/
public function toPercentage($number, $precision = 2) {
- return $this->precision($number, $precision) . '%';
+ return $this->_CakeNumber->toPercentage($number, $precision);
}
/**
- * Formats a number into a currency format.
+ * @see: CakeNumber::format()
*
* @param float $number A floating point number
* @param integer $options if int then places, if string then before, if (,.-) then use it
@@ -118,54 +99,11 @@ public function toPercentage($number, $precision = 2) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
public function format($number, $options = false) {
- $places = 0;
- if (is_int($options)) {
- $places = $options;
- }
-
- $separators = array(',', '.', '-', ':');
-
- $before = $after = null;
- if (is_string($options) && !in_array($options, $separators)) {
- $before = $options;
- }
- $thousands = ',';
- if (!is_array($options) && in_array($options, $separators)) {
- $thousands = $options;
- }
- $decimals = '.';
- if (!is_array($options) && in_array($options, $separators)) {
- $decimals = $options;
- }
-
- $escape = true;
- if (is_array($options)) {
- $options = array_merge(array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
- extract($options);
- }
-
- $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
-
- if ($escape) {
- return h($out);
- }
- return $out;
+ return $this->_CakeNumber->format($number, $options);
}
/**
- * Formats a number into a currency format.
- *
- * ### Options
- *
- * - `before` - The currency symbol to place before whole numbers ie. '$'
- * - `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.
- * - `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
- * - `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 htmlentity escaped? Defaults to true
+ * @see: CakeNumber::currency()
*
* @param float $number
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
@@ -175,73 +113,11 @@ public function format($number, $options = false) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
public function currency($number, $currency = 'USD', $options = array()) {
- $default = $this->_currencyDefaults;
-
- if (isset($this->_currencies[$currency])) {
- $default = $this->_currencies[$currency];
- } elseif (is_string($currency)) {
- $options['before'] = $currency;
- }
-
- $options = array_merge($default, $options);
-
- if (isset($options['before']) && $options['before'] !== '') {
- $options['wholeSymbol'] = $options['before'];
- }
- if (isset($options['after']) && !$options['after'] !== '') {
- $options['fractionSymbol'] = $options['after'];
- }
-
- $result = $options['before'] = $options['after'] = null;
-
- $symbolKey = 'whole';
- if ($number == 0 ) {
- if ($options['zero'] !== 0 ) {
- return $options['zero'];
- }
- } elseif ($number < 1 && $number > -1 ) {
- if ($options['fractionSymbol'] !== false) {
- $multiply = intval('1' . str_pad('', $options['places'], '0'));
- $number = $number * $multiply;
- $options['places'] = null;
- $symbolKey = 'fraction';
- }
- }
-
- $position = $options[$symbolKey.'Position'] != 'after' ? 'before' : 'after';
- $options[$position] = $options[$symbolKey.'Symbol'];
-
- $abs = abs($number);
- $result = $this->format($abs, $options);
-
- if ($number < 0 ) {
- if ($options['negative'] == '()') {
- $result = '(' . $result .')';
- } else {
- $result = $options['negative'] . $result;
- }
- }
- return $result;
+ return $this->_CakeNumber->currency($number, $currency, $options);
}
/**
- * Add a currency format to the Number helper. Makes reusing
- * currency formats easier.
- *
- * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
- *
- * You can now use `NOK` as a shortform when formatting currency amounts.
- *
- * {{{ $number->currency($value, 'NOK'); }}}
- *
- * Added formats are merged with the following defaults.
- *
- * {{{
- * array(
- * 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
- * 'decimals' => '.', 'negative' => '()', 'escape' => true
- * )
- * }}}
+ * @see: CakeNumber::addFormat()
*
* @param string $formatName The format name to be used in the future.
* @param array $options The array of options for this format.
@@ -250,7 +126,7 @@ public function currency($number, $currency = 'USD', $options = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
*/
public function addFormat($formatName, $options) {
- $this->_currencies[$formatName] = $options + $this->_currencyDefaults;
+ return $this->_CakeNumber->addFormat($formatName, $options);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.