NumberHelper.php 5.0 KB

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