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 447ec81d3290c439d5dd85790e4eed4c6dd0dad3 Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 18:29:30 +0100 Subject: [PATCH 2/5] Added ContainsLowercaseCharacters rule --- src/Rule/ContainsLowercaseCharacters.php | 90 +++++++++++++++++++ .../Rule/ContainsLowercaseCharactersTest.php | 26 ++++++ 2 files changed, 116 insertions(+) create mode 100644 src/Rule/ContainsLowercaseCharacters.php create mode 100644 tests/Rule/ContainsLowercaseCharactersTest.php diff --git a/src/Rule/ContainsLowercaseCharacters.php b/src/Rule/ContainsLowercaseCharacters.php new file mode 100644 index 0000000..88257c4 --- /dev/null +++ b/src/Rule/ContainsLowercaseCharacters.php @@ -0,0 +1,90 @@ +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->countLowerCase($v)<=$this->numberCharacters; + } + else{ + return $this->countLowerCase($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 lowercase characters exceeds ".${$this->numberCharacters}; + } + else{ + return "Number of lowercase characters should exceed ".${$this->numberCharacters}; + } + } + + /** + * @param string $message + * @return int + */ + private function countLowerCase(string $message) :int { + $upperCase = strtoupper($message); + $similar = similar_text($message,$upperCase); + return strlen($message)-$similar; + } +} \ No newline at end of file diff --git a/tests/Rule/ContainsLowercaseCharactersTest.php b/tests/Rule/ContainsLowercaseCharactersTest.php new file mode 100644 index 0000000..64eccd2 --- /dev/null +++ b/tests/Rule/ContainsLowercaseCharactersTest.php @@ -0,0 +1,26 @@ +assertTrue($ruleFirst->validate("tEST STRING")); + $this->assertFalse($ruleFirst->validate("tEST sTRING")); + $this->assertFalse($ruleSecond->validate('TEST STRING')); + $this->assertTrue($ruleSecond->validate('tEST sTRING')); + $this->assertEquals( "Custom Message", $ruleThird->getMessage("test string")); + } +} \ No newline at end of file From 4de25530049b93bf14fe49c5ceef3bc249ec1f41 Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 18:42:03 +0100 Subject: [PATCH 3/5] Fixed headers --- src/Rule/ContainsLowercaseCharacters.php | 10 ++-------- tests/Rule/ContainsLowercaseCharactersTest.php | 6 ------ 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Rule/ContainsLowercaseCharacters.php b/src/Rule/ContainsLowercaseCharacters.php index 88257c4..afca5b7 100644 --- a/src/Rule/ContainsLowercaseCharacters.php +++ b/src/Rule/ContainsLowercaseCharacters.php @@ -1,10 +1,4 @@ Date: Tue, 3 Mar 2020 20:33:40 +0100 Subject: [PATCH 4/5] Revert "added message functionality to maxlength rule" This reverts commit 24571c6c544bac687cb7a2939b0b1cf58c68e1d7. --- src/Rule/MaxLength.php | 13 ++----------- tests/Rule/MaxLengthTest.php | 26 -------------------------- 2 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 tests/Rule/MaxLengthTest.php diff --git a/src/Rule/MaxLength.php b/src/Rule/MaxLength.php index a79e197..c406f42 100644 --- a/src/Rule/MaxLength.php +++ b/src/Rule/MaxLength.php @@ -13,18 +13,9 @@ class MaxLength implements ValidationRuleInterface */ private $length; - /** - * @var int - */ - private $message; - - public function __construct( - int $length, - ?string $message = null - ) + public function __construct(int $length) { $this->length = $length; - $this->message = $message; } /** @@ -40,6 +31,6 @@ public function validate($v): bool */ public function getMessage($v): string { - return $this->message?:"The length of '{$v}' was expected to be at most {$this->length} characters long"; + return "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 deleted file mode 100644 index 3503028..0000000 --- a/tests/Rule/MaxLengthTest.php +++ /dev/null @@ -1,26 +0,0 @@ -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 b48dc8238959efcb1061fe7c52ba5f3df6727328 Mon Sep 17 00:00:00 2001 From: M1QN Date: Sat, 7 Mar 2020 11:34:09 +0100 Subject: [PATCH 5/5] ConatinsLowercaseCharacters pull reques fix --- src/Rule/ContainsLowercaseCharacters.php | 21 +++++++++---------- .../Rule/ContainsLowercaseCharactersTest.php | 3 +++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Rule/ContainsLowercaseCharacters.php b/src/Rule/ContainsLowercaseCharacters.php index afca5b7..b5859de 100644 --- a/src/Rule/ContainsLowercaseCharacters.php +++ b/src/Rule/ContainsLowercaseCharacters.php @@ -3,6 +3,7 @@ namespace Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\ValidationRuleInterface; class ContainsLowercaseCharacters implements ValidationRuleInterface @@ -24,16 +25,18 @@ class ContainsLowercaseCharacters implements ValidationRuleInterface /** * ContainsLowercaseCharacters constructor. - * @param int|null $numberCharacters - * @param bool|null $strict + * @param int $numberCharacters + * @param bool $strict * @param string|null $message */ public function __construct( - ?int $numberCharacters = 1, - ?bool $strict = false, + int $numberCharacters, + bool $strict, ?string $message = null ) { + if($numberCharacters<0) + throw new InvalidArgumentException(); $this->numberCharacters = $numberCharacters; $this->strict = $strict; $this->message = $message; @@ -49,9 +52,7 @@ public function validate($v): bool if($this->strict){ return $this->countLowerCase($v)<=$this->numberCharacters; } - else{ - return $this->countLowerCase($v)>=$this->numberCharacters; - } + return $this->countLowerCase($v)>=$this->numberCharacters; } /** @@ -64,12 +65,10 @@ public function getMessage($v): string if($this->message){ return $this->message; } - else if($this->strict){ + if($this->strict){ return "Number of lowercase characters exceeds ".${$this->numberCharacters}; } - else{ - return "Number of lowercase characters should exceed ".${$this->numberCharacters}; - } + return "Number of lowercase characters should exceed ".${$this->numberCharacters}; } /** diff --git a/tests/Rule/ContainsLowercaseCharactersTest.php b/tests/Rule/ContainsLowercaseCharactersTest.php index 69e4fbb..0728469 100644 --- a/tests/Rule/ContainsLowercaseCharactersTest.php +++ b/tests/Rule/ContainsLowercaseCharactersTest.php @@ -1,6 +1,7 @@ assertFalse($ruleSecond->validate('TEST STRING')); $this->assertTrue($ruleSecond->validate('tEST sTRING')); $this->assertEquals( "Custom Message", $ruleThird->getMessage("test string")); + $this->expectException(InvalidArgumentException::class); + $ruleFourth = new ContainsLowercaseCharacters(-1,false); } } \ No newline at end of file