Skip to content

Commit

Permalink
Merge pull request #32 from mpscholten/numeric-range-validator
Browse files Browse the repository at this point in the history
Numeric range validator
  • Loading branch information
mpscholten authored Aug 18, 2016
2 parents 6fc0977 + 9a8b69a commit 3e9ea49
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/AbstractBetweenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MPScholten\RequestParser;

abstract class AbstractBetweenParser extends AbstractValueParser
{
protected $minValue;
protected $maxValue;

public function __construct(Config $config, $name, $value, $minValue, $maxValue)
{
$this->minValue = $minValue;
$this->maxValue = $maxValue;
parent::__construct($config, $name, $value);
}

protected function parse($value)
{
if ($value >= $this->minValue && $value <= $this->maxValue) {
return $value;
}
return null;
}
}
44 changes: 44 additions & 0 deletions src/FloatBetweenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser;

class FloatBetweenParser extends AbstractBetweenParser
{
protected function describe()
{
return "a float between $this->minValue and $this->maxValue";
}

/**
* @param $value
* @return float
*/
protected function parse($value)
{
if (!is_numeric($value)) {
return null;
}
$value = (float) $value;
return parent::parse($value);
}

/**
* @param float $defaultValue
* @return float
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return float
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
10 changes: 10 additions & 0 deletions src/FloatParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}

/**
* @param float $minValue
* @param float $maxValue
* @return FloatBetweenParser
*/
public function between($minValue, $maxValue)
{
return new FloatBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue);
}
}
44 changes: 44 additions & 0 deletions src/IntBetweenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser;

class IntBetweenParser extends AbstractBetweenParser
{
protected function describe()
{
return "an integer between $this->minValue and $this->maxValue";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (!is_numeric($value)) {
return null;
}
$value = (int) $value;
return parent::parse($value);
}

/**
* @param int $defaultValue
* @return int
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return int
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
10 changes: 10 additions & 0 deletions src/IntParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}

/**
* @param int $minvalue
* @param int $maxValue
* @return IntBetweenParser
*/
public function between($minvalue, $maxValue)
{
return new IntBetweenParser($this->config, $this->name, $this->value, $minvalue, $maxValue);
}
}
33 changes: 32 additions & 1 deletion tests/ParserSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function specWithInvalidValueAndDefaultValue()
{
return [
[new IntParser(new Config(), 'id', 'string instead of an int'), 1],
[new FloatParser(new Config(), 'ration', 'string instead of an float'), 0.91],
[new FloatParser(new Config(), 'ration', 'string instead of a float'), 0.91],
[new YesNoBooleanParser(new Config(), 'isAwesome', 'invalid'), false],
[new BooleanParser(new Config(), 'isAwesome', 'invalid'), false],
[new EmailParser(new Config(), 'emailAddress', 'invalid_email'), '[email protected]'],
Expand Down Expand Up @@ -146,4 +146,35 @@ public function testStringSpecific()
$parser = new StringParser(new Config(), 'name', 'test');
$this->assertEquals('test', $parser->defaultsToIfEmpty('default'));
}

public function testBetweenValidatorWithValidValues()
{
$parser = new IntParser(new Config(), 'groupId', 1);
$parser->between(1, 6);
$this->assertEquals(1, $parser->required());
$parser = new IntParser(new Config(), 'groupId', 6);
$parser->between(1, 6);
$this->assertEquals(6, $parser->required());

$parser = new FloatParser(new Config(), 'precipitation', 60.99);
$parser->between(60.99, 101.12);
$this->assertEquals(60.99, $parser->required());
$parser = new FloatParser(new Config(), 'precipitation', 101.12);
$parser->between(60.99, 101.12);
$this->assertEquals(101.12, $parser->required());
}

public function testIntBetweenValidatorWithValuesOutOfRange()
{
$this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer between 1 and 6, but got "7"');
$parser = new IntParser(new Config(), 'groupId', 7);
$groupId = $parser->between(1, 6)->required();
}

public function testFloatBetweenValidatorWithValuesOutOfRange()
{
$this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "precipitation". Expected a float between 60.99 and 101.12, but got "101.13"');
$parser = new FloatParser(new Config(), 'precipitation', 101.13);
$precipitation = $parser->between(60.99, 101.12)->required();
}
}

0 comments on commit 3e9ea49

Please sign in to comment.