NumberHelper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Number Helper.
  4. *
  5. * Methods to make numbers more readable.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Helper
  17. * @since CakePHP(tm) v 0.10.0.1076
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('CakeNumber', 'Utility');
  21. App::uses('AppHelper', 'View/Helper');
  22. App::uses('Hash', 'Utility');
  23. /**
  24. * Number helper library.
  25. *
  26. * Methods to make numbers more readable.
  27. *
  28. * @package Cake.View.Helper
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
  30. * @see CakeNumber
  31. */
  32. class NumberHelper extends AppHelper {
  33. /**
  34. * CakeNumber instance
  35. *
  36. * @var CakeNumber
  37. */
  38. protected $_engine = null;
  39. /**
  40. * Default Constructor
  41. *
  42. * ### Settings:
  43. *
  44. * - `engine` Class name to use to replace CakeNumber functionality
  45. * The class needs to be placed in the `Utility` directory.
  46. *
  47. * @param View $View The View this helper is being attached to.
  48. * @param array $settings Configuration settings for the helper
  49. * @throws CakeException When the engine class could not be found.
  50. */
  51. public function __construct(View $View, $settings = array()) {
  52. $settings = Hash::merge(array('engine' => 'CakeNumber'), $settings);
  53. parent::__construct($View, $settings);
  54. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  55. App::uses($engineClass, $plugin . 'Utility');
  56. if (class_exists($engineClass)) {
  57. $this->_engine = new $engineClass($settings);
  58. } else {
  59. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  60. }
  61. }
  62. /**
  63. * Call methods from CakeNumber utility class
  64. *
  65. * @param string $method Method to call.
  66. * @param array $params Parameters to pass to method.
  67. * @return mixed Whatever is returned by called method, or false on failure
  68. */
  69. public function __call($method, $params) {
  70. return call_user_func_array(array($this->_engine, $method), $params);
  71. }
  72. /**
  73. * Formats a number with a level of precision.
  74. *
  75. * @param float $number A floating point number.
  76. * @param int $precision The precision of the returned number.
  77. * @return float Formatted float.
  78. * @see CakeNumber::precision()
  79. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
  80. */
  81. public function precision($number, $precision = 3) {
  82. return $this->_engine->precision($number, $precision);
  83. }
  84. /**
  85. * Returns a formatted-for-humans file size.
  86. *
  87. * @param int $size Size in bytes
  88. * @return string Human readable size
  89. * @see CakeNumber::toReadableSize()
  90. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  91. */
  92. public function toReadableSize($size) {
  93. return $this->_engine->toReadableSize($size);
  94. }
  95. /**
  96. * Formats a number into a percentage string.
  97. *
  98. * Options:
  99. *
  100. * - `multiply`: Multiply the input value by 100 for decimal percentages.
  101. *
  102. * @param float $number A floating point number
  103. * @param int $precision The precision of the returned number
  104. * @param array $options Options
  105. * @return string Percentage string
  106. * @see CakeNumber::toPercentage()
  107. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
  108. */
  109. public function toPercentage($number, $precision = 2, $options = array()) {
  110. return $this->_engine->toPercentage($number, $precision, $options);
  111. }
  112. /**
  113. * Formats a number into a currency format.
  114. *
  115. * @param float $number A floating point number
  116. * @param int $options If integer then places, if string then before, if (,.-) then use it
  117. * or array with places and before keys
  118. * @return string formatted number
  119. * @see CakeNumber::format()
  120. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
  121. */
  122. public function format($number, $options = false) {
  123. return $this->_engine->format($number, $options);
  124. }
  125. /**
  126. * Formats a number into a currency format.
  127. *
  128. * @param float $number Number to format.
  129. * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
  130. * set at least 'before' and 'after' options.
  131. * 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
  132. * @param array $options Options list.
  133. * @return string Number formatted as a currency.
  134. * @see CakeNumber::currency()
  135. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
  136. */
  137. public function currency($number, $currency = null, $options = array()) {
  138. return $this->_engine->currency($number, $currency, $options);
  139. }
  140. /**
  141. * Add a currency format to the Number helper. Makes reusing
  142. * currency formats easier.
  143. *
  144. * {{{ $this->Number->addFormat('NOK', array('before' => 'Kr. ')); }}}
  145. *
  146. * You can now use `NOK` as a shortform when formatting currency amounts.
  147. *
  148. * {{{ $this->Number->currency($value, 'NOK'); }}}
  149. *
  150. * Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults
  151. * See Cake\Utility\Number::currency() for more information on the various options and their function.
  152. *
  153. * @param string $formatName The format name to be used in the future.
  154. * @param array $options The array of options for this format.
  155. * @return void
  156. * @see CakeNumber::addFormat()
  157. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
  158. */
  159. public function addFormat($formatName, $options) {
  160. return $this->_engine->addFormat($formatName, $options);
  161. }
  162. /**
  163. * Getter/setter for default currency
  164. *
  165. * @param string $currency The currency to be used in the future.
  166. * @return string Currency
  167. * @see CakeNumber::defaultCurrency()
  168. */
  169. public function defaultCurrency($currency) {
  170. return $this->_engine->defaultCurrency($currency);
  171. }
  172. }