Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zf2-572'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public function isValid($value)

$this->setValue($value);

if ($this->fmod($value - $this->baseValue, $this->step) !== 0.0) {
$fmod = $this->fmod($value - $this->baseValue, $this->step);

if ($fmod !== 0.0 && $fmod !== $this->step) {
$this->error(self::NOT_STEP);
return false;
}
Expand All @@ -147,6 +149,10 @@ protected function fmod($x, $y)
if ($y == 0.0) {
return 1.0;
}
return $x - $y * floor($x / $y);

//find the maximum precision from both input params to give accurate results
$precision = strlen(substr($x, strpos($x, '.')+1)) + strlen(substr($y, strpos($y, '.')+1));

return round($x - $y * floor($x / $y), $precision);
}
}
42 changes: 42 additions & 0 deletions test/StepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ public function testDecimalStep()
array(0.1, false),
array(2.1, true),
array(3.1, false),
array(4.2, true),
array(6.3, true),
array(8.4, true),
array(10.5, true),
array(12.6, true),
array(14.7, true),
array(16.8, true),
array(18.9, true),
array(21.0, true),
array('2.1', true),
array('1.1', false),
array(1.11, false),
Expand All @@ -114,6 +122,40 @@ public function testDecimalStep()
}
}

public function testDecimalStep2()
{
$valuesExpected = array(
array(0.01, true),
array(0.02, true),
array(0.03, true),
array(0.04, true),
array(0.05, true),
array(0.06, true),
array(0.07, true),
array(0.08, true),
array(0.09, true),
array(0.001, false),
array(0.002, false),
array(0.003, false),
array(0.004, false),
array(0.005, false),
array(0.006, false),
array(0.007, false),
array(0.008, false),
array(0.009, false)
);

$validator = new Validator\Step(array(
'baseValue' => 0,
'step' => 0.01
));

foreach ($valuesExpected as $element) {
$this->assertEquals($element[1], $validator->isValid($element[0]),
'Test failed with ' . var_export($element, 1));
}
}

/**
* Ensures that getMessages() returns expected default value
*
Expand Down

0 comments on commit 3f1f758

Please sign in to comment.