diff --git a/src/StringParser.php b/src/StringParser.php index b5aaab2..0972cc5 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\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 @@ -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,55 @@ public function rightTrim() { return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM); } + + /** + * @param int $minValue + * @param int $maxValue + * + * @return StringLengthBetween + */ + public function lengthBetween($minValue, $maxValue) + { + return new StringLengthBetween($this->config, $this->name, $this->value, $minValue, $maxValue); + } + + /** + * @param int $minValue + * + * @return StringLengthLargerThanParser + */ + public function lengthLargerThan($minValue) + { + return new StringLengthLargerThanParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $minValue + * + * @return StringLengthLargerThanOrEqualToParser + */ + public function lengthLargerThanOrEqualTo($minValue) + { + return new StringLengthLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue); + } + + /** + * @param int $maxValue + * + * @return StringLengthSmallerThanParser + */ + public function lengthSmallerThan($maxValue) + { + return new StringLengthSmallerThanParser($this->config, $this->name, $this->value, $maxValue); + } + + /** + * @param int $maxValue + * + * @return StringLengthSmallerThanOrEqualToParser + */ + public function lengthSmallerThanOrEqualTo($maxValue) + { + return new StringLengthSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue); + } } diff --git a/src/Validator/StringLengthBetween.php b/src/Validator/StringLengthBetween.php new file mode 100644 index 0000000..3b89b0d --- /dev/null +++ b/src/Validator/StringLengthBetween.php @@ -0,0 +1,43 @@ +minValue and $this->maxValue"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (strlen($value) >= $this->minValue && strlen($value) <= $this->maxValue) { + return $value; + } + return null; + } + + /** + * @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/StringLengthLargerThanOrEqualToParser.php b/src/Validator/StringLengthLargerThanOrEqualToParser.php new file mode 100644 index 0000000..e170eab --- /dev/null +++ b/src/Validator/StringLengthLargerThanOrEqualToParser.php @@ -0,0 +1,43 @@ +minValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (strlen($value) >= $this->minValue) { + return $value; + } + return null; + } + + /** + * @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/StringLengthLargerThanParser.php b/src/Validator/StringLengthLargerThanParser.php new file mode 100644 index 0000000..04c46ef --- /dev/null +++ b/src/Validator/StringLengthLargerThanParser.php @@ -0,0 +1,43 @@ +minValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (strlen($value) > $this->minValue) { + return $value; + } + return null; + } + + /** + * @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/StringLengthSmallerThanOrEqualToParser.php b/src/Validator/StringLengthSmallerThanOrEqualToParser.php new file mode 100644 index 0000000..f187878 --- /dev/null +++ b/src/Validator/StringLengthSmallerThanOrEqualToParser.php @@ -0,0 +1,43 @@ +maxValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (strlen($value) <= $this->maxValue) { + return $value; + } + return null; + } + + /** + * @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/StringLengthSmallerThanParser.php b/src/Validator/StringLengthSmallerThanParser.php new file mode 100644 index 0000000..3f948ff --- /dev/null +++ b/src/Validator/StringLengthSmallerThanParser.php @@ -0,0 +1,43 @@ +maxValue characters"; + } + + /** + * @param $value + * @return int + */ + protected function parse($value) + { + if (strlen($value) < $this->maxValue) { + return $value; + } + return null; + } + + /** + * @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..10707db 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->lengthBetween(0, 1); + $this->assertEquals('', $parser->required()); + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->lengthBetween(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->lengthLargerThan(0); + $this->assertEquals('A', $parser->required()); + + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->lengthLargerThanOrEqualTo(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->lengthSmallerThan(2); + $this->assertEquals('A', $parser->required()); + + $parser = new StringParser(new Config(), 'groupId', 'A'); + $parser->lengthLargerThanOrEqualTo(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->lengthBetween(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->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->lengthLargerThanOrEqualTo(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->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->lengthSmallerThanOrEqualTo(1)->required(); + } } diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index ae62bce..7c694bc 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\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; @@ -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(StringLengthBetween::class, $spec->string()->lengthBetween(0, 1)); + } + + public function testStringLargerThan() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLengthLargerThanParser::class, $spec->string()->lengthLargerThan(0)); + } + + public function testStringLargerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLengthLargerThanOrEqualToParser::class, $spec->string()->lengthLargerThanOrEqualTo(1)); + } + + public function testStringSmallerThan() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLengthSmallerThanParser::class, $spec->string()->lengthSmallerThan(2)); + } + + public function testStringSmallerThanOrEqualTo() + { + $spec = new TypeParser(new Config(), 'groupId', 'A'); + $this->assertInstanceOf(StringLengthSmallerThanOrEqualToParser::class, $spec->string()->lengthSmallerThanOrEqualTo(1)); + } }