From 24571c6c544bac687cb7a2939b0b1cf58c68e1d7 Mon Sep 17 00:00:00 2001 From: M1QN Date: Tue, 3 Mar 2020 20:33:40 +0100 Subject: [PATCH 1/5] added message functionality to maxlength rule --- src/Rule/MaxLength.php | 13 +++++++++++-- tests/Rule/MaxLengthTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/Rule/MaxLengthTest.php diff --git a/src/Rule/MaxLength.php b/src/Rule/MaxLength.php index c406f42..a79e197 100644 --- a/src/Rule/MaxLength.php +++ b/src/Rule/MaxLength.php @@ -13,9 +13,18 @@ class MaxLength implements ValidationRuleInterface */ private $length; - public function __construct(int $length) + /** + * @var int + */ + private $message; + + public function __construct( + int $length, + ?string $message = null + ) { $this->length = $length; + $this->message = $message; } /** @@ -31,6 +40,6 @@ public function validate($v): bool */ public function getMessage($v): string { - return "The length of '{$v}' was expected to be at most {$this->length} characters long"; + return $this->message?:"The length of '{$v}' was expected to be at most {$this->length} characters long"; } } \ No newline at end of file diff --git a/tests/Rule/MaxLengthTest.php b/tests/Rule/MaxLengthTest.php new file mode 100644 index 0000000..3503028 --- /dev/null +++ b/tests/Rule/MaxLengthTest.php @@ -0,0 +1,26 @@ +assertTrue($firstRule->validate("test")); + $this->assertFalse($firstRule->validate("long test")); + $this->assertEquals( + "The length of 'arr' was expected to be at most 5 characters long", + $firstRule->getMessage("arr") + ); + $this->assertEquals( + "Custom message", + $secondRule->getMessage("arr") + ); + } +} \ No newline at end of file From 8c3b79ec083829e0901f2d5ec486594054ab8977 Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 18:38:44 +0100 Subject: [PATCH 2/5] Added ContainsNumericCharactersRule --- src/Rule/ContainsNumericCharacters.php | 81 ++++++++++++++++++++ tests/Rule/ContainsNumericCharactersTest.php | 19 +++++ 2 files changed, 100 insertions(+) create mode 100644 src/Rule/ContainsNumericCharacters.php create mode 100644 tests/Rule/ContainsNumericCharactersTest.php diff --git a/src/Rule/ContainsNumericCharacters.php b/src/Rule/ContainsNumericCharacters.php new file mode 100644 index 0000000..7d6d909 --- /dev/null +++ b/src/Rule/ContainsNumericCharacters.php @@ -0,0 +1,81 @@ +numberCharacters = $numberCharacters; + $this->strict = $strict; + $this->message = $message; + } + + /** + * Validates a value according to this rule and returns if it is valid or not + * @param mixed $v + * @return bool true if valid, otherwise false + */ + public function validate($v): bool + { + if($this->strict){ + return $this->countDigits($v)<=$this->numberCharacters; + } + else{ + return $this->countDigits($v)>=$this->numberCharacters; + } + } + + /** + * Returns the message to be used in case the validation did not pass + * @param mixed $v the value that did not pass the validation + * @return string + */ + public function getMessage($v): string + { + if($this->message){ + return $this->message; + } + else if($this->strict){ + return "Number of numeric characters exceeds ".${$this->numberCharacters}; + } + else{ + return "Number of numeric characters should exceed ".${$this->numberCharacters}; + } + } + + /** + * @param string $str + * @return int + */ + private function countDigits(string $str) + { + return preg_match_all( "/[0-9]/", $str )?:0; + } +} \ No newline at end of file diff --git a/tests/Rule/ContainsNumericCharactersTest.php b/tests/Rule/ContainsNumericCharactersTest.php new file mode 100644 index 0000000..7280883 --- /dev/null +++ b/tests/Rule/ContainsNumericCharactersTest.php @@ -0,0 +1,19 @@ +assertTrue($ruleFirst->validate("test string 1")); + $this->assertFalse($ruleFirst->validate("test string 12")); + $this->assertFalse($ruleSecond->validate('test string')); + $this->assertTrue($ruleSecond->validate('test string 12')); + $this->assertEquals( "Custom Message", $ruleThird->getMessage("test string")); + } +} \ No newline at end of file From 9867e5e5b7c21172a286b126f60b26171b834361 Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 18:38:44 +0100 Subject: [PATCH 3/5] Revert "Added ContainsNumericCharactersRule" This reverts commit 8c3b79ec083829e0901f2d5ec486594054ab8977. --- src/Rule/ContainsNumericCharacters.php | 81 -------------------- tests/Rule/ContainsNumericCharactersTest.php | 19 ----- 2 files changed, 100 deletions(-) delete mode 100644 src/Rule/ContainsNumericCharacters.php delete mode 100644 tests/Rule/ContainsNumericCharactersTest.php diff --git a/src/Rule/ContainsNumericCharacters.php b/src/Rule/ContainsNumericCharacters.php deleted file mode 100644 index 7d6d909..0000000 --- a/src/Rule/ContainsNumericCharacters.php +++ /dev/null @@ -1,81 +0,0 @@ -numberCharacters = $numberCharacters; - $this->strict = $strict; - $this->message = $message; - } - - /** - * Validates a value according to this rule and returns if it is valid or not - * @param mixed $v - * @return bool true if valid, otherwise false - */ - public function validate($v): bool - { - if($this->strict){ - return $this->countDigits($v)<=$this->numberCharacters; - } - else{ - return $this->countDigits($v)>=$this->numberCharacters; - } - } - - /** - * Returns the message to be used in case the validation did not pass - * @param mixed $v the value that did not pass the validation - * @return string - */ - public function getMessage($v): string - { - if($this->message){ - return $this->message; - } - else if($this->strict){ - return "Number of numeric characters exceeds ".${$this->numberCharacters}; - } - else{ - return "Number of numeric characters should exceed ".${$this->numberCharacters}; - } - } - - /** - * @param string $str - * @return int - */ - private function countDigits(string $str) - { - return preg_match_all( "/[0-9]/", $str )?:0; - } -} \ No newline at end of file diff --git a/tests/Rule/ContainsNumericCharactersTest.php b/tests/Rule/ContainsNumericCharactersTest.php deleted file mode 100644 index 7280883..0000000 --- a/tests/Rule/ContainsNumericCharactersTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertTrue($ruleFirst->validate("test string 1")); - $this->assertFalse($ruleFirst->validate("test string 12")); - $this->assertFalse($ruleSecond->validate('test string')); - $this->assertTrue($ruleSecond->validate('test string 12')); - $this->assertEquals( "Custom Message", $ruleThird->getMessage("test string")); - } -} \ No newline at end of file From 5959b86eb8e73d24563bac6da50f025ef13b07ac Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 19:05:56 +0100 Subject: [PATCH 4/5] Added minLength rule --- src/Rule/MinLength.php | 53 ++++++++++++++++++++++++++++++++++++ tests/Rule/MinLengthTest.php | 19 +++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/Rule/MinLength.php create mode 100644 tests/Rule/MinLengthTest.php diff --git a/src/Rule/MinLength.php b/src/Rule/MinLength.php new file mode 100644 index 0000000..1147923 --- /dev/null +++ b/src/Rule/MinLength.php @@ -0,0 +1,53 @@ +minLength = $minLength; + $this->message = $message; + } + + /** + * Validates a value according to this rule and returns if it is valid or not + * @param mixed $v + * @return bool true if valid, otherwise false + */ + public function validate($v): bool + { + return strlen($v)>=$this->minLength; + } + + /** + * Returns the message to be used in case the validation did not pass + * @param mixed $v the value that did not pass the validation + * @return string + */ + public function getMessage($v): string + { + return $this->message?:"'${$v}' is supposed to be at least ".$this->minLength." characters long."; + } +} \ No newline at end of file diff --git a/tests/Rule/MinLengthTest.php b/tests/Rule/MinLengthTest.php new file mode 100644 index 0000000..3dde5d5 --- /dev/null +++ b/tests/Rule/MinLengthTest.php @@ -0,0 +1,19 @@ +assertTrue($ruleFirst->validate("test string")); + $this->assertFalse($ruleFirst->validate("tes")); + $this->assertEquals("Custom message",$ruleSecond->getMessage("test")); + } +} \ No newline at end of file From bddc786b481891a802ba7a10daf109ba78938828 Mon Sep 17 00:00:00 2001 From: M1QN Date: Sat, 7 Mar 2020 10:59:55 +0100 Subject: [PATCH 5/5] Updated for negative values --- src/Rule/MaxLength.php | 5 ++++- src/Rule/MinLength.php | 3 +++ tests/Rule/MaxLengthTest.php | 4 ++++ tests/Rule/MinLengthTest.php | 3 +++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Rule/MaxLength.php b/src/Rule/MaxLength.php index a79e197..32aa744 100644 --- a/src/Rule/MaxLength.php +++ b/src/Rule/MaxLength.php @@ -4,6 +4,7 @@ namespace Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\ValidationRuleInterface; class MaxLength implements ValidationRuleInterface @@ -14,7 +15,7 @@ class MaxLength implements ValidationRuleInterface private $length; /** - * @var int + * @var string|null */ private $message; @@ -23,6 +24,8 @@ public function __construct( ?string $message = null ) { + if($length<0) + throw new InvalidArgumentException(); $this->length = $length; $this->message = $message; } diff --git a/src/Rule/MinLength.php b/src/Rule/MinLength.php index 1147923..8e951bf 100644 --- a/src/Rule/MinLength.php +++ b/src/Rule/MinLength.php @@ -3,6 +3,7 @@ namespace Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\ValidationRuleInterface; class MinLength implements ValidationRuleInterface @@ -27,6 +28,8 @@ public function __construct( ?string $message = null ) { + if($minLength<0) + throw new InvalidArgumentException(); $this->minLength = $minLength; $this->message = $message; } diff --git a/tests/Rule/MaxLengthTest.php b/tests/Rule/MaxLengthTest.php index 3503028..4a0edc4 100644 --- a/tests/Rule/MaxLengthTest.php +++ b/tests/Rule/MaxLengthTest.php @@ -4,8 +4,10 @@ use Morebec\Validator\Rule\MaxLength; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; + class MaxLengthTest extends TestCase { public function testValidate() @@ -22,5 +24,7 @@ public function testValidate() "Custom message", $secondRule->getMessage("arr") ); + $this->expectException(InvalidArgumentException::class); + $thirdRule = new MaxLength(-1); } } \ No newline at end of file diff --git a/tests/Rule/MinLengthTest.php b/tests/Rule/MinLengthTest.php index 3dde5d5..4d43982 100644 --- a/tests/Rule/MinLengthTest.php +++ b/tests/Rule/MinLengthTest.php @@ -3,6 +3,7 @@ namespace Tests\Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\Rule\MinLength; use PHPUnit\Framework\TestCase; @@ -15,5 +16,7 @@ public function testValidate(){ $this->assertTrue($ruleFirst->validate("test string")); $this->assertFalse($ruleFirst->validate("tes")); $this->assertEquals("Custom message",$ruleSecond->getMessage("test")); + $this->expectException(InvalidArgumentException::class); + $ruleThird = new MinLength(-1); } } \ No newline at end of file