From e5440d809c22a8c1bb2579d1ba6992f04028f641 Mon Sep 17 00:00:00 2001 From: Karsten Lettow Date: Wed, 1 Mar 2017 17:42:36 +0100 Subject: [PATCH] added string validation --- src/StringParser.php | 56 +++++++++++++++++- src/Validator/StringBetweenParser.php | 44 ++++++++++++++ .../StringLargerThanOrEqualToParser.php | 44 ++++++++++++++ src/Validator/StringLargerThanParser.php | 44 ++++++++++++++ .../StringSmallerThanOrEqualToParser.php | 44 ++++++++++++++ src/Validator/StringSmallerThanParser.php | 44 ++++++++++++++ tests/ParserSpecTest.php | 58 +++++++++++++++++++ tests/TypeSpecTest.php | 38 +++++++++++- 8 files changed, 370 insertions(+), 2 deletions(-) create mode 100644 src/Validator/StringBetweenParser.php create mode 100644 src/Validator/StringLargerThanOrEqualToParser.php create mode 100644 src/Validator/StringLargerThanParser.php create mode 100644 src/Validator/StringSmallerThanOrEqualToParser.php create mode 100644 src/Validator/StringSmallerThanParser.php diff --git a/src/StringParser.php b/src/StringParser.php index b5aaab2..731f635 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -3,6 +3,11 @@ namespace MPScholten\RequestParser; use MPScholten\RequestParser\Validator\EmailParser; +use MPScholten\RequestParser\Validator\StringBetweenParser; +use MPScholten\RequestParser\Validator\StringLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLargerThanParser; +use MPScholten\RequestParser\Validator\StringSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringSmallerThanParser; use MPScholten\RequestParser\Validator\UrlParser; class StringParser extends AbstractValueParser @@ -27,7 +32,10 @@ public function defaultsTo($defaultValue) } /** - * @return string + * @param string $invalidValueMessage + * @param string $notFoundMessage + * + * @return null */ public function required($invalidValueMessage = null, $notFoundMessage = null) { @@ -71,4 +79,50 @@ public function rightTrim() { return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM); } + + /** + * @param int $minValue + * @param int $maxValue + * @return StringBetweenParser + */ + public function between($minValue, $maxValue) + { + return new StringBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue); + } + + /** + * @param int $minValue + * @return StringLargerThanParser + */ + public function largerThan($minValue) + { + return new StringLargerThanParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $minValue + * @return StringLargerThanOrEqualToParser + */ + public function largerThanOrEqualTo($minValue) + { + return new StringLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $maxValue + * @return StringSmallerThanParser + */ + public function smallerThan($maxValue) + { + return new StringSmallerThanParser($this->config, $this->name, $this->value, $maxValue); + } + + /** + * @param int $maxValue + * @return StringSmallerThanOrEqualToParser + */ + public function smallerThanOrEqualTo($maxValue) + { + return new StringSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); + } } diff --git a/src/Validator/StringBetweenParser.php b/src/Validator/StringBetweenParser.php new file mode 100644 index 0000000..6d50a4b --- /dev/null +++ b/src/Validator/StringBetweenParser.php @@ -0,0 +1,44 @@ +minValue and $this->maxValue"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (!is_string($value)) { + return null; + } + $value = strlen($value); + return parent::parse($value); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @param string $invalidValueMessage + * @param string $notFoundMessage + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } +} diff --git a/src/Validator/StringLargerThanOrEqualToParser.php b/src/Validator/StringLargerThanOrEqualToParser.php new file mode 100644 index 0000000..2b28660 --- /dev/null +++ b/src/Validator/StringLargerThanOrEqualToParser.php @@ -0,0 +1,44 @@ +minValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (!is_string($value)) { + return null; + } + $value = strlen($value); + return parent::parse($value); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @param string $invalidValueMessage + * @param string $notFoundMessage + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } +} \ No newline at end of file diff --git a/src/Validator/StringLargerThanParser.php b/src/Validator/StringLargerThanParser.php new file mode 100644 index 0000000..c1696da --- /dev/null +++ b/src/Validator/StringLargerThanParser.php @@ -0,0 +1,44 @@ +minValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (!is_string($value)) { + return null; + } + $value = strlen($value); + return parent::parse($value); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @param string $invalidValueMessage + * @param string $notFoundMessage + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } +} \ No newline at end of file diff --git a/src/Validator/StringSmallerThanOrEqualToParser.php b/src/Validator/StringSmallerThanOrEqualToParser.php new file mode 100644 index 0000000..66095d7 --- /dev/null +++ b/src/Validator/StringSmallerThanOrEqualToParser.php @@ -0,0 +1,44 @@ +maxValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (!is_string($value)) { + return null; + } + $value = strlen($value); + return parent::parse($value); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @param string $invalidValueMessage + * @param string $notFoundMessage + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } +} \ No newline at end of file diff --git a/src/Validator/StringSmallerThanParser.php b/src/Validator/StringSmallerThanParser.php new file mode 100644 index 0000000..a1f778b --- /dev/null +++ b/src/Validator/StringSmallerThanParser.php @@ -0,0 +1,44 @@ +maxValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (!is_string($value)) { + return null; + } + $value = strlen($value); + return parent::parse($value); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @param string $invalidValueMessage + * @param string $notFoundMessage + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } +} \ No newline at end of file diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 03e74e3..e251aba 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -164,6 +164,13 @@ public function testBetweenValidatorWithValidValues() $parser = new FloatParser(new Config(), 'precipitation', 101.12); $parser->between(60.99, 101.12); $this->assertEquals(101.12, $parser->required()); + + $parser = new StringParser(new Config(), 'name', ''); + $parser->between(0, 1); + $this->assertEquals('', $parser->required()); + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->between(0, 1); + $this->assertEquals('A', $parser->required()); } public function testLargerThanValidatorWithValidValues() @@ -183,6 +190,14 @@ public function testLargerThanValidatorWithValidValues() $parser = new FloatParser(new Config(), 'precipitation', 1.01); $parser->largerThanOrEqualTo(1.01); $this->assertEquals(1.01, $parser->required()); + + $parser = new StringParser(new Config(), 'name', 'A'); + $parser->largerThan(0); + $this->assertEquals('A', $parser->required()); + + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->largerThanOrEqualTo(1); + $this->assertEquals('A', $parser->required()); } public function testSmallerThanValidatorWithValidValues() @@ -202,6 +217,14 @@ public function testSmallerThanValidatorWithValidValues() $parser = new FloatParser(new Config(), 'precipitation', -2.01); $parser->largerThanOrEqualTo(-2.01); $this->assertEquals(-2.01, $parser->required()); + + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->smallerThan(2); + $this->assertEquals('A', $parser->required()); + + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->largerThanOrEqualTo(1); + $this->assertEquals('A', $parser->required()); } public function testIntBetweenValidatorWithValuesOutOfRange() @@ -218,6 +241,13 @@ public function testFloatBetweenValidatorWithValuesOutOfRange() $precipitation = $parser->between(60.99, 101.12)->required(); } + public function testStringBetweenValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "name". Expected a string with character length between 0 and 1, but got "AB"'); + $parser = new StringParser(new Config(), 'name', 'AB'); + $parser->between(0, 1)->required(); + } + public function testIntLargerThanValidatorWithValuesOutOfRange() { $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer larger than 1, but got "0"'); @@ -246,6 +276,20 @@ public function testFloatLargerThanOrEqualToValidatorWithValuesOutOfRange() $precipitation = $parser->largerThanOrEqualTo(1.01)->required(); } + public function testStringLargerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "name". Expected a string longer than 0 characters, but got ""'); + $parser = new StringParser(new Config(), 'name', ''); + $parser->largerThan(0)->required(); + } + + public function testStringLargerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "name". Expected a string longer than or equal to 2 characters, but got "A"'); + $parser = new StringParser(new Config(), 'name', 'A'); + $parser->largerThanOrEqualTo(2)->required(); + } + public function testIntSmallerThanValidatorWithValuesOutOfRange() { $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "groupId". Expected an integer smaller than 0, but got "1"'); @@ -273,4 +317,18 @@ public function testFloatSmallerThanOrEqualToValidatorWithValuesOutOfRange() $parser = new FloatParser(new Config(), 'precipitation', 1.01); $precipitation = $parser->smallerThanOrEqualTo(0.01)->required(); } + + public function testStringSmallerThanValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "name". Expected a string shorter than 1 characters, but got "A"'); + $parser = new StringParser(new Config(), 'name', 'A'); + $parser->smallerThan(1)->required(); + } + + public function testStringSmallerThanOrEqualToValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, 'Invalid value for parameter "name". Expected a string shorter than or equal to 1 characters, but got "AB"'); + $parser = new StringParser(new Config(), 'name', 'AB'); + $parser->smallerThanOrEqualTo(1)->required(); + } } diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index ae62bce..a942e58 100644 --- a/tests/TypeSpecTest.php +++ b/tests/TypeSpecTest.php @@ -17,6 +17,12 @@ use MPScholten\RequestParser\IntParser; use MPScholten\RequestParser\FloatParser; use MPScholten\RequestParser\TrimParser; +use MPScholten\RequestParser\Validator\IntLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringBetweenParser; +use MPScholten\RequestParser\Validator\StringLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLargerThanParser; +use MPScholten\RequestParser\Validator\StringSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringSmallerThanParser; use MPScholten\RequestParser\Validator\UrlParser; use MPScholten\RequestParser\Validator\FloatLargerThanOrEqualToParser; use MPScholten\RequestParser\Validator\FloatLargerThanParser; @@ -79,7 +85,7 @@ public function testIntLargerThan() public function testIntLargerThanOrEqualTo() { $spec = new TypeParser(new Config(), 'groupId', '1'); - $this->assertInstanceOf(IntLargerThanParser::class, $spec->int()->largerThan(1)); + $this->assertInstanceOf(IntLargerThanOrEqualToParser::class, $spec->int()->largerThanOrEqualTo(1)); } public function testIntSmallerThan() @@ -214,4 +220,34 @@ public function testCommaSeparatedBoolean() $spec = new TypeParser(new Config(), 'answers', 'true,false,true'); $this->assertInstanceOf(CommaSeparatedBooleanParser::class, $spec->commaSeparated()->boolean()); } + + public function testStringBetween() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringBetweenParser::class, $spec->string()->between(0, 1)); + } + + public function testStringLargerThan() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLargerThanParser::class, $spec->string()->largerThan(0)); + } + + public function testStringLargerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLargerThanOrEqualToParser::class, $spec->string()->largerThanOrEqualTo(1)); + } + + public function testStringSmallerThan() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringSmallerThanParser::class, $spec->string()->smallerThan(2)); + } + + public function testStringSmallerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringSmallerThanOrEqualToParser::class, $spec->string()->smallerThanOrEqualTo(1)); + } }