Thanks to visit codestin.com
Credit goes to github.com

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

Commit 3e0fb0f

Browse files
committed
Merge branch 'hotfix/zf2-572' into develop
Forward port zendframework/zendframework#2401

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/Step.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public function isValid($value)
127127

128128
$this->setValue($value);
129129

130-
if ($this->fmod($value - $this->baseValue, $this->step) !== 0.0) {
130+
$fmod = $this->fmod($value - $this->baseValue, $this->step);
131+
132+
if ($fmod !== 0.0 && $fmod !== $this->step) {
131133
$this->error(self::NOT_STEP);
132134
return false;
133135
}
@@ -147,6 +149,10 @@ protected function fmod($x, $y)
147149
if ($y == 0.0) {
148150
return 1.0;
149151
}
150-
return $x - $y * floor($x / $y);
152+
153+
//find the maximum precision from both input params to give accurate results
154+
$precision = strlen(substr($x, strpos($x, '.')+1)) + strlen(substr($y, strpos($y, '.')+1));
155+
156+
return round($x - $y * floor($x / $y), $precision);
151157
}
152158
}

test/StepTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ public function testDecimalStep()
9696
array(0.1, false),
9797
array(2.1, true),
9898
array(3.1, false),
99+
array(4.2, true),
100+
array(6.3, true),
101+
array(8.4, true),
99102
array(10.5, true),
103+
array(12.6, true),
104+
array(14.7, true),
105+
array(16.8, true),
106+
array(18.9, true),
107+
array(21.0, true),
100108
array('2.1', true),
101109
array('1.1', false),
102110
array(1.11, false),
@@ -114,6 +122,40 @@ public function testDecimalStep()
114122
}
115123
}
116124

125+
public function testDecimalStep2()
126+
{
127+
$valuesExpected = array(
128+
array(0.01, true),
129+
array(0.02, true),
130+
array(0.03, true),
131+
array(0.04, true),
132+
array(0.05, true),
133+
array(0.06, true),
134+
array(0.07, true),
135+
array(0.08, true),
136+
array(0.09, true),
137+
array(0.001, false),
138+
array(0.002, false),
139+
array(0.003, false),
140+
array(0.004, false),
141+
array(0.005, false),
142+
array(0.006, false),
143+
array(0.007, false),
144+
array(0.008, false),
145+
array(0.009, false)
146+
);
147+
148+
$validator = new Validator\Step(array(
149+
'baseValue' => 0,
150+
'step' => 0.01
151+
));
152+
153+
foreach ($valuesExpected as $element) {
154+
$this->assertEquals($element[1], $validator->isValid($element[0]),
155+
'Test failed with ' . var_export($element, 1));
156+
}
157+
}
158+
117159
/**
118160
* Ensures that getMessages() returns expected default value
119161
*

0 commit comments

Comments
 (0)