‪TYPO3CMS  ‪main
MathUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 {
34  public static function ‪forceIntegerInRange(mixed $theInt, int $min, int $max = 2000000000, int $defaultValue = 0): int
35  {
36  // Returns $theInt as an integer in the integerspace from $min to $max
37  $theInt = (int)$theInt;
38  // If the input value is zero after being converted to integer,
39  // defaultValue may set another default value for it.
40  if ($defaultValue && !$theInt) {
41  $theInt = $defaultValue;
42  }
43  if ($theInt < $min) {
44  $theInt = $min;
45  }
46  if ($theInt > $max) {
47  $theInt = $max;
48  }
49  return $theInt;
50  }
51 
56  public static function ‪convertToPositiveInteger(mixed $theInt): int
57  {
58  trigger_error(
59  __METHOD__ . ' is deprecated and will be removed in TYPO3 v14. Use max() instead.',
60  E_USER_DEPRECATED
61  );
62  return ‪self::forceIntegerInRange($theInt, 0, PHP_INT_MAX);
63  }
64 
74  public static function ‪canBeInterpretedAsInteger(mixed $var): bool
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(mixed $var): bool
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 . '$/', (string)$var);
102  return $matches === 1;
103  }
104 
112  public static function ‪calculateWithPriorityToAdditionAndSubtraction(string $string): float|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 $string): 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 
201  public static function ‪isIntegerInRange(mixed $value, int $minimum, int $maximum): bool
202  {
203  $value = filter_var($value, FILTER_VALIDATE_INT, [
204  'options' => [
205  'min_range' => $minimum,
206  'max_range' => $maximum,
207  ],
208  ]);
209  return is_int($value);
210  }
211 }
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithParentheses
‪static string calculateWithParentheses(string $string)
Definition: MathUtility.php:172
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Utility\MathUtility\convertToPositiveInteger
‪static convertToPositiveInteger(mixed $theInt)
Definition: MathUtility.php:56
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithPriorityToAdditionAndSubtraction
‪static float string calculateWithPriorityToAdditionAndSubtraction(string $string)
Definition: MathUtility.php:112
‪TYPO3\CMS\Core\Utility\MathUtility\isIntegerInRange
‪static isIntegerInRange(mixed $value, int $minimum, int $maximum)
Definition: MathUtility.php:201
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat(mixed $var)
Definition: MathUtility.php:91
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34