/t3lib/utility/class.t3lib_utility_math.php

https://github.com/itag/TYPO3v4-Core · PHP · 174 lines · 95 code · 9 blank · 70 comment · 32 complexity · 16279ac035cd05948cda7bb494519647 MD5 · raw file

  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. final class t3lib_utility_Math {
  35. /**
  36. * Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is FALSE then the $defaultValue is applied.
  37. *
  38. * @param $theInt integer Input value
  39. * @param $min integer Lower limit
  40. * @param $max integer Higher limit
  41. * @param $defaultValue integer Default value if input is FALSE.
  42. * @return integer The input value forced into the boundaries of $min and $max
  43. */
  44. public static function forceIntegerInRange($theInt, $min, $max = 2000000000, $defaultValue = 0) {
  45. // Returns $theInt as an integer in the integerspace from $min to $max
  46. $theInt = intval($theInt);
  47. if ($defaultValue && !$theInt) {
  48. $theInt = $defaultValue;
  49. } // If the input value is zero after being converted to integer, defaultValue may set another default value for it.
  50. if ($theInt < $min) {
  51. $theInt = $min;
  52. }
  53. if ($theInt > $max) {
  54. $theInt = $max;
  55. }
  56. return $theInt;
  57. }
  58. /**
  59. * Returns $theInt if it is greater than zero, otherwise returns zero.
  60. *
  61. * @param $theInt integer Integer string to process
  62. * @return integer
  63. */
  64. public static function convertToPositiveInteger($theInt) {
  65. $theInt = intval($theInt);
  66. if ($theInt < 0) {
  67. $theInt = 0;
  68. }
  69. return $theInt;
  70. }
  71. /**
  72. * Tests if the input can be interpreted as integer.
  73. *
  74. * @param $var mixed Any input variable to test
  75. * @return boolean Returns TRUE if string is an integer
  76. */
  77. public static function canBeInterpretedAsInteger($var) {
  78. if ($var === '') {
  79. return FALSE;
  80. }
  81. return (string) intval($var) === (string) $var;
  82. }
  83. /**
  84. * Calculates the input by +,-,*,/,%,^ with priority to + and -
  85. *
  86. * @param $string string Input string, eg "123 + 456 / 789 - 4"
  87. * @return integer Calculated value. Or error string.
  88. * @see t3lib_utility_Math::calculateWithParentheses()
  89. */
  90. public static function calculateWithPriorityToAdditionAndSubtraction($string) {
  91. $string = preg_replace('/[[:space:]]*/', '', $string); // removing all whitespace
  92. $string = '+' . $string; // Ensuring an operator for the first entrance
  93. $qm = '\*\/\+-^%';
  94. $regex = '([' . $qm . '])([' . $qm . ']?[0-9\.]*)';
  95. // split the expression here:
  96. $reg = array();
  97. preg_match_all('/' . $regex . '/', $string, $reg);
  98. reset($reg[2]);
  99. $number = 0;
  100. $Msign = '+';
  101. $err = '';
  102. $buffer = doubleval(current($reg[2]));
  103. next($reg[2]); // Advance pointer
  104. while (list($k, $v) = each($reg[2])) {
  105. $v = doubleval($v);
  106. $sign = $reg[1][$k];
  107. if ($sign == '+' || $sign == '-') {
  108. $Msign == '-' ? $number -= $buffer : $number += $buffer;
  109. $Msign = $sign;
  110. $buffer = $v;
  111. } else {
  112. if ($sign == '/') {
  113. if ($v) {
  114. $buffer /= $v;
  115. } else {
  116. $err = 'dividing by zero';
  117. }
  118. }
  119. if ($sign == '%') {
  120. if ($v) {
  121. $buffer %= $v;
  122. } else {
  123. $err = 'dividing by zero';
  124. }
  125. }
  126. if ($sign == '*') {
  127. $buffer *= $v;
  128. }
  129. if ($sign == '^') {
  130. $buffer = pow($buffer, $v);
  131. }
  132. }
  133. }
  134. $number = $Msign == '-' ? $number -= $buffer : $number += $buffer;
  135. return $err ? 'ERROR: ' . $err : $number;
  136. }
  137. /**
  138. * Calculates the input with parenthesis levels
  139. *
  140. * @param $string string Input string, eg "(123 + 456) / 789 - 4"
  141. * @return integer Calculated value. Or error string.
  142. * @see calculateWithPriorityToAdditionAndSubtraction(), tslib_cObj::stdWrap()
  143. */
  144. public static function calculateWithParentheses($string) {
  145. $securC = 100;
  146. do {
  147. $valueLenO = strcspn($string, '(');
  148. $valueLenC = strcspn($string, ')');
  149. if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
  150. $value = self::calculateWithPriorityToAdditionAndSubtraction(substr($string, 0, $valueLenC));
  151. $string = $value . substr($string, $valueLenC + 1);
  152. return $string;
  153. } else {
  154. $string = substr($string, 0, $valueLenO) . self::calculateWithParentheses(substr($string, $valueLenO + 1));
  155. }
  156. // Security:
  157. $securC--;
  158. if ($securC <= 0) {
  159. break;
  160. }
  161. } while ($valueLenO < strlen($string));
  162. return $string;
  163. }
  164. }
  165. ?>