Skip to content

Commit

Permalink
Merge pull request #14 from lohanidamodar/fix-range-type
Browse files Browse the repository at this point in the history
fix range get type
  • Loading branch information
eldadfux authored May 21, 2021
2 parents 451e665 + a9a5b01 commit 6321132
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
46 changes: 40 additions & 6 deletions src/Validator/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -54,19 +61,28 @@ 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
*
* Returns validator description
*
* @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);
}
Expand All @@ -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;
}
Expand Down
47 changes: 33 additions & 14 deletions tests/Validator/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 6321132

Please sign in to comment.