-
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 uppercase characters rule #23
base: dev
Are you sure you want to change the base?
Changes from 3 commits
24571c6
dd7a143
83be6ae
2feef81
941df1a
d30fb52
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,84 @@ | ||
<?php | ||
|
||
namespace Morebec\Validator\Rule; | ||
|
||
|
||
use Morebec\Validator\ValidationRuleInterface; | ||
|
||
class ContainsUppercaseCharacters implements ValidationRuleInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $numberCharacters; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $strict; | ||
/** | ||
* @var string|null | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* ContainsUppercaseCharacters constructor. | ||
* @param int|null $numberCharacters | ||
* @param bool|null $strict | ||
* @param string|null $message | ||
*/ | ||
public function __construct( | ||
?int $numberCharacters = 1, | ||
?bool $strict = false, | ||
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. Why did you make it accept null? |
||
?string $message = null | ||
) | ||
{ | ||
$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->countUpperCase($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 an error here. Can you find it? |
||
} | ||
else{ | ||
return $this->countUpperCase($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){ | ||
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 conditional workflows rely on return statements. |
||
return "Number of uppercase 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} upper cased characters" |
||
} | ||
else{ | ||
return "Number of uppercase characters should exceed ".${$this->numberCharacters}; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @return int | ||
*/ | ||
private function countUpperCase(string $message): int | ||
{ | ||
$lowerCase = strtolower($message); | ||
$similar = similar_text($message, $lowerCase); | ||
return strlen($message)-$similar; | ||
Comment on lines
+79
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! :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Tests\Morebec\Validator\Rule; | ||
|
||
use Morebec\Validator\Rule\ContainsUppercaseCharacters; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ContainsUppercaseCharactersTest extends TestCase | ||
{ | ||
public function testValidate(){ | ||
$ruleFirst = new ContainsUppercaseCharacters(1,true); | ||
$ruleSecond = new ContainsUppercaseCharacters(1,false); | ||
$ruleThird= new ContainsUppercaseCharacters(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 use different values for number of characters in the tests. |
||
$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")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Morebec\Validator\Rule; | ||
|
||
|
||
use Morebec\Validator\Rule\MaxLength; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MaxLengthTest extends TestCase | ||
{ | ||
public function testValidate() | ||
{ | ||
$firstRule = new MaxLength(5); | ||
$secondRule = new MaxLength(5,"Custom message"); | ||
$this->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") | ||
); | ||
} | ||
} |
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.
Can you validate that
numberCharacters
is a positive integer or zero?Also, why did you make it accept null?