NumericHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. return '---';
  61. }
  62. if ($places === null) {
  63. $places = 2;
  64. }
  65. $options = array('currency' => true);
  66. if (!empty($formatOptions)) {
  67. $options = array_merge($options, $formatOptions); # Set::merge not neccessary
  68. }
  69. return $this->format($val, $places, $options); // ->currency()
  70. }
  71. /**
  72. * format numeric values
  73. * @param float $number
  74. * @param int $places (0 = int, 1..x places after dec, -1..-x places before dec)
  75. * @param array $option : currency=true/false, ... (leave empty for no special treatment)
  76. * //TODO: automize per localeconv() ?
  77. * 2009-04-03 ms
  78. */
  79. public function format($number, $places = null, $formatOptions = array()) {
  80. if (!is_numeric($number)) {
  81. return '---';
  82. }
  83. if (!is_integer($places)) {
  84. $places = 2;
  85. }
  86. $options = array('before' => '', 'after' => '', 'places' => $places, 'thousands' => $this->thousandsPoint, 'decimals' => $this->
  87. decimalPoint, 'escape' => false);
  88. if (!empty($formatOptions['currency'])) {
  89. if (!empty($this->symbolRight)) {
  90. $options['after'] = ' ' . $this->symbolRight;
  91. } elseif (!empty($this->symbolLeft)) {
  92. $options['before'] = $this->symbolLeft . ' ';
  93. } else {
  94. }
  95. } else {
  96. if (!empty($formatOptions['after'])) {
  97. $options['after'] = $formatOptions['after'];
  98. }
  99. if (!empty($formatOptions['before'])) {
  100. $options['before'] = $formatOptions['before'];
  101. }
  102. }
  103. if (!empty($formatOptions['thousands'])) {
  104. $options['thousands'] = $formatOptions['thousands'];
  105. }
  106. if (!empty($formatOptions['decimals'])) {
  107. $options['decimals'] = $formatOptions['decimals'];
  108. }
  109. if ($places < 0) {
  110. $number = round($number, $places);
  111. }
  112. return parent::format($number, $options);
  113. }
  114. /**
  115. * Returns the English ordinal suffix (th, st, nd, etc) of a number.
  116. *
  117. * echo 2, Num::ordinal(2); // "2nd"
  118. * echo 10, Num::ordinal(10); // "10th"
  119. * echo 33, Num::ordinal(33); // "33rd"
  120. *
  121. * @param integer number
  122. * @return string
  123. */
  124. public static function ordinal($number) {
  125. if ($number % 100 > 10 and $number % 100 < 14) {
  126. return 'th';
  127. }
  128. switch ($number % 10) {
  129. case 1:
  130. return 'st';
  131. case 2:
  132. return 'nd';
  133. case 3:
  134. return 'rd';
  135. default:
  136. return 'th';
  137. }
  138. }
  139. }