NumberHelper.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Helper;
  16. use Cake\Core\App;
  17. use Cake\Error;
  18. use Cake\Utility\Hash;
  19. use Cake\View\Helper;
  20. use Cake\View\View;
  21. /**
  22. * Number helper library.
  23. *
  24. * Methods to make numbers more readable.
  25. *
  26. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
  27. * @see \Cake\Utility\Number
  28. */
  29. class NumberHelper extends Helper {
  30. /**
  31. * Default config for this class
  32. *
  33. * @var mixed
  34. */
  35. protected $_defaultConfig = [
  36. 'engine' => 'Cake\Utility\Number'
  37. ];
  38. /**
  39. * Cake\Utility\Number instance
  40. *
  41. * @var \Cake\Utility\Number
  42. */
  43. protected $_engine = null;
  44. /**
  45. * Default Constructor
  46. *
  47. * ### Settings:
  48. *
  49. * - `engine` Class name to use to replace Cake\Utility\Number functionality
  50. * The class needs to be placed in the `Utility` directory.
  51. *
  52. * @param View $View The View this helper is being attached to.
  53. * @param array $config Configuration settings for the helper
  54. * @throws \Cake\Error\Exception When the engine class could not be found.
  55. */
  56. public function __construct(View $View, $config = array()) {
  57. parent::__construct($View, $config);
  58. $config = $this->config();
  59. $engineClass = App::classname($config['engine'], 'Utility');
  60. if ($engineClass) {
  61. $this->_engine = new $engineClass($config);
  62. } else {
  63. throw new Error\Exception(sprintf('Class for %s could not be found', $config['engine']));
  64. }
  65. }
  66. /**
  67. * Call methods from Cake\Utility\Number utility class
  68. *
  69. * @param string $method Method to invoke
  70. * @param array $params Array of params for the method.
  71. * @return mixed Whatever is returned by called method, or false on failure
  72. */
  73. public function __call($method, $params) {
  74. return call_user_func_array(array($this->_engine, $method), $params);
  75. }
  76. /**
  77. * Formats a number with a level of precision.
  78. *
  79. * @see \Cake\Utility\Number::precision()
  80. *
  81. * @param float $number A floating point number.
  82. * @param integer $precision The precision of the returned number.
  83. * @return float Formatted float.
  84. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
  85. */
  86. public function precision($number, $precision = 3) {
  87. return $this->_engine->precision($number, $precision);
  88. }
  89. /**
  90. * Returns a formatted-for-humans file size.
  91. *
  92. * @see \Cake\Utility\Number::toReadableSize()
  93. *
  94. * @param integer $size Size in bytes
  95. * @return string Human readable size
  96. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  97. */
  98. public function toReadableSize($size) {
  99. return $this->_engine->toReadableSize($size);
  100. }
  101. /**
  102. * Formats a number into a percentage string.
  103. *
  104. * Options:
  105. *
  106. * - `multiply`: Multiply the input value by 100 for decimal percentages.
  107. *
  108. * @see \Cake\Utility\Number::toPercentage()
  109. *
  110. * @param float $number A floating point number
  111. * @param integer $precision The precision of the returned number
  112. * @param array $options Options
  113. * @return string Percentage string
  114. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
  115. */
  116. public function toPercentage($number, $precision = 2, $options = array()) {
  117. return $this->_engine->toPercentage($number, $precision, $options);
  118. }
  119. /**
  120. * Formats a number into a currency format.
  121. *
  122. * @see \Cake\Utility\Number::format()
  123. *
  124. * @param float $number A floating point number
  125. * @param integer $options If integer then places, if string then before, if (,.-) then use it
  126. * or array with places and before keys
  127. * @return string formatted number
  128. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
  129. */
  130. public function format($number, $options = false) {
  131. return $this->_engine->format($number, $options);
  132. }
  133. /**
  134. * Formats a number into a currency format.
  135. *
  136. * ### Options
  137. *
  138. * - `wholeSymbol` - The currency symbol to use for whole numbers,
  139. * greater than 1, or less than -1.
  140. * - `wholePosition` - The position the whole symbol should be placed
  141. * valid options are 'before' & 'after'.
  142. * - `fractionSymbol` - The currency symbol to use for fractional numbers.
  143. * - `fractionPosition` - The position the fraction symbol should be placed
  144. * valid options are 'before' & 'after'.
  145. * - `before` - The currency symbol to place before whole numbers
  146. * ie. '$'. `before` is an alias for `wholeSymbol`.
  147. * - `after` - The currency symbol to place after decimal numbers
  148. * ie. 'c'. Set to boolean false to use no decimal symbol.
  149. * eg. 0.35 => $0.35. `after` is an alias for `fractionSymbol`
  150. * - `zero` - The text to use for zero values, can be a
  151. * string or a number. ie. 0, 'Free!'
  152. * - `places` - Number of decimal places to use. ie. 2
  153. * - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2.
  154. * - `thousands` - Thousands separator ie. ','
  155. * - `decimals` - Decimal separator symbol ie. '.'
  156. * - `negative` - Symbol for negative numbers. If equal to '()',
  157. * the number will be wrapped with ( and )
  158. * - `escape` - Should the output be escaped for html special characters.
  159. * The default value for this option is controlled by the currency settings.
  160. * By default all currencies contain utf-8 symbols and don't need this changed. If you require
  161. * non HTML encoded symbols you will need to update the settings with the correct bytes.
  162. *
  163. * @see \Cake\Utility\Number::currency()
  164. *
  165. * @param float $number
  166. * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
  167. * set at least 'before' and 'after' options.
  168. * 'USD' is the default currency, use Number::defaultCurrency() to change this default.
  169. * @param array $options
  170. * @return string Number formatted as a currency.
  171. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
  172. */
  173. public function currency($number, $currency = null, $options = array()) {
  174. return $this->_engine->currency($number, $currency, $options);
  175. }
  176. /**
  177. * Add a currency format to the Number helper. Makes reusing
  178. * currency formats easier.
  179. *
  180. * {{{ $this->Number->addFormat('NOK', array('before' => 'Kr. ')); }}}
  181. *
  182. * You can now use `NOK` as a shortform when formatting currency amounts.
  183. *
  184. * {{{ $this->Number->currency($value, 'NOK'); }}}
  185. *
  186. * Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults
  187. * See Cake\Utility\Number::currency() for more information on the various options and their function.
  188. *
  189. * @see \Cake\Utility\Number::addFormat()
  190. *
  191. * @param string $formatName The format name to be used in the future.
  192. * @param array $options The array of options for this format.
  193. * @return void
  194. * @see NumberHelper::currency()
  195. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
  196. */
  197. public function addFormat($formatName, $options) {
  198. return $this->_engine->addFormat($formatName, $options);
  199. }
  200. /**
  201. * Getter/setter for default currency
  202. *
  203. * @see \Cake\Utility\Number::defaultCurrency()
  204. *
  205. * @param string $currency The currency to be used in the future.
  206. * @return void
  207. * @see NumberHelper::currency()
  208. */
  209. public function defaultCurrency($currency) {
  210. return $this->_engine->defaultCurrency($currency);
  211. }
  212. /**
  213. * Event listeners.
  214. *
  215. * @return array
  216. */
  217. public function implementedEvents() {
  218. return [];
  219. }
  220. }