Merge pull request #15665 from MikeyMJCO/patch-1
[civicrm-core.git] / CRM / Utils / Number.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2020
31 */
32
33 /**
34 * Class CRM_Utils_Number
35 */
36 class CRM_Utils_Number {
37
38 /**
39 * Create a random number with a given precision.
40 *
41 * @param array $precision
42 * (int $significantDigits, int $postDecimalDigits).
43 *
44 * @return float
45 *
46 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
47 */
48 public static function createRandomDecimal($precision) {
49 list ($sigFigs, $decFigs) = $precision;
50 $rand = rand(0, pow(10, $sigFigs) - 1);
51 return $rand / pow(10, $decFigs);
52 }
53
54 /**
55 * Given a number, coerce it to meet the precision requirement. If possible, it should
56 * keep the number as-is. If necessary, this may drop the least-significant digits
57 * and/or move the decimal place.
58 *
59 * @param int|float $keyValue
60 * @param array $precision
61 * (int $significantDigits, int $postDecimalDigits).
62 * @return float
63 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
64 */
65 public static function createTruncatedDecimal($keyValue, $precision) {
66 list ($sigFigs, $decFigs) = $precision;
67 $sign = ($keyValue < 0) ? '-1' : 1;
68 // ex: -123.456 ==> 123456
69 $val = str_replace('.', '', abs($keyValue));
70 // ex: 123456 => 1234
71 $val = substr($val, 0, $sigFigs);
72
73 // Move any extra digits after decimal
74 $extraFigs = strlen($val) - ($sigFigs - $decFigs);
75 if ($extraFigs > 0) {
76 // ex: 1234 => 1.234
77 return $sign * $val / pow(10, $extraFigs);
78 }
79 else {
80 return $sign * $val;
81 }
82 }
83
84 /**
85 * Some kind of numbery-looky-printy thing.
86 *
87 * @param string $size
88 * @param bool $checkForPostMax
89 *
90 * @return int
91 */
92 public static function formatUnitSize($size, $checkForPostMax = FALSE) {
93 if ($size) {
94 $last = strtolower($size{strlen($size) - 1});
95 $size = (int) $size;
96 switch ($last) {
97 // The 'G' modifier is available since PHP 5.1.0
98
99 case 'g':
100 $size *= 1024;
101 case 'm':
102 $size *= 1024;
103 case 'k':
104 $size *= 1024;
105 }
106
107 if ($checkForPostMax) {
108 $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize'));
109 $postMaxSize = self::formatUnitSize(ini_get('post_max_size'));
110 if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) {
111 CRM_Core_Session::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert");
112 }
113 // respect php.ini upload_max_filesize
114 if ($size > $maxImportFileSize && $size !== $postMaxSize) {
115 $size = $maxImportFileSize;
116 CRM_Core_Session::setStatus(ts("Note: Please verify your configuration for Maximum File Size (in MB) <a href='%1'>Administrator >> System Settings >> Misc</a>. It should support 'upload_max_size' as defined in PHP.ini.Please check with your system administrator.", [1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1')]), ts("Warning"), "alert");
117 }
118 }
119 return $size;
120 }
121 }
122
123 }