diff --git a/src/Rule/ContainsLowercaseCharacters.php b/src/Rule/ContainsLowercaseCharacters.php new file mode 100644 index 0000000..b5859de --- /dev/null +++ b/src/Rule/ContainsLowercaseCharacters.php @@ -0,0 +1,83 @@ +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; + } + 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; + } + if($this->strict){ + return "Number of lowercase characters exceeds ".${$this->numberCharacters}; + } + 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..0728469 --- /dev/null +++ b/tests/Rule/ContainsLowercaseCharactersTest.php @@ -0,0 +1,23 @@ +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")); + $this->expectException(InvalidArgumentException::class); + $ruleFourth = new ContainsLowercaseCharacters(-1,false); + } +} \ No newline at end of file