PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/number.php

http://github.com/Datawalke/Coordino
PHP | 257 lines | 110 code | 21 blank | 126 comment | 21 complexity | 5497f5b6afc36a7452157a5d70699f8f MD5 | raw file
  1. <?php
  2. /**
  3. * Number Helper.
  4. *
  5. * Methods to make numbers more readable.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs.view.helpers
  19. * @since CakePHP(tm) v 0.10.0.1076
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Number helper library.
  24. *
  25. * Methods to make numbers more readable.
  26. *
  27. * @package cake
  28. * @subpackage cake.cake.libs.view.helpers
  29. * @link http://book.cakephp.org/view/1452/Number
  30. */
  31. class NumberHelper extends AppHelper {
  32. /**
  33. * Currencies supported by the helper. You can add additional currency formats
  34. * with NumberHelper::addFormat
  35. *
  36. * @var array
  37. * @access protected
  38. */
  39. var $_currencies = array(
  40. 'USD' => array(
  41. 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  42. 'decimals' => '.', 'negative' => '()', 'escape' => true
  43. ),
  44. 'GBP' => array(
  45. 'before'=>'&#163;', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  46. 'decimals' => '.', 'negative' => '()','escape' => false
  47. ),
  48. 'EUR' => array(
  49. 'before'=>'&#8364;', 'after' => false, 'zero' => 0, 'places' => 2, 'thousands' => '.',
  50. 'decimals' => ',', 'negative' => '()', 'escape' => false
  51. )
  52. );
  53. /**
  54. * Default options for currency formats
  55. *
  56. * @var array
  57. * @access protected
  58. */
  59. var $_currencyDefaults = array(
  60. 'before'=>'', 'after' => false, 'zero' => '0', 'places' => 2, 'thousands' => ',',
  61. 'decimals' => '.','negative' => '()', 'escape' => true
  62. );
  63. /**
  64. * Formats a number with a level of precision.
  65. *
  66. * @param float $number A floating point number.
  67. * @param integer $precision The precision of the returned number.
  68. * @return float Formatted float.
  69. * @access public
  70. * @link http://book.cakephp.org/view/1454/precision
  71. */
  72. function precision($number, $precision = 3) {
  73. return sprintf("%01.{$precision}f", $number);
  74. }
  75. /**
  76. * Returns a formatted-for-humans file size.
  77. *
  78. * @param integer $length Size in bytes
  79. * @return string Human readable size
  80. * @access public
  81. * @link http://book.cakephp.org/view/1456/toReadableSize
  82. */
  83. function toReadableSize($size) {
  84. switch (true) {
  85. case $size < 1024:
  86. return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
  87. case round($size / 1024) < 1024:
  88. return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
  89. case round($size / 1024 / 1024, 2) < 1024:
  90. return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
  91. case round($size / 1024 / 1024 / 1024, 2) < 1024:
  92. return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
  93. default:
  94. return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
  95. }
  96. }
  97. /**
  98. * Formats a number into a percentage string.
  99. *
  100. * @param float $number A floating point number
  101. * @param integer $precision The precision of the returned number
  102. * @return string Percentage string
  103. * @access public
  104. * @link http://book.cakephp.org/view/1455/toPercentage
  105. */
  106. function toPercentage($number, $precision = 2) {
  107. return $this->precision($number, $precision) . '%';
  108. }
  109. /**
  110. * Formats a number into a currency format.
  111. *
  112. * @param float $number A floating point number
  113. * @param integer $options if int then places, if string then before, if (,.-) then use it
  114. * or array with places and before keys
  115. * @return string formatted number
  116. * @access public
  117. * @link http://book.cakephp.org/view/1457/format
  118. */
  119. function format($number, $options = false) {
  120. $places = 0;
  121. if (is_int($options)) {
  122. $places = $options;
  123. }
  124. $separators = array(',', '.', '-', ':');
  125. $before = $after = null;
  126. if (is_string($options) && !in_array($options, $separators)) {
  127. $before = $options;
  128. }
  129. $thousands = ',';
  130. if (!is_array($options) && in_array($options, $separators)) {
  131. $thousands = $options;
  132. }
  133. $decimals = '.';
  134. if (!is_array($options) && in_array($options, $separators)) {
  135. $decimals = $options;
  136. }
  137. $escape = true;
  138. if (is_array($options)) {
  139. $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
  140. extract($options);
  141. }
  142. $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
  143. if ($escape) {
  144. return h($out);
  145. }
  146. return $out;
  147. }
  148. /**
  149. * Formats a number into a currency format.
  150. *
  151. * ### Options
  152. *
  153. * - `before` - The currency symbol to place before whole numbers ie. '$'
  154. * - `after` - The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to
  155. * use no decimal symbol. eg. 0.35 => $0.35.
  156. * - `zero` - The text to use for zero values, can be a string or a number. ie. 0, 'Free!'
  157. * - `places` - Number of decimal places to use. ie. 2
  158. * - `thousands` - Thousands separator ie. ','
  159. * - `decimals` - Decimal separator symbol ie. '.'
  160. * - `negative` - Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and )
  161. * - `escape` - Should the output be htmlentity escaped? Defaults to true
  162. *
  163. * @param float $number
  164. * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
  165. * set at least 'before' and 'after' options.
  166. * @param array $options
  167. * @return string Number formatted as a currency.
  168. * @access public
  169. * @link http://book.cakephp.org/view/1453/currency
  170. */
  171. function currency($number, $currency = 'USD', $options = array()) {
  172. $default = $this->_currencyDefaults;
  173. if (isset($this->_currencies[$currency])) {
  174. $default = $this->_currencies[$currency];
  175. } elseif (is_string($currency)) {
  176. $options['before'] = $currency;
  177. }
  178. $options = array_merge($default, $options);
  179. $result = null;
  180. if ($number == 0 ) {
  181. if ($options['zero'] !== 0 ) {
  182. return $options['zero'];
  183. }
  184. $options['after'] = null;
  185. } elseif ($number < 1 && $number > -1 ) {
  186. if ($options['after'] !== false) {
  187. $multiply = intval('1' . str_pad('', $options['places'], '0'));
  188. $number = $number * $multiply;
  189. $options['before'] = null;
  190. $options['places'] = null;
  191. }
  192. } elseif (empty($options['before'])) {
  193. $options['before'] = null;
  194. } else {
  195. $options['after'] = null;
  196. }
  197. $abs = abs($number);
  198. $result = $this->format($abs, $options);
  199. if ($number < 0 ) {
  200. if ($options['negative'] == '()') {
  201. $result = '(' . $result .')';
  202. } else {
  203. $result = $options['negative'] . $result;
  204. }
  205. }
  206. return $result;
  207. }
  208. /**
  209. * Add a currency format to the Number helper. Makes reusing
  210. * currency formats easier.
  211. *
  212. * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
  213. *
  214. * You can now use `NOK` as a shortform when formatting currency amounts.
  215. *
  216. * {{{ $number->currency($value, 'NOK'); }}}
  217. *
  218. * Added formats are merged with the following defaults.
  219. *
  220. * {{{
  221. * array(
  222. * 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  223. * 'decimals' => '.', 'negative' => '()', 'escape' => true
  224. * )
  225. * }}}
  226. *
  227. * @param string $formatName The format name to be used in the future.
  228. * @param array $options The array of options for this format.
  229. * @return void
  230. * @see NumberHelper::currency()
  231. * @access public
  232. */
  233. function addFormat($formatName, $options) {
  234. $this->_currencies[$formatName] = $options + $this->_currencyDefaults;
  235. }
  236. }