diff --git a/src/AbstractBetweenParser.php b/src/AbstractBetweenParser.php new file mode 100644 index 0000000..674384c --- /dev/null +++ b/src/AbstractBetweenParser.php @@ -0,0 +1,24 @@ +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; + } +} diff --git a/src/FloatBetweenParser.php b/src/FloatBetweenParser.php new file mode 100644 index 0000000..f6d9de0 --- /dev/null +++ b/src/FloatBetweenParser.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/src/FloatParser.php b/src/FloatParser.php index bdf2c16..2bfb83d 100644 --- a/src/FloatParser.php +++ b/src/FloatParser.php @@ -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); + } } diff --git a/src/IntBetweenParser.php b/src/IntBetweenParser.php new file mode 100644 index 0000000..fc9b7cf --- /dev/null +++ b/src/IntBetweenParser.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/src/IntParser.php b/src/IntParser.php index 59d5bd6..1e6df7e 100644 --- a/src/IntParser.php +++ b/src/IntParser.php @@ -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); + } } diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 0c5036d..2faa8e8 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -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'), 'john@doe.com'], @@ -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(); + } }