‪TYPO3CMS  10.4
MathUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 {
32  public static function ‪forceIntegerInRange($theInt, $min, $max = 2000000000, $defaultValue = 0)
33  {
34  // Returns $theInt as an integer in the integerspace from $min to $max
35  $theInt = (int)$theInt;
36  // If the input value is zero after being converted to integer,
37  // defaultValue may set another default value for it.
38  if ($defaultValue && !$theInt) {
39  $theInt = $defaultValue;
40  }
41  if ($theInt < $min) {
42  $theInt = $min;
43  }
44  if ($theInt > $max) {
45  $theInt = $max;
46  }
47  return $theInt;
48  }
49 
56  public static function ‪convertToPositiveInteger($theInt)
57  {
58  $theInt = (int)$theInt;
59  if ($theInt < 0) {
60  $theInt = 0;
61  }
62  return $theInt;
63  }
64 
74  public static function ‪canBeInterpretedAsInteger($var)
75  {
76  if ($var === '' || is_object($var) || is_array($var)) {
77  return false;
78  }
79  return (string)(int)$var === (string)$var;
80  }
81 
91  public static function ‪canBeInterpretedAsFloat($var)
92  {
93  $pattern_lnum = '[0-9]+';
94  $pattern_dnum = '([0-9]*[\.]' . $pattern_lnum . ')|(' . $pattern_lnum . '[\.][0-9]*)';
95  $pattern_exp_dnum = '[+-]?((' . $pattern_lnum . '|' . $pattern_dnum . ')([eE][+-]?' . $pattern_lnum . ')?)';
96 
97  if ($var === '' || is_object($var) || is_array($var)) {
98  return false;
99  }
100 
101  $matches = preg_match('/^' . $pattern_exp_dnum . '$/', $var);
102  return $matches === 1;
103  }
104 
112  public static function ‪calculateWithPriorityToAdditionAndSubtraction($string)
113  {
114  // Removing all whitespace
115  $string = preg_replace('/[[:space:]]*/', '', $string);
116  // Ensuring an operator for the first entrance
117  $string = '+' . $string;
118  $qm = '\\*\\/\\+-^%';
119  $regex = '([' . $qm . '])([' . $qm . ']?[0-9\\.]*)';
120  // Split the expression here:
121  $reg = [];
122  preg_match_all('/' . $regex . '/', $string, $reg);
123  reset($reg[2]);
124  $number = 0;
125  $Msign = '+';
126  $err = '';
127  $buffer = (float)current($reg[2]);
128  // Advance pointer
129  $regSliced = array_slice($reg[2], 1, null, true);
130  foreach ($regSliced as $k => $v) {
131  $v = (float)$v;
132  $sign = $reg[1][$k];
133  if ($sign === '+' || $sign === '-') {
134  $Msign === '-' ? ($number -= $buffer) : ($number += $buffer);
135  $Msign = $sign;
136  $buffer = $v;
137  } else {
138  if ($sign === '/') {
139  if ($v) {
140  $buffer /= $v;
141  } else {
142  $err = 'dividing by zero';
143  }
144  }
145  if ($sign === '%') {
146  if ($v) {
147  $buffer %= $v;
148  } else {
149  $err = 'dividing by zero';
150  }
151  }
152  if ($sign === '*') {
153  $buffer *= $v;
154  }
155  if ($sign === '^') {
156  $buffer = $buffer ** $v;
157  }
158  }
159  }
160  $number = $Msign === '-' ? ($number - $buffer) : ($number + $buffer);
161  return $err ? 'ERROR: ' . $err : $number;
162  }
163 
172  public static function ‪calculateWithParentheses($string)
173  {
174  $securC = 100;
175  do {
176  $valueLenO = strcspn($string, '(');
177  $valueLenC = strcspn($string, ')');
178  if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
179  $value = ‪self::calculateWithPriorityToAdditionAndSubtraction(substr($string, 0, $valueLenC));
180  $string = $value . substr($string, $valueLenC + 1);
181  return $string;
182  }
183  $string = substr($string, 0, $valueLenO) . ‪self::calculateWithParentheses(substr($string, $valueLenO + 1));
184 
185  // Security:
186  $securC--;
187  if ($securC <= 0) {
188  break;
189  }
190  } while ($valueLenO < strlen($string));
191  return $string;
192  }
193 
202  public static function ‪isIntegerInRange($value, $minimum, $maximum)
203  {
204  $value = filter_var($value, FILTER_VALIDATE_INT, [
205  'options' => [
206  'min_range' => $minimum,
207  'max_range' => $maximum
208  ]
209  ]);
210  $isInRange = is_int($value);
211  return $isInRange;
212  }
213 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithPriorityToAdditionAndSubtraction
‪static int calculateWithPriorityToAdditionAndSubtraction($string)
Definition: MathUtility.php:112
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Core\Utility\MathUtility\isIntegerInRange
‪static bool isIntegerInRange($value, $minimum, $maximum)
Definition: MathUtility.php:202
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat($var)
Definition: MathUtility.php:91
‪TYPO3\CMS\Core\Utility\MathUtility\convertToPositiveInteger
‪static int convertToPositiveInteger($theInt)
Definition: MathUtility.php:56
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithParentheses
‪static int calculateWithParentheses($string)
Definition: MathUtility.php:172
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22