/src/View/Helper/NumberHelper.php

https://github.com/LubosRemplik/cakephp · PHP · 248 lines · 70 code · 20 blank · 158 comment · 2 complexity · c849e5f5c93360a7f942ac0bada50463 MD5 · raw file

  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.0/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. * @return string Formatted float.
  84. * @see \Cake\I18n\Number::precision()
  85. * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-floating-point-numbers
  86. */
  87. public function precision($number, $precision = 3)
  88. {
  89. return $this->_engine->precision($number, $precision);
  90. }
  91. /**
  92. * Returns a formatted-for-humans file size.
  93. *
  94. * @param int $size Size in bytes
  95. * @return string Human readable size
  96. * @see \Cake\I18n\Number::toReadableSize()
  97. * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#interacting-with-human-readable-values
  98. */
  99. public function toReadableSize($size)
  100. {
  101. return $this->_engine->toReadableSize($size);
  102. }
  103. /**
  104. * Formats a number into a percentage string.
  105. *
  106. * Options:
  107. *
  108. * - `multiply`: Multiply the input value by 100 for decimal percentages.
  109. *
  110. * @param float $number A floating point number
  111. * @param int $precision The precision of the returned number
  112. * @param array $options Options
  113. * @return string Percentage string
  114. * @see \Cake\I18n\Number::toPercentage()
  115. * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-percentages
  116. */
  117. public function toPercentage($number, $precision = 2, array $options = [])
  118. {
  119. return $this->_engine->toPercentage($number, $precision, $options);
  120. }
  121. /**
  122. * Formats a number into the correct locale format
  123. *
  124. * Options:
  125. *
  126. * - `places` - Minimum number or decimals to use, e.g 0
  127. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  128. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  129. * - `before` - The string to place before whole numbers, e.g. '['
  130. * - `after` - The string to place after decimal numbers, e.g. ']'
  131. * - `escape` - Whether or not to escape html in resulting string
  132. *
  133. * @param float $number A floating point number.
  134. * @param array $options An array with options.
  135. * @return string Formatted number
  136. * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-numbers
  137. */
  138. public function format($number, array $options = [])
  139. {
  140. $formatted = $this->_engine->format($number, $options);
  141. $options += ['escape' => true];
  142. return $options['escape'] ? h($formatted) : $formatted;
  143. }
  144. /**
  145. * Formats a number into a currency format.
  146. *
  147. * ### Options
  148. *
  149. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  150. * - `fractionSymbol` - The currency symbol to use for fractional numbers.
  151. * - `fractionPosition` - The position the fraction symbol should be placed
  152. * valid options are 'before' & 'after'.
  153. * - `before` - Text to display before the rendered number
  154. * - `after` - Text to display after the rendered number
  155. * - `zero` - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
  156. * - `places` - Number of decimal places to use. e.g. 2
  157. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  158. * - `pattern` - An ICU number pattern to use for formatting the number. e.g #,###.00
  159. * - `useIntlCode` - Whether or not to replace the currency symbol with the international
  160. * currency code.
  161. * - `escape` - Whether or not to escape html in resulting string
  162. *
  163. * @param float $number Value to format.
  164. * @param string|null $currency International currency name such as 'USD', 'EUR', 'JPY', 'CAD'
  165. * @param array $options Options list.
  166. * @return string Number formatted as a currency.
  167. */
  168. public function currency($number, $currency = null, array $options = [])
  169. {
  170. $formatted = $this->_engine->currency($number, $currency, $options);
  171. $options += ['escape' => true];
  172. return $options['escape'] ? h($formatted) : $formatted;
  173. }
  174. /**
  175. * Formats a number into the correct locale format to show deltas (signed differences in value).
  176. *
  177. * ### Options
  178. *
  179. * - `places` - Minimum number or decimals to use, e.g 0
  180. * - `precision` - Maximum Number of decimal places to use, e.g. 2
  181. * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
  182. * - `before` - The string to place before whole numbers, e.g. '['
  183. * - `after` - The string to place after decimal numbers, e.g. ']'
  184. * - `escape` - Set to false to prevent escaping
  185. *
  186. * @param float $value A floating point number
  187. * @param array $options Options list.
  188. * @return string formatted delta
  189. */
  190. public function formatDelta($value, array $options = [])
  191. {
  192. $formatted = $this->_engine->formatDelta($value, $options);
  193. $options += ['escape' => true];
  194. return $options['escape'] ? h($formatted) : $formatted;
  195. }
  196. /**
  197. * Getter/setter for default currency
  198. *
  199. * @param string|bool $currency Default currency string to be used by currency()
  200. * if $currency argument is not provided. If boolean false is passed, it will clear the
  201. * currently stored value
  202. * @return string Currency
  203. */
  204. public function defaultCurrency($currency)
  205. {
  206. return $this->_engine->defaultCurrency($currency);
  207. }
  208. /**
  209. * Event listeners.
  210. *
  211. * @return array
  212. */
  213. public function implementedEvents()
  214. {
  215. return [];
  216. }
  217. /**
  218. * Formats a number into locale specific ordinal suffix.
  219. *
  220. * @param int|float $value An integer
  221. * @param array $options An array with options.
  222. * @return string formatted number
  223. */
  224. public function ordinal($value, array $options = [])
  225. {
  226. return $this->_engine->ordinal($value, $options);
  227. }
  228. }