Number.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace Tools\Utility;
  3. use Cake\Core\Configure;
  4. use Cake\I18n\Number as CakeNumber;
  5. /**
  6. * Extend CakeNumber with a few important improvements:
  7. * - config setting for format()
  8. * - spacer char for currency (initially from https://github.com/cakephp/cakephp/pull/1148)
  9. * - signed values possible
  10. */
  11. class Number extends CakeNumber {
  12. protected static $_currency = 'EUR';
  13. protected static $_symbolRight = '€';
  14. protected static $_symbolLeft = '';
  15. protected static $_decimals = ',';
  16. protected static $_thousands = '.';
  17. /**
  18. * Correct the default values according to localization
  19. *
  20. * @return void
  21. * @deprecated Should not be used anymore with 3.x functionality?
  22. */
  23. public static function setConfig($options = []) {
  24. $config = $options + (array)Configure::read('Localization');
  25. foreach ($config as $key => $value) {
  26. $key = '_' . $key;
  27. if (!isset(static::${$key})) {
  28. continue;
  29. }
  30. static::${$key} = $value;
  31. }
  32. }
  33. /**
  34. * Convenience method to display the default currency
  35. *
  36. * @param float $amount
  37. * @param array $formatOptions
  38. * @return string
  39. */
  40. public static function money($amount, array $formatOptions = []) {
  41. return static::currency($amount, null, $formatOptions);
  42. }
  43. /**
  44. * Format numeric values
  45. * should not be used for currencies
  46. * //TODO: automize per localeconv() ?
  47. *
  48. * @param float $number
  49. * @param array $options : currency=true/false, ... (leave empty for no special treatment)
  50. * @return string
  51. */
  52. public static function _format($number, array $formatOptions = []) {
  53. if (!is_numeric($number)) {
  54. $default = '---';
  55. if (!empty($options['default'])) {
  56. $default = $options['default'];
  57. }
  58. return $default;
  59. }
  60. if ($formatOptions === false) {
  61. $formatOptions = [];
  62. } elseif (!is_array($formatOptions)) {
  63. $formatOptions = ['places' => $formatOptions];
  64. }
  65. $options = ['before' => '', 'after' => '', 'places' => 2, 'thousands' => static::$_thousands, 'decimals' => static::$_decimals, 'escape' => false];
  66. $options = $formatOptions + $options;
  67. if (!empty($options['currency'])) {
  68. if (!empty(static::$_symbolRight)) {
  69. $options['after'] = ' ' . static::$_symbolRight;
  70. } elseif (!empty(static::$_symbolLeft)) {
  71. $options['before'] = static::$_symbolLeft . ' ';
  72. }
  73. }
  74. /*
  75. if ($spacer !== false) {
  76. $spacer = ($spacer === true) ? ' ' : $spacer;
  77. if ((string)$before !== '') {
  78. $before .= $spacer;
  79. }
  80. if ((string)$after !== '') {
  81. $after = $spacer . $after;
  82. }
  83. }
  84. */
  85. if ($options['places'] < 0) {
  86. $number = round($number, $options['places']);
  87. }
  88. $sign = '';
  89. if ($number > 0 && !empty($options['signed'])) {
  90. $sign = '+';
  91. }
  92. if (isset($options['signed'])) {
  93. unset($options['signed']);
  94. }
  95. return $sign . parent::format($number, $options);
  96. }
  97. /**
  98. * Format
  99. *
  100. * Additional options
  101. * - signed
  102. * - positive
  103. *
  104. * @param float $number
  105. * @param array $options
  106. * @return string
  107. */
  108. public static function format($number, array $options = []) {
  109. $defaults = [
  110. 'positive' => '+', 'signed' => false
  111. ];
  112. $options += $defaults;
  113. $sign = '';
  114. if ($number > 0 && !empty($options['signed'])) {
  115. $sign = '+';
  116. }
  117. if (isset($options['signed'])) {
  118. unset($options['signed']);
  119. }
  120. return $sign . parent::format($number, $options);
  121. }
  122. /**
  123. * Overwrite to allow
  124. *
  125. * - signed: true/false
  126. *
  127. * @param float $number
  128. * @param string $currency
  129. * @param array $options
  130. * @return string
  131. */
  132. public static function currency($number, $currency = null, array $options = []) {
  133. $defaults = [
  134. 'positive' => '+', 'signed' => false
  135. ];
  136. $options += $defaults;
  137. $sign = '';
  138. if ($number > 0 && !empty($options['signed'])) {
  139. $sign = $options['positive'];
  140. }
  141. return $sign . parent::currency($number, $currency, $options);
  142. }
  143. /**
  144. * Returns a formatted-for-humans file size.
  145. *
  146. * @param int $size Size in bytes
  147. * @return string Human readable size
  148. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  149. */
  150. public static function _toReadableSize($size, $decimals = '.') {
  151. $size = parent::toReadableSize($size);
  152. if ($decimals !== '.') {
  153. $size = str_replace('.', $decimals, $size);
  154. }
  155. return $size;
  156. }
  157. /**
  158. * Get the rounded average.
  159. *
  160. * @param array $values: int or float values
  161. * @param int $precision
  162. * @return float Average
  163. */
  164. public static function average($values, $precision = 0) {
  165. if (empty($values)) {
  166. return 0.0;
  167. }
  168. return round(array_sum($values) / count($values), $precision);
  169. }
  170. /**
  171. * Round value.
  172. *
  173. * @param float $number
  174. * @param float $increment
  175. * @return float result
  176. */
  177. public static function roundTo($number, $increments = 1.0) {
  178. $precision = static::getDecimalPlaces($increments);
  179. $res = round($number, $precision);
  180. if ($precision <= 0) {
  181. $res = (int)$res;
  182. }
  183. return $res;
  184. }
  185. /**
  186. * Round value up.
  187. *
  188. * @param float $number
  189. * @param int $increment
  190. * @return float result
  191. */
  192. public static function roundUpTo($number, $increments = 1) {
  193. return ceil($number / $increments) * $increments;
  194. }
  195. /**
  196. * Round value down.
  197. *
  198. * @param float $number
  199. * @param int $increment
  200. * @return float result
  201. */
  202. public static function roundDownTo($number, $increments = 1) {
  203. return floor($number / $increments) * $increments;
  204. }
  205. /**
  206. * Get decimal places
  207. *
  208. * @param float $number
  209. * @return int decimalPlaces
  210. */
  211. public static function getDecimalPlaces($number) {
  212. $decimalPlaces = 0;
  213. while ($number > 1 && $number != 0) {
  214. $number /= 10;
  215. $decimalPlaces -= 1;
  216. }
  217. while ($number < 1 && $number != 0) {
  218. $number *= 10;
  219. $decimalPlaces += 1;
  220. }
  221. return $decimalPlaces;
  222. }
  223. /**
  224. * Can compare two float values
  225. *
  226. * @link http://php.net/manual/en/language.types.float.php
  227. * @param float $x
  228. * @param float $y
  229. * @param float $precision
  230. * @return bool
  231. */
  232. public static function isFloatEqual($x, $y, $precision = 0.0000001) {
  233. return ($x + $precision >= $y) && ($x - $precision <= $y);
  234. }
  235. /**
  236. * Get the settings for a specific formatName
  237. *
  238. * @param string $formatName (EUR, ...)
  239. * @return array currencySettings or null on failure
  240. */
  241. public static function getFormat($formatName) {
  242. if (!isset(static::$_currencies[$formatName])) {
  243. return null;
  244. }
  245. return static::$_currencies[$formatName];
  246. }
  247. }