diff --git a/src/Validator/Range.php b/src/Validator/Range.php index 0c631f06..201648f5 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -31,21 +31,28 @@ class Range extends Numeric */ protected $max; + /** + * @var string + */ + protected $format; + /** * @param int $min * @param int $max + * @param string $format */ - public function __construct($min, $max) + public function __construct($min, $max, $format = self::TYPE_INTEGER) { $this->min = $min; $this->max = $max; + $this->format = $format; } /** * Get Range Minimum Value * @return int */ - public function getMin() + public function getMin(): int { return $this->min; } @@ -54,11 +61,20 @@ public function getMin() * Get Range Maximum Value * @return int */ - public function getMax() + public function getMax(): int { return $this->max; } + /** + * Get Range Format + * @return string + */ + public function getFormat(): string + { + return $this->format; + } + /** * Get Description * @@ -66,7 +82,7 @@ public function getMax() * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Value must be a valid range between ' . \number_format($this->min) . ' and ' . \number_format($this->max); } @@ -92,23 +108,41 @@ public function isArray(): bool */ public function getType(): string { - return self::TYPE_INTEGER; + return $this->format; } /** * Is valid * * Validation will pass when $value number is bigger or equal than $min number and lower or equal than $max. + * Not strict, considers any valid integer to be a valid float * * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (!parent::isValid($value)) { return false; } + switch ($this->format) { + case self::TYPE_INTEGER: + $value = $value+0; + if(!is_int($value)) { + return false; + } + break; + case self::TYPE_FLOAT: + $value = $value+0; + if(!is_float($value) && !is_int($value)) { + return false; + } + break; + default: + return false; + } + if ($this->min <= $value && $this->max >= $value) { return true; } diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index 88268b33..05593288 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -20,11 +20,13 @@ class RangeTest extends TestCase /** * @var Range */ - protected $range = null; + protected $rangeFloat = null; + protected $rangeInt = null; public function setUp():void { - $this->range = new Range(0, 5); + $this->rangeFloat = new Range(0, 1, \Utopia\Validator::TYPE_FLOAT); + $this->rangeInt = new Range(0, 5, \Utopia\Validator::TYPE_INTEGER); } public function tearDown():void @@ -34,17 +36,34 @@ public function tearDown():void public function testIsValid() { - // Assertions - $this->assertEquals($this->range->isValid(0), true); - $this->assertEquals($this->range->isValid(1), true); - $this->assertEquals($this->range->isValid(4), true); - $this->assertEquals($this->range->isValid(5), true); - $this->assertEquals($this->range->isValid('5'), true); - $this->assertEquals($this->range->isValid(6), false); - $this->assertEquals($this->range->isValid(-1), false); - $this->assertEquals($this->range->getMin(), 0); - $this->assertEquals($this->range->getMax(), 5); - $this->assertEquals($this->range->isArray(), false); - $this->assertEquals($this->range->getType(), \Utopia\Validator::TYPE_INTEGER); + // Assertions for integer + $this->assertEquals($this->rangeInt->isValid(0), true); + $this->assertEquals($this->rangeInt->isValid(1), true); + $this->assertEquals($this->rangeInt->isValid(4), true); + $this->assertEquals($this->rangeInt->isValid(5), true); + $this->assertEquals($this->rangeInt->isValid('5'), true); + $this->assertEquals($this->rangeInt->isValid('1.5'), false); + $this->assertEquals($this->rangeInt->isValid(6), false); + $this->assertEquals($this->rangeInt->isValid(-1), false); + $this->assertEquals($this->rangeInt->getMin(), 0); + $this->assertEquals($this->rangeInt->getMax(), 5); + $this->assertEquals($this->rangeInt->getFormat(), \Utopia\Validator::TYPE_INTEGER); + $this->assertEquals($this->rangeInt->isArray(), false); + $this->assertEquals($this->rangeInt->getType(), \Utopia\Validator::TYPE_INTEGER); + + // Assertions for float + $this->assertEquals($this->rangeFloat->isValid(0.0), true); + $this->assertEquals($this->rangeFloat->isValid(1.0), true); + $this->assertEquals($this->rangeFloat->isValid(0.5), true); + $this->assertEquals($this->rangeFloat->isValid('0.5'), true); + $this->assertEquals($this->rangeFloat->isValid(4), false); + $this->assertEquals($this->rangeFloat->isValid('0.6'), true); + $this->assertEquals($this->rangeFloat->isValid(1.5), false); + $this->assertEquals($this->rangeFloat->isValid(-1), false); + $this->assertEquals($this->rangeFloat->getMin(), 0); + $this->assertEquals($this->rangeFloat->getMax(), 1); + $this->assertEquals($this->rangeFloat->getFormat(), \Utopia\Validator::TYPE_FLOAT); + $this->assertEquals($this->rangeFloat->isArray(), false); + $this->assertEquals($this->rangeFloat->getType(), \Utopia\Validator::TYPE_FLOAT); } }