‪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 
55  public static function ‪convertToPositiveInteger(mixed $theInt): int
56  {
57  return ‪self::forceIntegerInRange($theInt, 0, PHP_INT_MAX);
58  }
59 
69  public static function ‪canBeInterpretedAsInteger(mixed $var): bool
70  {
71  if ($var === '' || is_object($var) || is_array($var)) {
72  return false;
73  }
74  return (string)(int)$var === (string)$var;
75  }
76 
86  public static function ‪canBeInterpretedAsFloat(mixed $var): bool
87  {
88  $pattern_lnum = '[0-9]+';
89  $pattern_dnum = '([0-9]*[\.]' . $pattern_lnum . ')|(' . $pattern_lnum . '[\.][0-9]*)';
90  $pattern_exp_dnum = '[+-]?((' . $pattern_lnum . '|' . $pattern_dnum . ')([eE][+-]?' . $pattern_lnum . ')?)';
91 
92  if ($var === '' || is_object($var) || is_array($var)) {
93  return false;
94  }
95 
96  $matches = preg_match('/^' . $pattern_exp_dnum . '$/', (string)$var);
97  return $matches === 1;
98  }
99 
107  public static function ‪calculateWithPriorityToAdditionAndSubtraction(string $string): float|string
108  {
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 = [];
117  preg_match_all('/' . $regex . '/', $string, $reg);
118  reset($reg[2]);
119  $number = 0;
120  $Msign = '+';
121  $err = '';
122  $buffer = (float)current($reg[2]);
123  // Advance pointer
124  $regSliced = array_slice($reg[2], 1, null, true);
125  foreach ($regSliced as $k => $v) {
126  $v = (float)$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 = $buffer ** $v;
152  }
153  }
154  }
155  $number = $Msign === '-' ? ($number - $buffer) : ($number + $buffer);
156  return $err ? 'ERROR: ' . $err : $number;
157  }
158 
167  public static function ‪calculateWithParentheses(string $string): string
168  {
169  $securC = 100;
170  do {
171  $valueLenO = strcspn($string, '(');
172  $valueLenC = strcspn($string, ')');
173  if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
174  $value = ‪self::calculateWithPriorityToAdditionAndSubtraction(substr($string, 0, $valueLenC));
175  $string = $value . substr($string, $valueLenC + 1);
176  return $string;
177  }
178  $string = substr($string, 0, $valueLenO) . ‪self::calculateWithParentheses(substr($string, $valueLenO + 1));
179 
180  // Security:
181  $securC--;
182  if ($securC <= 0) {
183  break;
184  }
185  } while ($valueLenO < strlen($string));
186  return $string;
187  }
188 
196  public static function ‪isIntegerInRange(mixed $value, int $minimum, int $maximum): bool
197  {
198  $value = filter_var($value, FILTER_VALIDATE_INT, [
199  'options' => [
200  'min_range' => $minimum,
201  'max_range' => $maximum,
202  ],
203  ]);
204  return is_int($value);
205  }
206 }
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithParentheses
‪static string calculateWithParentheses(string $string)
Definition: MathUtility.php:167
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Utility\MathUtility\convertToPositiveInteger
‪static convertToPositiveInteger(mixed $theInt)
Definition: MathUtility.php:55
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithPriorityToAdditionAndSubtraction
‪static float string calculateWithPriorityToAdditionAndSubtraction(string $string)
Definition: MathUtility.php:107
‪TYPO3\CMS\Core\Utility\MathUtility\isIntegerInRange
‪static isIntegerInRange(mixed $value, int $minimum, int $maximum)
Definition: MathUtility.php:196
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat(mixed $var)
Definition: MathUtility.php:86
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34