Skip to content

Commit 47f2159

Browse files
committed
Fix casting error in AbstractInterval classes
The classes that are children of "AbstractInterval" convert their values before comparing them. Because PHP tries to convert values when making comparisons and an "DateTime" object cannot be converted to integer or float some validations would result into PHP triggering an error like: > Object of class DateTime could not be converted to int > Object of class DateTime could not be converted to float This commit prevents that to happen by verifying if both compared values are scalar or not before comparing them with each other. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
1 parent 4f3aa90 commit 47f2159

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

library/Rules/AbstractInterval.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function __construct($interval, $inclusive = true)
2525
$this->inclusive = $inclusive;
2626
}
2727

28+
protected function isAbleToCompareValues($left, $right)
29+
{
30+
return is_scalar($left) === is_scalar($right);
31+
}
32+
2833
protected function filterInterval($value)
2934
{
3035
if (!is_string($value) || is_numeric($value) || empty($value)) {

library/Rules/Max.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ class Max extends AbstractInterval
1515
{
1616
public function validate($input)
1717
{
18+
$filteredInput = $this->filterInterval($input);
19+
$filteredInterval = $this->filterInterval($this->interval);
20+
21+
if (!$this->isAbleToCompareValues($filteredInput, $filteredInterval)) {
22+
return false;
23+
}
24+
1825
if ($this->inclusive) {
19-
return $this->filterInterval($input) <= $this->filterInterval($this->interval);
26+
return $filteredInput <= $filteredInterval;
2027
}
2128

22-
return $this->filterInterval($input) < $this->filterInterval($this->interval);
29+
return $filteredInput < $filteredInterval;
2330
}
2431
}

library/Rules/Min.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ class Min extends AbstractInterval
1515
{
1616
public function validate($input)
1717
{
18+
$filteredInput = $this->filterInterval($input);
19+
$filteredInterval = $this->filterInterval($this->interval);
20+
21+
if (!$this->isAbleToCompareValues($filteredInput, $filteredInterval)) {
22+
return false;
23+
}
24+
1825
if ($this->inclusive) {
19-
return $this->filterInterval($input) >= $this->filterInterval($this->interval);
26+
return $filteredInput >= $filteredInterval;
2027
}
2128

22-
return $this->filterInterval($input) > $this->filterInterval($this->interval);
29+
return $filteredInput > $filteredInterval;
2330
}
2431
}

tests/unit/Rules/MaxTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function providerForInvalidMax()
6262
[200, false, 250],
6363
[200, false, 1500],
6464
[200, false, 200],
65+
[1900, false, '2018-01-25'],
66+
[10.5, false, '2018-01-25'],
6567
];
6668
}
6769
}

tests/unit/Rules/MinTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function providerForInvalidMin()
7070
[0, false, -250],
7171
[0, false, -50],
7272
[50, false, 50],
73+
[2040, false, '2018-01-25'],
74+
[10.5, false, '2018-01-25'],
7375
];
7476
}
7577
}

0 commit comments

Comments
 (0)