From 24571c6c544bac687cb7a2939b0b1cf58c68e1d7 Mon Sep 17 00:00:00 2001 From: M1QN Date: Tue, 3 Mar 2020 20:33:40 +0100 Subject: [PATCH 1/4] 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/4] 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/4] 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 7995eee80532b5b2dd2586faf14a3150fe1c6483 Mon Sep 17 00:00:00 2001 From: M1QN Date: Fri, 6 Mar 2020 18:54:23 +0100 Subject: [PATCH 4/4] Added MatchesRegex rule --- src/Rule/MatchesRegex.php | 52 +++++++++++++++++++++++++++++++++ tests/Rule/MatchesRegexTest.php | 18 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/Rule/MatchesRegex.php create mode 100644 tests/Rule/MatchesRegexTest.php diff --git a/src/Rule/MatchesRegex.php b/src/Rule/MatchesRegex.php new file mode 100644 index 0000000..cdcd285 --- /dev/null +++ b/src/Rule/MatchesRegex.php @@ -0,0 +1,52 @@ +regex = $regex; + $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 preg_match($this->regex, $v) != 0?true:false; + } + + /** + * 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}' does not match provided regex"; + } +} \ No newline at end of file diff --git a/tests/Rule/MatchesRegexTest.php b/tests/Rule/MatchesRegexTest.php new file mode 100644 index 0000000..2341932 --- /dev/null +++ b/tests/Rule/MatchesRegexTest.php @@ -0,0 +1,18 @@ +assertTrue($ruleFirst->validate("hello1")); + $this->assertFalse($ruleFirst->validate("hello")); + $this->assertEquals("Custom message",$ruleSecond->getMessage("hello")); + } +} \ No newline at end of file