From e5440d809c22a8c1bb2579d1ba6992f04028f641 Mon Sep 17 00:00:00 2001 From: Karsten Lettow Date: Wed, 1 Mar 2017 17:42:36 +0100 Subject: [PATCH 1/3] 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)); + } } From 6c75f160925de3247113658a8a26c8a46ca4b1ce Mon Sep 17 00:00:00 2001 From: Karsten Lettow Date: Thu, 2 Mar 2017 12:05:58 +0100 Subject: [PATCH 2/3] refactored naming for string length validation --- src/StringParser.php | 45 ++++++++++--------- ...weenParser.php => StringLengthBetween.php} | 2 +- ...StringLengthLargerThanOrEqualToParser.php} | 2 +- ...r.php => StringLengthLargerThanParser.php} | 2 +- ...tringLengthSmallerThanOrEqualToParser.php} | 2 +- ....php => StringLengthSmallerThanParser.php} | 2 +- tests/ParserSpecTest.php | 22 ++++----- tests/TypeSpecTest.php | 20 ++++----- 8 files changed, 51 insertions(+), 46 deletions(-) rename src/Validator/{StringBetweenParser.php => StringLengthBetween.php} (94%) rename src/Validator/{StringLargerThanOrEqualToParser.php => StringLengthLargerThanOrEqualToParser.php} (91%) rename src/Validator/{StringLargerThanParser.php => StringLengthLargerThanParser.php} (93%) rename src/Validator/{StringSmallerThanOrEqualToParser.php => StringLengthSmallerThanOrEqualToParser.php} (91%) rename src/Validator/{StringSmallerThanParser.php => StringLengthSmallerThanParser.php} (92%) diff --git a/src/StringParser.php b/src/StringParser.php index 731f635..0972cc5 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -3,11 +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\StringLengthBetween; +use MPScholten\RequestParser\Validator\StringLengthLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLengthLargerThanParser; +use MPScholten\RequestParser\Validator\StringLengthSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLengthSmallerThanParser; use MPScholten\RequestParser\Validator\UrlParser; class StringParser extends AbstractValueParser @@ -83,46 +83,51 @@ public function rightTrim() /** * @param int $minValue * @param int $maxValue - * @return StringBetweenParser + * + * @return StringLengthBetween */ - public function between($minValue, $maxValue) + public function lengthBetween($minValue, $maxValue) { - return new StringBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue); + return new StringLengthBetween($this->config, $this->name, $this->value, $minValue, $maxValue); } /** * @param int $minValue - * @return StringLargerThanParser + * + * @return StringLengthLargerThanParser */ - public function largerThan($minValue) + public function lengthLargerThan($minValue) { - return new StringLargerThanParser($this->config, $this->name, $this->value, $minValue); + return new StringLengthLargerThanParser($this->config, $this->name, $this->value, $minValue); } /** * @param int $minValue - * @return StringLargerThanOrEqualToParser + * + * @return StringLengthLargerThanOrEqualToParser */ - public function largerThanOrEqualTo($minValue) + public function lengthLargerThanOrEqualTo($minValue) { - return new StringLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); + return new StringLengthLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); } /** * @param int $maxValue - * @return StringSmallerThanParser + * + * @return StringLengthSmallerThanParser */ - public function smallerThan($maxValue) + public function lengthSmallerThan($maxValue) { - return new StringSmallerThanParser($this->config, $this->name, $this->value, $maxValue); + return new StringLengthSmallerThanParser($this->config, $this->name, $this->value, $maxValue); } /** * @param int $maxValue - * @return StringSmallerThanOrEqualToParser + * + * @return StringLengthSmallerThanOrEqualToParser */ - public function smallerThanOrEqualTo($maxValue) + public function lengthSmallerThanOrEqualTo($maxValue) { - return new StringSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); + return new StringLengthSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); } } diff --git a/src/Validator/StringBetweenParser.php b/src/Validator/StringLengthBetween.php similarity index 94% rename from src/Validator/StringBetweenParser.php rename to src/Validator/StringLengthBetween.php index 6d50a4b..d0df507 100644 --- a/src/Validator/StringBetweenParser.php +++ b/src/Validator/StringLengthBetween.php @@ -2,7 +2,7 @@ namespace MPScholten\RequestParser\Validator; -class StringBetweenParser extends AbstractBetweenParser +class StringLengthBetween extends AbstractBetweenParser { protected function describe() { diff --git a/src/Validator/StringLargerThanOrEqualToParser.php b/src/Validator/StringLengthLargerThanOrEqualToParser.php similarity index 91% rename from src/Validator/StringLargerThanOrEqualToParser.php rename to src/Validator/StringLengthLargerThanOrEqualToParser.php index 2b28660..112e5ec 100644 --- a/src/Validator/StringLargerThanOrEqualToParser.php +++ b/src/Validator/StringLengthLargerThanOrEqualToParser.php @@ -2,7 +2,7 @@ namespace MPScholten\RequestParser\Validator; -class StringLargerThanOrEqualToParser extends AbstractLargerThanOrEqualToParser +class StringLengthLargerThanOrEqualToParser extends AbstractLargerThanOrEqualToParser { protected function describe() { diff --git a/src/Validator/StringLargerThanParser.php b/src/Validator/StringLengthLargerThanParser.php similarity index 93% rename from src/Validator/StringLargerThanParser.php rename to src/Validator/StringLengthLargerThanParser.php index c1696da..eb4b1a7 100644 --- a/src/Validator/StringLargerThanParser.php +++ b/src/Validator/StringLengthLargerThanParser.php @@ -2,7 +2,7 @@ namespace MPScholten\RequestParser\Validator; -class StringLargerThanParser extends AbstractLargerThanParser +class StringLengthLargerThanParser extends AbstractLargerThanParser { protected function describe() { diff --git a/src/Validator/StringSmallerThanOrEqualToParser.php b/src/Validator/StringLengthSmallerThanOrEqualToParser.php similarity index 91% rename from src/Validator/StringSmallerThanOrEqualToParser.php rename to src/Validator/StringLengthSmallerThanOrEqualToParser.php index 66095d7..32143cb 100644 --- a/src/Validator/StringSmallerThanOrEqualToParser.php +++ b/src/Validator/StringLengthSmallerThanOrEqualToParser.php @@ -2,7 +2,7 @@ namespace MPScholten\RequestParser\Validator; -class StringSmallerThanOrEqualToParser extends AbstractSmallerThanOrEqualToParser +class StringLengthSmallerThanOrEqualToParser extends AbstractSmallerThanOrEqualToParser { protected function describe() { diff --git a/src/Validator/StringSmallerThanParser.php b/src/Validator/StringLengthSmallerThanParser.php similarity index 92% rename from src/Validator/StringSmallerThanParser.php rename to src/Validator/StringLengthSmallerThanParser.php index a1f778b..d2d0691 100644 --- a/src/Validator/StringSmallerThanParser.php +++ b/src/Validator/StringLengthSmallerThanParser.php @@ -2,7 +2,7 @@ namespace MPScholten\RequestParser\Validator; -class StringSmallerThanParser extends AbstractSmallerThanParser +class StringLengthSmallerThanParser extends AbstractSmallerThanParser { protected function describe() { diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index e251aba..10707db 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -166,10 +166,10 @@ public function testBetweenValidatorWithValidValues() $this->assertEquals(101.12, $parser->required()); $parser = new StringParser(new Config(), 'name', ''); - $parser->between(0, 1); + $parser->lengthBetween(0, 1); $this->assertEquals('', $parser->required()); $parser = new StringParser(new Config(), 'groupId', 'A'); - $parser->between(0, 1); + $parser->lengthBetween(0, 1); $this->assertEquals('A', $parser->required()); } @@ -192,11 +192,11 @@ public function testLargerThanValidatorWithValidValues() $this->assertEquals(1.01, $parser->required()); $parser = new StringParser(new Config(), 'name', 'A'); - $parser->largerThan(0); + $parser->lengthLargerThan(0); $this->assertEquals('A', $parser->required()); $parser = new StringParser(new Config(), 'groupId', 'A'); - $parser->largerThanOrEqualTo(1); + $parser->lengthLargerThanOrEqualTo(1); $this->assertEquals('A', $parser->required()); } @@ -219,11 +219,11 @@ public function testSmallerThanValidatorWithValidValues() $this->assertEquals(-2.01, $parser->required()); $parser = new StringParser(new Config(), 'groupId', 'A'); - $parser->smallerThan(2); + $parser->lengthSmallerThan(2); $this->assertEquals('A', $parser->required()); $parser = new StringParser(new Config(), 'groupId', 'A'); - $parser->largerThanOrEqualTo(1); + $parser->lengthLargerThanOrEqualTo(1); $this->assertEquals('A', $parser->required()); } @@ -245,7 +245,7 @@ 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(); + $parser->lengthBetween(0, 1)->required(); } public function testIntLargerThanValidatorWithValuesOutOfRange() @@ -280,14 +280,14 @@ 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(); + $parser->lengthLargerThan(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(); + $parser->lengthLargerThanOrEqualTo(2)->required(); } public function testIntSmallerThanValidatorWithValuesOutOfRange() @@ -322,13 +322,13 @@ 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(); + $parser->lengthSmallerThan(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(); + $parser->lengthSmallerThanOrEqualTo(1)->required(); } } diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index a942e58..7c694bc 100644 --- a/tests/TypeSpecTest.php +++ b/tests/TypeSpecTest.php @@ -18,11 +18,11 @@ 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\StringLengthBetween; +use MPScholten\RequestParser\Validator\StringLengthLargerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLengthLargerThanParser; +use MPScholten\RequestParser\Validator\StringLengthSmallerThanOrEqualToParser; +use MPScholten\RequestParser\Validator\StringLengthSmallerThanParser; use MPScholten\RequestParser\Validator\UrlParser; use MPScholten\RequestParser\Validator\FloatLargerThanOrEqualToParser; use MPScholten\RequestParser\Validator\FloatLargerThanParser; @@ -224,30 +224,30 @@ public function testCommaSeparatedBoolean() public function testStringBetween() { $spec = new TypeParser(new Config(), 'groupId', 'A'); - $this->assertInstanceOf(StringBetweenParser::class, $spec->string()->between(0, 1)); + $this->assertInstanceOf(StringLengthBetween::class, $spec->string()->lengthBetween(0, 1)); } public function testStringLargerThan() { $spec = new TypeParser(new Config(), 'groupId', 'A'); - $this->assertInstanceOf(StringLargerThanParser::class, $spec->string()->largerThan(0)); + $this->assertInstanceOf(StringLengthLargerThanParser::class, $spec->string()->lengthLargerThan(0)); } public function testStringLargerThanOrEqualTo() { $spec = new TypeParser(new Config(), 'groupId', 'A'); - $this->assertInstanceOf(StringLargerThanOrEqualToParser::class, $spec->string()->largerThanOrEqualTo(1)); + $this->assertInstanceOf(StringLengthLargerThanOrEqualToParser::class, $spec->string()->lengthLargerThanOrEqualTo(1)); } public function testStringSmallerThan() { $spec = new TypeParser(new Config(), 'groupId', 'A'); - $this->assertInstanceOf(StringSmallerThanParser::class, $spec->string()->smallerThan(2)); + $this->assertInstanceOf(StringLengthSmallerThanParser::class, $spec->string()->lengthSmallerThan(2)); } public function testStringSmallerThanOrEqualTo() { $spec = new TypeParser(new Config(), 'groupId', 'A'); - $this->assertInstanceOf(StringSmallerThanOrEqualToParser::class, $spec->string()->smallerThanOrEqualTo(1)); + $this->assertInstanceOf(StringLengthSmallerThanOrEqualToParser::class, $spec->string()->lengthSmallerThanOrEqualTo(1)); } } From 15142fb0f4e5018f35fe76f8975191bb79b17178 Mon Sep 17 00:00:00 2001 From: Karsten Lettow Date: Tue, 7 Mar 2017 15:21:55 +0100 Subject: [PATCH 3/3] added workaround --- src/Validator/StringLengthBetween.php | 7 +++---- src/Validator/StringLengthLargerThanOrEqualToParser.php | 7 +++---- src/Validator/StringLengthLargerThanParser.php | 7 +++---- src/Validator/StringLengthSmallerThanOrEqualToParser.php | 7 +++---- src/Validator/StringLengthSmallerThanParser.php | 7 +++---- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Validator/StringLengthBetween.php b/src/Validator/StringLengthBetween.php index d0df507..3b89b0d 100644 --- a/src/Validator/StringLengthBetween.php +++ b/src/Validator/StringLengthBetween.php @@ -15,11 +15,10 @@ protected function describe() */ protected function parse($value) { - if (!is_string($value)) { - return null; + if (strlen($value) >= $this->minValue && strlen($value) <= $this->maxValue) { + return $value; } - $value = strlen($value); - return parent::parse($value); + return null; } /** diff --git a/src/Validator/StringLengthLargerThanOrEqualToParser.php b/src/Validator/StringLengthLargerThanOrEqualToParser.php index 112e5ec..e170eab 100644 --- a/src/Validator/StringLengthLargerThanOrEqualToParser.php +++ b/src/Validator/StringLengthLargerThanOrEqualToParser.php @@ -15,11 +15,10 @@ protected function describe() */ protected function parse($value) { - if (!is_string($value)) { - return null; + if (strlen($value) >= $this->minValue) { + return $value; } - $value = strlen($value); - return parent::parse($value); + return null; } /** diff --git a/src/Validator/StringLengthLargerThanParser.php b/src/Validator/StringLengthLargerThanParser.php index eb4b1a7..04c46ef 100644 --- a/src/Validator/StringLengthLargerThanParser.php +++ b/src/Validator/StringLengthLargerThanParser.php @@ -15,11 +15,10 @@ protected function describe() */ protected function parse($value) { - if (!is_string($value)) { - return null; + if (strlen($value) > $this->minValue) { + return $value; } - $value = strlen($value); - return parent::parse($value); + return null; } /** diff --git a/src/Validator/StringLengthSmallerThanOrEqualToParser.php b/src/Validator/StringLengthSmallerThanOrEqualToParser.php index 32143cb..f187878 100644 --- a/src/Validator/StringLengthSmallerThanOrEqualToParser.php +++ b/src/Validator/StringLengthSmallerThanOrEqualToParser.php @@ -15,11 +15,10 @@ protected function describe() */ protected function parse($value) { - if (!is_string($value)) { - return null; + if (strlen($value) <= $this->maxValue) { + return $value; } - $value = strlen($value); - return parent::parse($value); + return null; } /** diff --git a/src/Validator/StringLengthSmallerThanParser.php b/src/Validator/StringLengthSmallerThanParser.php index d2d0691..3f948ff 100644 --- a/src/Validator/StringLengthSmallerThanParser.php +++ b/src/Validator/StringLengthSmallerThanParser.php @@ -15,11 +15,10 @@ protected function describe() */ protected function parse($value) { - if (!is_string($value)) { - return null; + if (strlen($value) < $this->maxValue) { + return $value; } - $value = strlen($value); - return parent::parse($value); + return null; } /**