[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / MathUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * Class with helper functions for mathematical calculations
18 *
19 * @author Susanne Moog <typo3@susanne-moog.de>
20 */
21 class MathUtility {
22
23 /**
24 * Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is FALSE then the $defaultValue is applied.
25 *
26 * @param integer $theInt Input value
27 * @param integer $min Lower limit
28 * @param integer $max Higher limit
29 * @param integer $defaultValue Default value if input is FALSE.
30 * @return integer The input value forced into the boundaries of $min and $max
31 */
32 static public function forceIntegerInRange($theInt, $min, $max = 2000000000, $defaultValue = 0) {
33 // Returns $theInt as an integer in the integerspace from $min to $max
34 $theInt = (int)$theInt;
35 // If the input value is zero after being converted to integer,
36 // defaultValue may set another default value for it.
37 if ($defaultValue && !$theInt) {
38 $theInt = $defaultValue;
39 }
40 if ($theInt < $min) {
41 $theInt = $min;
42 }
43 if ($theInt > $max) {
44 $theInt = $max;
45 }
46 return $theInt;
47 }
48
49 /**
50 * Returns $theInt if it is greater than zero, otherwise returns zero.
51 *
52 * @param integer $theInt Integer string to process
53 * @return integer
54 */
55 static public function convertToPositiveInteger($theInt) {
56 $theInt = (int)$theInt;
57 if ($theInt < 0) {
58 $theInt = 0;
59 }
60 return $theInt;
61 }
62
63 /**
64 * Tests if the input can be interpreted as integer.
65 *
66 * Note: Integer casting from objects or arrays is considered undefined and thus will return false.
67 *
68 * @see http://php.net/manual/en/language.types.integer.php#language.types.integer.casting.from-other
69 * @param mixed $var Any input variable to test
70 * @return boolean Returns TRUE if string is an integer
71 */
72 static public function canBeInterpretedAsInteger($var) {
73 if ($var === '' || is_object($var) || is_array($var)) {
74 return FALSE;
75 }
76 return (string) (int)$var === (string) $var;
77 }
78
79 /**
80 * Tests if the input can be interpreted as float.
81 *
82 * Note: Float casting from objects or arrays is considered undefined and thus will return false.
83 *
84 * @see http://www.php.net/manual/en/language.types.float.php, section "Formally" for the notation
85 * @param mixed $var Any input variable to test
86 * @return boolean Returns TRUE if string is a float
87 */
88 static public function canBeInterpretedAsFloat($var) {
89 $pattern_lnum = '[0-9]+';
90 $pattern_dnum = '([0-9]*[\.]' . $pattern_lnum . ')|(' . $pattern_lnum . '[\.][0-9]*)';
91 $pattern_exp_dnum = '[+-]?((' . $pattern_lnum . '|' . $pattern_dnum . ')([eE][+-]?' . $pattern_lnum . ')?)';
92
93 if ($var === '' || is_object($var) || is_array($var)) {
94 return FALSE;
95 }
96
97 $matches = preg_match('/^' . $pattern_exp_dnum . '$/', $var);
98 return $matches === 1;
99 }
100
101 /**
102 * Calculates the input by +,-,*,/,%,^ with priority to + and -
103 *
104 * @param string $string Input string, eg "123 + 456 / 789 - 4
105 * @return integer Calculated value. Or error string.
106 * @see \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses()
107 */
108 static public function calculateWithPriorityToAdditionAndSubtraction($string) {
109 // Removing all whitespace
110 $string = preg_replace('/[[:space:]]*/', '', $string);
111 // Ensuring an operator for the first entrance
112 $string = '+' . $string;
113 $qm = '\\*\\/\\+-^%';
114 $regex = '([' . $qm . '])([' . $qm . ']?[0-9\\.]*)';
115 // Split the expression here:
116 $reg = array();
117 preg_match_all('/' . $regex . '/', $string, $reg);
118 reset($reg[2]);
119 $number = 0;
120 $Msign = '+';
121 $err = '';
122 $buffer = doubleval(current($reg[2]));
123 // Advance pointer
124 next($reg[2]);
125 while (list($k, $v) = each($reg[2])) {
126 $v = doubleval($v);
127 $sign = $reg[1][$k];
128 if ($sign == '+' || $sign == '-') {
129 $Msign == '-' ? ($number -= $buffer) : ($number += $buffer);
130 $Msign = $sign;
131 $buffer = $v;
132 } else {
133 if ($sign == '/') {
134 if ($v) {
135 $buffer /= $v;
136 } else {
137 $err = 'dividing by zero';
138 }
139 }
140 if ($sign == '%') {
141 if ($v) {
142 $buffer %= $v;
143 } else {
144 $err = 'dividing by zero';
145 }
146 }
147 if ($sign == '*') {
148 $buffer *= $v;
149 }
150 if ($sign == '^') {
151 $buffer = pow($buffer, $v);
152 }
153 }
154 }
155 $number = $Msign == '-' ? ($number -= $buffer) : ($number += $buffer);
156 return $err ? 'ERROR: ' . $err : $number;
157 }
158
159 /**
160 * Calculates the input with parenthesis levels
161 *
162 * @param string $string Input string, eg "(123 + 456) / 789 - 4
163 * @return integer Calculated value. Or error string.
164 * @see calculateWithPriorityToAdditionAndSubtraction(), \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::stdWrap()
165 */
166 static public function calculateWithParentheses($string) {
167 $securC = 100;
168 do {
169 $valueLenO = strcspn($string, '(');
170 $valueLenC = strcspn($string, ')');
171 if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
172 $value = self::calculateWithPriorityToAdditionAndSubtraction(substr($string, 0, $valueLenC));
173 $string = $value . substr($string, ($valueLenC + 1));
174 return $string;
175 } else {
176 $string = substr($string, 0, $valueLenO) . self::calculateWithParentheses(substr($string, ($valueLenO + 1)));
177 }
178 // Security:
179 $securC--;
180 if ($securC <= 0) {
181 break;
182 }
183 } while ($valueLenO < strlen($string));
184 return $string;
185 }
186
187 /**
188 * Checks whether the given number $value is an integer in the range [$minimum;$maximum]
189 *
190 * @param integer $value Integer value to check
191 * @param integer $minimum Lower boundary of the range
192 * @param integer $maximum Upper boundary of the range
193 * @return boolean
194 */
195 static public function isIntegerInRange($value, $minimum, $maximum) {
196 $value = filter_var($value, FILTER_VALIDATE_INT, array(
197 'options' => array(
198 'min_range' => $minimum,
199 'max_range' => $maximum
200 )
201 ));
202 $isInRange = is_int($value);
203 return $isInRange;
204 }
205
206 }