NumericHelper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. App::import('Helper', 'Number');
  3. class NumericHelper extends NumberHelper {
  4. public $helpers = array('Session');
  5. protected $_settings = array(
  6. );
  7. protected $code = null;
  8. protected $places = 0;
  9. protected $symbolRight = null;
  10. protected $symbolLeft = null;
  11. protected $decimalPoint = '.';
  12. protected $thousandsPoint = ',';
  13. public function __construct($View = null, $settings = array()) {
  14. parent::__construct($View, $settings);
  15. $i18n = Configure::read('Currency');
  16. if (!empty($i18n['code'])) {
  17. $this->code = $i18n['code'];
  18. }
  19. if (!empty($i18n['places'])) {
  20. $this->places = $i18n['places'];
  21. }
  22. if (!empty($i18n['symbolRight'])) {
  23. $this->symbolRight = $i18n['symbolRight'];
  24. }
  25. if (!empty($i18n['symbolLeft'])) {
  26. $this->symbolLeft = $i18n['symbolLeft'];
  27. }
  28. if (isset($i18n['decimals'])) {
  29. $this->decimalPoint = $i18n['decimals'];
  30. }
  31. if (isset($i18n['thousands'])) {
  32. $this->thousandsPoint = $i18n['thousands'];
  33. }
  34. }
  35. /**
  36. * like price but with negative values allowed
  37. * @return string
  38. * 2011-10-05 ms
  39. */
  40. public function money($amount, $places = null, $formatOptions = array()) {
  41. $formatOptions['allowNegative'] = true;
  42. return $this->price($amount, null, $places, $formatOptions);
  43. }
  44. /**
  45. * @param price
  46. * @param specialPrice (outranks the price)
  47. * @param places
  48. * @param options
  49. * - allowNegative (defaults to false - price needs to be > 0)
  50. * - currency (defaults to true)
  51. * @return string
  52. * 2011-07-30 ms
  53. */
  54. public function price($price, $specialPrice = null, $places = null, $formatOptions = array()) {
  55. if ($specialPrice !== null && (float)$specialPrice > 0) {
  56. $val = $specialPrice;
  57. } elseif ((float)$price > 0 || !empty($formatOptions['allowNegative'])) {
  58. $val = $price;
  59. } else {
  60. if (isset($formatOptions['default'])) {
  61. return $formatOptions['default'];
  62. }
  63. $val = $price;
  64. }
  65. if ($places === null) {
  66. $places = 2;
  67. }
  68. $options = array('currency' => true);
  69. if (!empty($formatOptions)) {
  70. $options = array_merge($options, $formatOptions); # Set::merge not neccessary
  71. }
  72. return $this->format($val, $places, $options); // ->currency()
  73. }
  74. /**
  75. * format numeric values
  76. * @param float $number
  77. * @param int $places (0 = int, 1..x places after dec, -1..-x places before dec)
  78. * @param array $option : currency=true/false, ... (leave empty for no special treatment)
  79. * //TODO: automize per localeconv() ?
  80. * 2009-04-03 ms
  81. */
  82. public function format($number, $places = null, $formatOptions = array()) {
  83. if (!is_numeric($number)) {
  84. return '---';
  85. }
  86. if (!is_integer($places)) {
  87. $places = 2;
  88. }
  89. $options = array('before' => '', 'after' => '', 'places' => $places, 'thousands' => $this->thousandsPoint, 'decimals' => $this->
  90. decimalPoint, 'escape' => false);
  91. if (!empty($formatOptions['currency'])) {
  92. if (!empty($this->symbolRight)) {
  93. $options['after'] = ' ' . $this->symbolRight;
  94. } elseif (!empty($this->symbolLeft)) {
  95. $options['before'] = $this->symbolLeft . ' ';
  96. } else {
  97. }
  98. } else {
  99. if (!empty($formatOptions['after'])) {
  100. $options['after'] = $formatOptions['after'];
  101. }
  102. if (!empty($formatOptions['before'])) {
  103. $options['before'] = $formatOptions['before'];
  104. }
  105. }
  106. if (!empty($formatOptions['thousands'])) {
  107. $options['thousands'] = $formatOptions['thousands'];
  108. }
  109. if (!empty($formatOptions['decimals'])) {
  110. $options['decimals'] = $formatOptions['decimals'];
  111. }
  112. if ($places < 0) {
  113. $number = round($number, $places);
  114. }
  115. return parent::format($number, $options);
  116. }
  117. /**
  118. * Returns the English ordinal suffix (th, st, nd, etc) of a number.
  119. *
  120. * echo 2, Num::ordinal(2); // "2nd"
  121. * echo 10, Num::ordinal(10); // "10th"
  122. * echo 33, Num::ordinal(33); // "33rd"
  123. *
  124. * @param integer number
  125. * @return string
  126. */
  127. public static function ordinal($number) {
  128. if ($number % 100 > 10 and $number % 100 < 14) {
  129. return 'th';
  130. }
  131. switch ($number % 10) {
  132. case 1:
  133. return 'st';
  134. case 2:
  135. return 'nd';
  136. case 3:
  137. return 'rd';
  138. default:
  139. return 'th';
  140. }
  141. }
  142. public function currency($number, $currency = 'EUR', $options = array()) {
  143. return parent::currency($number, $currency, $options);
  144. }
  145. }