-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from mpscholten/numeric-range-validator
Numeric range validator
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]'], | ||
|
@@ -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(); | ||
} | ||
} |