-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contains lowercase characters rule #21
base: dev
Are you sure you want to change the base?
Changes from all commits
24571c6
447ec81
4de2553
b38b986
b48dc82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Morebec\Validator\Rule; | ||
|
||
|
||
use InvalidArgumentException; | ||
use Morebec\Validator\ValidationRuleInterface; | ||
|
||
class ContainsLowercaseCharacters implements ValidationRuleInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $numberCharacters; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $strict; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* ContainsLowercaseCharacters constructor. | ||
* @param int $numberCharacters | ||
* @param bool $strict | ||
* @param string|null $message | ||
*/ | ||
public function __construct( | ||
int $numberCharacters, | ||
bool $strict, | ||
?string $message = null | ||
) | ||
{ | ||
if($numberCharacters<0) | ||
throw new InvalidArgumentException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When throwing an exception, a message should always be provided in order to inform the user why the exception was thrown without needing to look at the source code |
||
$this->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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a mistake here actually. |
||
} | ||
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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message here should be: "The value '{$v}' should have exactly {$this->numberCharacters} lower cased characters" |
||
} | ||
return "Number of lowercase characters should exceed ".${$this->numberCharacters}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message here should be: "The value '{$v}' should have at least {$this->numberCharacters} lower cased characters" |
||
} | ||
|
||
/** | ||
* @param string $message | ||
* @return int | ||
*/ | ||
private function countLowerCase(string $message) :int { | ||
$upperCase = strtoupper($message); | ||
$similar = similar_text($message,$upperCase); | ||
return strlen($message)-$similar; | ||
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice solution again :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Tests\Morebec\Validator\Rule; | ||
use InvalidArgumentException; | ||
use Morebec\Validator\Rule\ContainsLowercaseCharacters; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
|
||
class ContainsLowercaseCharactersTest extends TestCase | ||
{ | ||
public function testValidate(){ | ||
$ruleFirst = new ContainsLowercaseCharacters(1,true); | ||
$ruleSecond = new ContainsLowercaseCharacters(1,false); | ||
$ruleThird= new ContainsLowercaseCharacters(1,false,"Custom Message"); | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to test using different number of character values. |
||
$this->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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also set the default parameter values I had provided in the issue?