TYPO3 CMS  TYPO3_6-2
MathUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
21 class MathUtility {
22 
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 
55  static public function convertToPositiveInteger($theInt) {
56  $theInt = (int)$theInt;
57  if ($theInt < 0) {
58  $theInt = 0;
59  }
60  return $theInt;
61  }
62 
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 
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 
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 
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 
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 }
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
static calculateWithPriorityToAdditionAndSubtraction($string)
static calculateWithParentheses($string)
static isIntegerInRange($value, $minimum, $maximum)
static convertToPositiveInteger($theInt)
Definition: MathUtility.php:55