Number.php 6.5 KB

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