Skip to content

Commit

Permalink
Added $step parameter to Valid::range(), fixes #4043
Browse files Browse the repository at this point in the history
  • Loading branch information
Woody Gilk committed Jan 9, 2012
1 parent c9e8d02 commit 23e1b0b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
18 changes: 16 additions & 2 deletions classes/kohana/valid.php
Expand Up @@ -473,11 +473,25 @@ public static function numeric($str)
* @param string $number number to check
* @param integer $min minimum value
* @param integer $max maximum value
* @param integer $step increment size
* @return boolean
*/
public static function range($number, $min, $max)
public static function range($number, $min, $max, $step = NULL)
{
return ($number >= $min AND $number <= $max);
if ($number <= $min OR $number >= $max)
{
// Number is outside of range
return FALSE;
}

if ( ! $step)
{
// Default to steps of 1
$step = 1;
}

// Check step requirements
return (($number - $min) % $step === 0);
}

/**
Expand Down
25 changes: 14 additions & 11 deletions tests/kohana/ValidTest.php
Expand Up @@ -848,16 +848,19 @@ public function test_regex($value, $regex, $expected)
public function provider_range()
{
return array(
array(1, 0, 2, TRUE),
array(-1, -5, 0, TRUE),
array(-1, 0, 1, FALSE),
array(1, 0, 0, FALSE),
array(2147483647, 0, 200000000000000, TRUE),
array(-2147483647, -2147483655, 2147483645, TRUE),
array(1, 0, 2, NULL, TRUE),
array(-1, -5, 0, NULL, TRUE),
array(-1, 0, 1, NULL, FALSE),
array(1, 0, 0, NULL, FALSE),
array(2147483647, 0, 200000000000000, NULL, TRUE),
array(-2147483647, -2147483655, 2147483645, NULL, TRUE),
// #4043
array(2, 0, 10, 2, TRUE),
array(3, 0, 10, 2, FALSE),
// Empty test
array('', 5, 10, FALSE),
array(NULL, 5, 10, FALSE),
array(FALSE, 5, 10, FALSE),
array('', 5, 10, NULL, FALSE),
array(NULL, 5, 10, NULL, FALSE),
array(FALSE, 5, 10, NULL, FALSE),
);
}

Expand All @@ -873,11 +876,11 @@ public function provider_range()
* @param integer $max Upper bound
* @param boolean $expected Is Number within the bounds of $min && $max
*/
public function test_range($number, $min, $max, $expected)
public function test_range($number, $min, $max, $step, $expected)
{
$this->AssertSame(
$expected,
Valid::range($number, $min, $max)
Valid::range($number, $min, $max, $step)
);
}

Expand Down

0 comments on commit 23e1b0b

Please sign in to comment.