diff --git a/src/FloatParser.php b/src/FloatParser.php index 2bfb83d..2fb8ffd 100644 --- a/src/FloatParser.php +++ b/src/FloatParser.php @@ -2,6 +2,12 @@ namespace MPScholten\RequestParser; +use MPScholten\RequestParser\Validator\FloatBetweenParser; +use MPScholten\RequestParser\Validator\FloatLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\FloatLargerThanParser; +use MPScholten\RequestParser\Validator\FloatSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\FloatSmallerThanParser; + class FloatParser extends AbstractValueParser { protected function describe() @@ -41,4 +47,40 @@ public function between($minValue, $maxValue) { return new FloatBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue); } + + /** + * @param int $minValue + * @return FloatLargerThanParser + */ + public function largerThan($minValue) + { + return new FloatLargerThanParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $minValue + * @return FloatLargerThanOrEqualToParser + */ + public function largerThanOrEqualTo($minValue) + { + return new FloatLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $maxValue + * @return FloatSmallerThanParser + */ + public function smallerThan($maxValue) + { + return new FloatSmallerThanParser($this->config, $this->name, $this->value, $maxValue); + } + + /** + * @param int $maxValue + * @return FloatSmallerThanOrEqualToParser + */ + public function smallerThanOrEqualTo($maxValue) + { + return new FloatSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); + } } diff --git a/src/IntParser.php b/src/IntParser.php index 1e6df7e..a011781 100644 --- a/src/IntParser.php +++ b/src/IntParser.php @@ -2,6 +2,12 @@ namespace MPScholten\RequestParser; +use MPScholten\RequestParser\Validator\IntBetweenParser; +use MPScholten\RequestParser\Validator\IntLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\IntLargerThanParser; +use MPScholten\RequestParser\Validator\IntSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\IntSmallerThanParser; + class IntParser extends AbstractValueParser { protected function describe() @@ -33,12 +39,48 @@ public function required($invalidValueMessage = null, $notFoundMessage = null) } /** - * @param int $minvalue + * @param int $minValue * @param int $maxValue * @return IntBetweenParser */ - public function between($minvalue, $maxValue) + public function between($minValue, $maxValue) + { + return new IntBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue); + } + + /** + * @param int $minValue + * @return IntLargerThanParser + */ + public function largerThan($minValue) + { + return new IntLargerThanParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $minValue + * @return IntLargerThanOrEqualToParser + */ + public function largerThanOrEqualTo($minValue) + { + return new IntLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $maxValue + * @return IntSmallerThanParser + */ + public function smallerThan($maxValue) + { + return new IntSmallerThanParser($this->config, $this->name, $this->value, $maxValue); + } + + /** + * @param int $maxValue + * @return IntSmallerThanOrEqualToParser + */ + public function smallerThanOrEqualTo($maxValue) { - return new IntBetweenParser($this->config, $this->name, $this->value, $minvalue, $maxValue); + return new IntSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); } } diff --git a/src/StringParser.php b/src/StringParser.php index b2b35bd..b5aaab2 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -2,6 +2,9 @@ namespace MPScholten\RequestParser; +use MPScholten\RequestParser\Validator\EmailParser; +use MPScholten\RequestParser\Validator\UrlParser; + class StringParser extends AbstractValueParser { protected function describe() diff --git a/src/TypeParser.php b/src/TypeParser.php index 7814df4..884aae8 100644 --- a/src/TypeParser.php +++ b/src/TypeParser.php @@ -2,6 +2,8 @@ namespace MPScholten\RequestParser; +use MPScholten\RequestParser\Validator\OneOfParser; + class TypeParser { private $value; diff --git a/src/AbstractBetweenParser.php b/src/Validator/AbstractBetweenParser.php similarity index 79% rename from src/AbstractBetweenParser.php rename to src/Validator/AbstractBetweenParser.php index 674384c..04fc2ec 100644 --- a/src/AbstractBetweenParser.php +++ b/src/Validator/AbstractBetweenParser.php @@ -1,6 +1,9 @@ minValue = $minValue; + parent::__construct($config, $name, $value); + } + + protected function parse($value) + { + if ($value >= $this->minValue) { + return $value; + } + return null; + } +} diff --git a/src/Validator/AbstractLargerThanParser.php b/src/Validator/AbstractLargerThanParser.php new file mode 100644 index 0000000..752eb1e --- /dev/null +++ b/src/Validator/AbstractLargerThanParser.php @@ -0,0 +1,25 @@ +minValue = $minValue; + parent::__construct($config, $name, $value); + } + + protected function parse($value) + { + if ($value > $this->minValue) { + return $value; + } + return null; + } +} diff --git a/src/Validator/AbstractSmallerThanOrEqualToParser.php b/src/Validator/AbstractSmallerThanOrEqualToParser.php new file mode 100644 index 0000000..710e583 --- /dev/null +++ b/src/Validator/AbstractSmallerThanOrEqualToParser.php @@ -0,0 +1,25 @@ +maxValue = $maxValue; + parent::__construct($config, $name, $value); + } + + protected function parse($value) + { + if ($value <= $this->maxValue) { + return $value; + } + return null; + } +} diff --git a/src/Validator/AbstractSmallerThanParser.php b/src/Validator/AbstractSmallerThanParser.php new file mode 100644 index 0000000..d3ad147 --- /dev/null +++ b/src/Validator/AbstractSmallerThanParser.php @@ -0,0 +1,25 @@ +maxValue = $maxValue; + parent::__construct($config, $name, $value); + } + + protected function parse($value) + { + if ($value < $this->maxValue) { + return $value; + } + return null; + } +} diff --git a/src/EmailParser.php b/src/Validator/EmailParser.php similarity index 91% rename from src/EmailParser.php rename to src/Validator/EmailParser.php index d4bcb79..f6507eb 100644 --- a/src/EmailParser.php +++ b/src/Validator/EmailParser.php @@ -1,6 +1,8 @@ minValue"; + } + + /** + * @param $value + * @return int + */ + 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/Validator/FloatLargerThanParser.php b/src/Validator/FloatLargerThanParser.php new file mode 100644 index 0000000..296d72e --- /dev/null +++ b/src/Validator/FloatLargerThanParser.php @@ -0,0 +1,44 @@ +minValue"; + } + + /** + * @param $value + * @return int + */ + 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/Validator/FloatSmallerThanOrEqualToParser.php b/src/Validator/FloatSmallerThanOrEqualToParser.php new file mode 100644 index 0000000..4cd17f6 --- /dev/null +++ b/src/Validator/FloatSmallerThanOrEqualToParser.php @@ -0,0 +1,44 @@ +maxValue"; + } + + /** + * @param $value + * @return int + */ + 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/Validator/FloatSmallerThanParser.php b/src/Validator/FloatSmallerThanParser.php new file mode 100644 index 0000000..ee28d47 --- /dev/null +++ b/src/Validator/FloatSmallerThanParser.php @@ -0,0 +1,44 @@ +maxValue"; + } + + /** + * @param $value + * @return int + */ + 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/IntBetweenParser.php b/src/Validator/IntBetweenParser.php similarity index 95% rename from src/IntBetweenParser.php rename to src/Validator/IntBetweenParser.php index fc9b7cf..a337116 100644 --- a/src/IntBetweenParser.php +++ b/src/Validator/IntBetweenParser.php @@ -1,6 +1,6 @@ minValue"; + } + + /** + * @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/Validator/IntLargerThanParser.php b/src/Validator/IntLargerThanParser.php new file mode 100644 index 0000000..1126c06 --- /dev/null +++ b/src/Validator/IntLargerThanParser.php @@ -0,0 +1,44 @@ +minValue"; + } + + /** + * @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/Validator/IntSmallerThanOrEqualToParser.php b/src/Validator/IntSmallerThanOrEqualToParser.php new file mode 100644 index 0000000..0b46294 --- /dev/null +++ b/src/Validator/IntSmallerThanOrEqualToParser.php @@ -0,0 +1,44 @@ +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/Validator/IntSmallerThanParser.php b/src/Validator/IntSmallerThanParser.php new file mode 100644 index 0000000..4a1eb2b --- /dev/null +++ b/src/Validator/IntSmallerThanParser.php @@ -0,0 +1,44 @@ +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/OneOfParser.php b/src/Validator/OneOfParser.php similarity index 87% rename from src/OneOfParser.php rename to src/Validator/OneOfParser.php index fd85ee8..ec367c2 100644 --- a/src/OneOfParser.php +++ b/src/Validator/OneOfParser.php @@ -1,6 +1,9 @@ assertEquals(101.12, $parser->required()); } + public function testLargerThanValidatorWithValidValues() + { + $parser = new IntParser(new Config(), 'groupId', 1); + $parser->largerThan(0); + $this->assertEquals(1, $parser->required()); + + $parser = new IntParser(new Config(), 'groupId', 1); + $parser->largerThanOrEqualTo(1); + $this->assertEquals(1, $parser->required()); + + $parser = new FloatParser(new Config(), 'precipitation', 1.01); + $parser->largerThan(1.001); + $this->assertEquals(1.01, $parser->required()); + + $parser = new FloatParser(new Config(), 'precipitation', 1.01); + $parser->largerThanOrEqualTo(1.01); + $this->assertEquals(1.01, $parser->required()); + } + + public function testSmallerThanValidatorWithValidValues() + { + $parser = new IntParser(new Config(), 'groupId', -1); + $parser->smallerThan(0); + $this->assertEquals(-1, $parser->required()); + + $parser = new IntParser(new Config(), 'groupId', -1); + $parser->largerThanOrEqualTo(-1); + $this->assertEquals(-1, $parser->required()); + + $parser = new FloatParser(new Config(), 'precipitation', -2.01); + $parser->largerThan(-3.01); + $this->assertEquals(-2.01, $parser->required()); + + $parser = new FloatParser(new Config(), 'precipitation', -2.01); + $parser->largerThanOrEqualTo(-2.01); + $this->assertEquals(-2.01, $parser->required()); + } + public function testIntBetweenValidatorWithValuesOutOfRange() { $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer between 1 and 6, but got "7"'); @@ -179,4 +217,60 @@ public function testFloatBetweenValidatorWithValuesOutOfRange() $parser = new FloatParser(new Config(), 'precipitation', 101.13); $precipitation = $parser->between(60.99, 101.12)->required(); } + + public function testIntLargerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer larger than 1, but got "0"'); + $parser = new IntParser(new Config(), 'groupId', 0); + $groupId = $parser->largerThan(1)->required(); + } + + public function testIntLargerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer larger than or equal to 1, but got "0"'); + $parser = new IntParser(new Config(), 'groupId', 0); + $groupId = $parser->largerThanOrEqualTo(1)->required(); + } + + public function testFloatLargerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "precipitation". Expected a float larger than 1.01, but got "0.01"'); + $parser = new FloatParser(new Config(), 'precipitation', 0.01); + $precipitation = $parser->largerThan(1.01)->required(); + } + + public function testFloatLargerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "precipitation". Expected a float larger than or equal to 1.01, but got "0.01"'); + $parser = new FloatParser(new Config(), 'precipitation', 0.01); + $precipitation = $parser->largerThanOrEqualTo(1.01)->required(); + } + + public function testIntSmallerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer smaller than 0, but got "1"'); + $parser = new IntParser(new Config(), 'groupId', 1); + $groupId = $parser->smallerThan(0)->required(); + } + + public function testIntSmallerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer smaller than or equal to 0, but got "1"'); + $parser = new IntParser(new Config(), 'groupId', 1); + $groupId = $parser->smallerThanOrEqualTo(0)->required(); + } + + public function testFloatSmallerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "precipitation". Expected a float smaller than 0.01, but got "1.01"'); + $parser = new FloatParser(new Config(), 'precipitation', 1.01); + $precipitation = $parser->smallerThan(0.01)->required(); + } + + public function testFloatSmallerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "precipitation". Expected a float smaller than or equal to 0.01, but got "1.01"'); + $parser = new FloatParser(new Config(), 'precipitation', 1.01); + $precipitation = $parser->smallerThanOrEqualTo(0.01)->required(); + } } diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index 9ec4419..ae62bce 100644 --- a/tests/TypeSpecTest.php +++ b/tests/TypeSpecTest.php @@ -11,18 +11,24 @@ use MPScholten\RequestParser\CommaSeparatedYesNoBooleanParser; use MPScholten\RequestParser\Config; use MPScholten\RequestParser\DateTimeParser; -use MPScholten\RequestParser\ExceptionMessageFactory; -use MPScholten\RequestParser\EmailParser; -use MPScholten\RequestParser\ExceptionFactory; +use MPScholten\RequestParser\Validator\EmailParser; +use MPScholten\RequestParser\Validator\FloatBetweenParser; +use MPScholten\RequestParser\Validator\IntBetweenParser; use MPScholten\RequestParser\IntParser; use MPScholten\RequestParser\FloatParser; use MPScholten\RequestParser\TrimParser; -use MPScholten\RequestParser\UrlParser; +use MPScholten\RequestParser\Validator\UrlParser; +use MPScholten\RequestParser\Validator\FloatLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\FloatLargerThanParser; +use MPScholten\RequestParser\Validator\FloatSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\FloatSmallerThanParser; +use MPScholten\RequestParser\Validator\IntLargerThanParser; +use MPScholten\RequestParser\Validator\IntSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\IntSmallerThanParser; use MPScholten\RequestParser\YesNoBooleanParser; use MPScholten\RequestParser\BooleanParser; use MPScholten\RequestParser\JsonParser; -use MPScholten\RequestParser\NotFoundException; -use MPScholten\RequestParser\OneOfParser; +use MPScholten\RequestParser\Validator\OneOfParser; use MPScholten\RequestParser\StringParser; use MPScholten\RequestParser\TypeParser; @@ -58,6 +64,66 @@ public function testEmail() $this->assertInstanceOf(EmailParser::class, $spec->string()->email()); } + public function testIntBetween() + { + $spec = new TypeParser(new Config(), 'groupId', '1'); + $this->assertInstanceOf(IntBetweenParser::class, $spec->int()->between(0, 100)); + } + + public function testIntLargerThan() + { + $spec = new TypeParser(new Config(), 'groupId', '1'); + $this->assertInstanceOf(IntLargerThanParser::class, $spec->int()->largerThan(0)); + } + + public function testIntLargerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', '1'); + $this->assertInstanceOf(IntLargerThanParser::class, $spec->int()->largerThan(1)); + } + + public function testIntSmallerThan() + { + $spec = new TypeParser(new Config(), 'groupId', '-1'); + $this->assertInstanceOf(IntSmallerThanParser::class, $spec->int()->smallerThan(0)); + } + + public function testIntSmallerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', '1'); + $this->assertInstanceOf(IntSmallerThanOrEqualToParser::class, $spec->int()->smallerThanOrEqualTo(1)); + } + + public function testFloatLargerThan() + { + $spec = new TypeParser(new Config(), 'precipitation', '1.01'); + $this->assertInstanceOf(FloatLargerThanParser::class, $spec->float()->largerThan(0)); + } + + public function testFloatLargerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'precipitation', '1.01'); + $this->assertInstanceOf(FloatLargerThanOrEqualToParser::class, $spec->float()->largerThanOrEqualTo(1.01)); + } + + public function testFloatSmallerThan() + { + $spec = new TypeParser(new Config(), 'precipitation', '-1.19'); + $this->assertInstanceOf(FloatSmallerThanParser::class, $spec->float()->smallerThan(-1)); + } + + public function testFloatSmallerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'precipitation', '-1.19'); + $this->assertInstanceOf(FloatSmallerThanOrEqualToParser::class, $spec->float()->smallerThanOrEqualTo(-1.19)); + } + + public function testFloatBetween() + { + $spec = new TypeParser(new Config(), 'precipitation', '101.39'); + $this->assertInstanceOf(FloatBetweenParser::class, $spec->float()->between(0.01, 1000.09)); + } + public function testTrim() { $spec = new TypeParser(new Config(), 'emailAddress', ' john@doe.com ');