[TASK] Remove XCLASS definitions from cache classes
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_math.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Susanne Moog <typo3@susanne-moog.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Class with helper functions for mathematical calculations
29 *
30 * @author Susanne Moog <typo3@susanne-moog.de>
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34
35 final class t3lib_utility_Math {
36
37 /**
38 * Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is FALSE then the $defaultValue is applied.
39 *
40 * @param $theInt integer Input value
41 * @param $min integer Lower limit
42 * @param $max integer Higher limit
43 * @param $defaultValue integer Default value if input is FALSE.
44 * @return integer The input value forced into the boundaries of $min and $max
45 */
46 public static function forceIntegerInRange($theInt, $min, $max = 2000000000, $defaultValue = 0) {
47 // Returns $theInt as an integer in the integerspace from $min to $max
48 $theInt = intval($theInt);
49 if ($defaultValue && !$theInt) {
50 $theInt = $defaultValue;
51 } // If the input value is zero after being converted to integer, defaultValue may set another default value for it.
52 if ($theInt < $min) {
53 $theInt = $min;
54 }
55 if ($theInt > $max) {
56 $theInt = $max;
57 }
58 return $theInt;
59 }
60
61 /**
62 * Returns $theInt if it is greater than zero, otherwise returns zero.
63 *
64 * @param $theInt integer Integer string to process
65 * @return integer
66 */
67 public static function convertToPositiveInteger($theInt) {
68 $theInt = intval($theInt);
69 if ($theInt < 0) {
70 $theInt = 0;
71 }
72 return $theInt;
73 }
74
75 /**
76 * Tests if the input can be interpreted as integer.
77 *
78 * @param $var mixed Any input variable to test
79 * @return boolean Returns TRUE if string is an integer
80 */
81 public static function canBeInterpretedAsInteger($var) {
82 if ($var === '') {
83 return FALSE;
84 }
85 return (string) intval($var) === (string) $var;
86 }
87
88 /**
89 * Calculates the input by +,-,*,/,%,^ with priority to + and -
90 *
91 * @param $string string Input string, eg "123 + 456 / 789 - 4"
92 * @return integer Calculated value. Or error string.
93 * @see t3lib_utility_Math::calculateWithParentheses()
94 */
95 public static function calculateWithPriorityToAdditionAndSubtraction($string) {
96 $string = preg_replace('/[[:space:]]*/', '', $string); // removing all whitespace
97 $string = '+' . $string; // Ensuring an operator for the first entrance
98 $qm = '\*\/\+-^%';
99 $regex = '([' . $qm . '])([' . $qm . ']?[0-9\.]*)';
100 // split the expression here:
101 $reg = array();
102 preg_match_all('/' . $regex . '/', $string, $reg);
103
104 reset($reg[2]);
105 $number = 0;
106 $Msign = '+';
107 $err = '';
108 $buffer = doubleval(current($reg[2]));
109 next($reg[2]); // Advance pointer
110
111 while (list($k, $v) = each($reg[2])) {
112 $v = doubleval($v);
113 $sign = $reg[1][$k];
114 if ($sign == '+' || $sign == '-') {
115 $Msign == '-' ? $number -= $buffer : $number += $buffer;
116 $Msign = $sign;
117 $buffer = $v;
118 } else {
119 if ($sign == '/') {
120 if ($v) {
121 $buffer /= $v;
122 } else {
123 $err = 'dividing by zero';
124 }
125 }
126 if ($sign == '%') {
127 if ($v) {
128 $buffer %= $v;
129 } else {
130 $err = 'dividing by zero';
131 }
132 }
133 if ($sign == '*') {
134 $buffer *= $v;
135 }
136 if ($sign == '^') {
137 $buffer = pow($buffer, $v);
138 }
139 }
140 }
141 $number = $Msign == '-' ? $number -= $buffer : $number += $buffer;
142 return $err ? 'ERROR: ' . $err : $number;
143 }
144
145 /**
146 * Calculates the input with parenthesis levels
147 *
148 * @param $string string Input string, eg "(123 + 456) / 789 - 4"
149 * @return integer Calculated value. Or error string.
150 * @see calculateWithPriorityToAdditionAndSubtraction(), tslib_cObj::stdWrap()
151 */
152 public static function calculateWithParentheses($string) {
153 $securC = 100;
154 do {
155 $valueLenO = strcspn($string, '(');
156 $valueLenC = strcspn($string, ')');
157 if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
158 $value = self::calculateWithPriorityToAdditionAndSubtraction(substr($string, 0, $valueLenC));
159 $string = $value . substr($string, $valueLenC + 1);
160 return $string;
161 } else {
162 $string = substr($string, 0, $valueLenO) . self::calculateWithParentheses(substr($string, $valueLenO + 1));
163 }
164 // Security:
165 $securC--;
166 if ($securC <= 0) {
167 break;
168 }
169 } while ($valueLenO < strlen($string));
170 return $string;
171 }
172 }
173
174 ?>