NumberHelper.php 5.0 KB

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