Skip to content

Commit

Permalink
Fix casting error in AbstractInterval classes
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
henriquemoody committed Jul 28, 2018
1 parent 4f3aa90 commit 47f2159
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions library/Rules/AbstractInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function __construct($interval, $inclusive = true)
$this->inclusive = $inclusive;
}

protected function isAbleToCompareValues($left, $right)
{
return is_scalar($left) === is_scalar($right);
}

protected function filterInterval($value)
{
if (!is_string($value) || is_numeric($value) || empty($value)) {
Expand Down
11 changes: 9 additions & 2 deletions library/Rules/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ class Max extends AbstractInterval
{
public function validate($input)
{
$filteredInput = $this->filterInterval($input);
$filteredInterval = $this->filterInterval($this->interval);

if (!$this->isAbleToCompareValues($filteredInput, $filteredInterval)) {
return false;
}

if ($this->inclusive) {
return $this->filterInterval($input) <= $this->filterInterval($this->interval);
return $filteredInput <= $filteredInterval;
}

return $this->filterInterval($input) < $this->filterInterval($this->interval);
return $filteredInput < $filteredInterval;
}
}
11 changes: 9 additions & 2 deletions library/Rules/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ class Min extends AbstractInterval
{
public function validate($input)
{
$filteredInput = $this->filterInterval($input);
$filteredInterval = $this->filterInterval($this->interval);

if (!$this->isAbleToCompareValues($filteredInput, $filteredInterval)) {
return false;
}

if ($this->inclusive) {
return $this->filterInterval($input) >= $this->filterInterval($this->interval);
return $filteredInput >= $filteredInterval;
}

return $this->filterInterval($input) > $this->filterInterval($this->interval);
return $filteredInput > $filteredInterval;
}
}
2 changes: 2 additions & 0 deletions tests/unit/Rules/MaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function providerForInvalidMax()
[200, false, 250],
[200, false, 1500],
[200, false, 200],
[1900, false, '2018-01-25'],
[10.5, false, '2018-01-25'],
];
}
}
2 changes: 2 additions & 0 deletions tests/unit/Rules/MinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function providerForInvalidMin()
[0, false, -250],
[0, false, -50],
[50, false, 50],
[2040, false, '2018-01-25'],
[10.5, false, '2018-01-25'],
];
}
}

0 comments on commit 47f2159

Please sign in to comment.