NumberHelper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Helper;
  16. use Cake\Core\App;
  17. use Cake\Core\Exception\Exception;
  18. use Cake\View\Helper;
  19. use Cake\View\View;
  20. /**
  21. * Number helper library.
  22. *
  23. * Methods to make numbers more readable.
  24. *
  25. * @link https://book.cakephp.org/3/en/views/helpers/number.html
  26. * @see \Cake\I18n\Number
  27. */
  28. class NumberHelper extends Helper
  29. {
  30. /**
  31. * Default config for this class
  32. *
  33. * @var array
  34. */
  35. protected $_defaultConfig = [
  36. 'engine' => 'Cake\I18n\Number',
  37. ];
  38. /**
  39. * Cake\I18n\Number instance
  40. *
  41. * @var \Cake\I18n\Number
  42. */
  43. protected $_engine;
  44. /**
  45. * Default Constructor
  46. *
  47. * ### Settings:
  48. *
  49. * - `engine` Class name to use to replace Cake\I18n\Number functionality
  50. * The class needs to be placed in the `Utility` directory.
  51. *
  52. * @param \Cake\View\View $View The View this helper is being attached to.
  53. * @param array $config Configuration settings for the helper
  54. * @throws \Cake\Core\Exception\Exception When the engine class could not be found.
  55. */
  56. public function __construct(View $View, array $config = [])
  57. {
  58. parent::__construct($View, $config);
  59. $config = $this->_config;
  60. $engineClass = App::className($config['engine'], 'Utility');
  61. if ($engineClass) {
  62. $this->_engine = new $engineClass($config);
  63. } else {
  64. throw new Exception(sprintf('Class for %s could not be found', $config['engine']));
  65. }
  66. }
  67. /**
  68. * Call methods from Cake\I18n\Number utility class
  69. *
  70. * @param string $method Method to invoke
  71. * @param array $params Array of params for the method.
  72. * @return mixed Whatever is returned by called method, or false on failure
  73. */
  74. public function __call($method, $params)
  75. {
  76. return call_user_func_array([$this->_engine, $method], $params);
  77. }
  78. /**
  79. * Formats a number with a level of precision.
  80. *
  81. * @param float $number A floating point number.
  82. * @param int $precision The precision of the returned number.
  83. * @param array $options Additional options.
  84. * @return string Formatted float.
  85. * @see \Cake\I18n\Number::precision()
  86. * @link https://book.cakephp.org/3/en/views/helpers/number.html#formatting-floating-point-numbers
  87. */
  88. public function precision($number, $precision = 3, array $options = [])
  89. {
  90. return $this->_engine->precision($number, $precision, $options);
  91. }
  92. /**
  93. * Returns a formatted-for-humans file size.
  94. *
  95. * @param int $size Size in bytes
  96. * @return string Human readable size
  97. * @see \Cake\I18n\Number::toReadableSize()
  98. * @link https://book.cakephp.org/3/en/views/helpers/number.html#interacting-with-human-readable-values
  99. */
  100. public function toReadableSize($size)
  101. {
  102. return $this->_engine->toReadableSize($size);
  103. }
  104. /**
  105. * Formats a number into a percentage string.
  106. *
  107. * Options:
  108. *
  109. * - `multiply`: Multiply the input value by 100 for decimal percentages.
  110. *
  111. * @param float $number A floating point number
  112. * @param int $precision The precision of the returned number
  113. * @param array $options Options
  114. * @return string Percentage string
  115. * @see \Cake\I18n\Number::toPercentage()
  116. * @link https://book.cakephp.org/3/en/views/helpers/number.html#formatting-percentages
  117. */
  118. public function toPercentage($number, $precision = 2, array $options = [])
  119. {
  120. return $this->_engine->toPercentage($number, $precision, $options);
  121. }
  122. /**
  123. * Formats a number into the correct locale format
  124. *
  125. * Options:
  126. *
  127. * - `places` - Minimum number or decimals to use, e.g 0
  128. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  129. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  130. * - `before` - The string to place before whole numbers, e.g. '['
  131. * - `after` - The string to place after decimal numbers, e.g. ']'
  132. * - `escape` - Whether or not to escape html in resulting string
  133. *
  134. * @param float $number A floating point number.
  135. * @param array $options An array with options.
  136. * @return string Formatted number
  137. * @link https://book.cakephp.org/3/en/views/helpers/number.html#formatting-numbers
  138. */
  139. public function format($number, array $options = [])
  140. {
  141. $formatted = $this->_engine->format($number, $options);
  142. $options += ['escape' => true];
  143. return $options['escape'] ? h($formatted) : $formatted;
  144. }
  145. /**
  146. * Formats a number into a currency format.
  147. *
  148. * ### Options
  149. *
  150. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  151. * - `fractionSymbol` - The currency symbol to use for fractional numbers.
  152. * - `fractionPosition` - The position the fraction symbol should be placed
  153. * valid options are 'before' & 'after'.
  154. * - `before` - Text to display before the rendered number
  155. * - `after` - Text to display after the rendered number
  156. * - `zero` - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
  157. * - `places` - Number of decimal places to use. e.g. 2
  158. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  159. * - `pattern` - An ICU number pattern to use for formatting the number. e.g #,##0.00
  160. * - `useIntlCode` - Whether or not to replace the currency symbol with the international
  161. * currency code.
  162. * - `escape` - Whether or not to escape html in resulting string
  163. *
  164. * @param float $number Value to format.
  165. * @param string|null $currency International currency name such as 'USD', 'EUR', 'JPY', 'CAD'
  166. * @param array $options Options list.
  167. * @return string Number formatted as a currency.
  168. */
  169. public function currency($number, $currency = null, array $options = [])
  170. {
  171. $formatted = $this->_engine->currency($number, $currency, $options);
  172. $options += ['escape' => true];
  173. return $options['escape'] ? h($formatted) : $formatted;
  174. }
  175. /**
  176. * Formats a number into the correct locale format to show deltas (signed differences in value).
  177. *
  178. * ### Options
  179. *
  180. * - `places` - Minimum number or decimals to use, e.g 0
  181. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  182. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  183. * - `before` - The string to place before whole numbers, e.g. '['
  184. * - `after` - The string to place after decimal numbers, e.g. ']'
  185. * - `escape` - Set to false to prevent escaping
  186. *
  187. * @param float $value A floating point number
  188. * @param array $options Options list.
  189. * @return string formatted delta
  190. */
  191. public function formatDelta($value, array $options = [])
  192. {
  193. $formatted = $this->_engine->formatDelta($value, $options);
  194. $options += ['escape' => true];
  195. return $options['escape'] ? h($formatted) : $formatted;
  196. }
  197. /**
  198. * Getter/setter for default currency
  199. *
  200. * @param string|bool $currency Default currency string to be used by currency()
  201. * if $currency argument is not provided. If boolean false is passed, it will clear the
  202. * currently stored value
  203. * @return string Currency
  204. */
  205. public function defaultCurrency($currency)
  206. {
  207. return $this->_engine->defaultCurrency($currency);
  208. }
  209. /**
  210. * Event listeners.
  211. *
  212. * @return array
  213. */
  214. public function implementedEvents()
  215. {
  216. return [];
  217. }
  218. /**
  219. * Formats a number into locale specific ordinal suffix.
  220. *
  221. * @param int|float $value An integer
  222. * @param array $options An array with options.
  223. * @return string formatted number
  224. */
  225. public function ordinal($value, array $options = [])
  226. {
  227. return $this->_engine->ordinal($value, $options);
  228. }
  229. }