[FEATURE] Add t3lib_utility_Math::isIntegerInRange
authorSteffen Gebert <steffen.gebert@typo3.org>
Sun, 12 Aug 2012 17:59:39 +0000 (19:59 +0200)
committerJigal van Hemert <jigal@xs4all.nl>
Sun, 12 Aug 2012 20:18:21 +0000 (22:18 +0200)
Change-Id: Icf8567dc009d86b9795823009572555422f5c7ad
Resolves: #39739
Releases: 6.0
Reviewed-on: http://review.typo3.org/13617
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
t3lib/utility/class.t3lib_utility_math.php
tests/Unit/t3lib/utility/class.t3lib_utility_mathTest.php

index 1e2124f..a9fc904 100644 (file)
@@ -177,6 +177,26 @@ final class t3lib_utility_Math {
                } while ($valueLenO < strlen($string));
                return $string;
        }
+
+       /**
+        * Checks whether the given number $value is an integer in the range [$minimum;$maximum]
+        *
+        * @param $value integer Integer value to check
+        * @param $minimum integer Lower boundary of the range
+        * @param $maximum integer Upper boundary of the range
+        * @return bool
+        */
+       public static function isIntegerInRange($value, $minimum, $maximum) {
+               $value = filter_var($value, FILTER_VALIDATE_INT, array(
+                       'options' => array(
+                               'min_range' => $minimum,
+                               'max_range' => $maximum
+                       )
+               ));
+
+               $isInRange = is_int($value);
+               return $isInRange;
+       }
 }
 
 ?>
\ No newline at end of file
index cc4a5c0..cdd9c9e 100644 (file)
@@ -222,6 +222,62 @@ class t3lib_utility_MathTest extends tx_phpunit_testcase {
        public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
                $this->assertEquals($expected, t3lib_utility_Math::calculateWithParentheses($expression));
        }
+
+       //////////////////////////////////
+       // Tests concerning isIntegerInRange
+       //////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function isIntegerInRangeIncludesLowerBoundary() {
+               $this->assertTrue(t3lib_utility_Math::isIntegerInRange(1, 1, 2));
+       }
+
+       /**
+        * @test
+        */
+       public function isIntegerInRangeIncludesUpperBoundary() {
+               $this->assertTrue(t3lib_utility_Math::isIntegerInRange(2, 1, 2));
+       }
+
+       /**
+        * @test
+        */
+       public function isIntegerInRangeAcceptsValueInRange() {
+               $this->assertTrue(t3lib_utility_Math::isIntegerInRange(10, 1, 100));
+       }
+
+       /**
+        * @test
+        */
+       public function isIntegerInRangeRejectsValueOutsideOfRange() {
+               $this->assertFalse(t3lib_utility_Math::isIntegerInRange(10, 1, 2));
+       }
+
+       /**
+        * Data provider or isIntegerInRangeRejectsOtherDataTypes
+        */
+       public function isIntegerInRangeRejectsOtherDataTypesDataProvider() {
+               return array(
+                       'negative integer' => array(-1),
+                       'float' => array(1.5),
+                       'string' => array('string'),
+                       'array' => array(array()),
+                       'object' => array(new stdClass()),
+                       'boolean FALSE' => array(FALSE),
+                       'NULL' => array(NULL),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
+        */
+       public function isIntegerInRangeRejectsOtherDataTypes($inputValue) {
+               $this->assertFalse(t3lib_utility_Math::isIntegerInRange($invalidValue, 0, 10));
+       }
+
 }
 
 ?>
\ No newline at end of file