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 fe669b6afdb45b421c9502979b4c8e0184e41eee Mon Sep 17 00:00:00 2001 From: M1QN Date: Tue, 3 Mar 2020 20:33:40 +0100 Subject: [PATCH 3/4] 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 b82314915eb562837aa1f1879a006a763566a48e Mon Sep 17 00:00:00 2001 From: M1QN Date: Sat, 7 Mar 2020 11:30:10 +0100 Subject: [PATCH 4/4] ContainsNumericCharactersRule pull request fix --- src/Rule/ContainsNumericCharacters.php | 22 ++++++++++---------- tests/Rule/ContainsNumericCharactersTest.php | 3 +++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Rule/ContainsNumericCharacters.php b/src/Rule/ContainsNumericCharacters.php index 7d6d909..eb5cd32 100644 --- a/src/Rule/ContainsNumericCharacters.php +++ b/src/Rule/ContainsNumericCharacters.php @@ -3,6 +3,7 @@ namespace Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\ValidationRuleInterface; class ContainsNumericCharacters implements ValidationRuleInterface @@ -22,16 +23,18 @@ class ContainsNumericCharacters implements ValidationRuleInterface /** * ContainsNumericCharacters 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; @@ -47,9 +50,8 @@ public function validate($v): bool if($this->strict){ return $this->countDigits($v)<=$this->numberCharacters; } - else{ - return $this->countDigits($v)>=$this->numberCharacters; - } + return $this->countDigits($v)>=$this->numberCharacters; + } /** @@ -62,12 +64,10 @@ public function getMessage($v): string if($this->message){ return $this->message; } - else if($this->strict){ + if($this->strict){ return "Number of numeric characters exceeds ".${$this->numberCharacters}; } - else{ - return "Number of numeric characters should exceed ".${$this->numberCharacters}; - } + return "Number of numeric characters should exceed ".${$this->numberCharacters}; } /** diff --git a/tests/Rule/ContainsNumericCharactersTest.php b/tests/Rule/ContainsNumericCharactersTest.php index 7280883..d77f30d 100644 --- a/tests/Rule/ContainsNumericCharactersTest.php +++ b/tests/Rule/ContainsNumericCharactersTest.php @@ -1,6 +1,7 @@ assertFalse($ruleSecond->validate('test string')); $this->assertTrue($ruleSecond->validate('test string 12')); $this->assertEquals( "Custom Message", $ruleThird->getMessage("test string")); + $this->expectException(InvalidArgumentException::class); + $ruleForth = new ContainsNumericCharacters(-1,true); } } \ No newline at end of file