-
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
Min length rule #25
base: dev
Are you sure you want to change the base?
Min length rule #25
Changes from all commits
24571c6
8c3b79e
9867e5e
5959b86
bddc786
d66c8f1
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,56 @@ | ||
<?php | ||
|
||
namespace Morebec\Validator\Rule; | ||
|
||
|
||
use InvalidArgumentException; | ||
use Morebec\Validator\ValidationRuleInterface; | ||
|
||
class MinLength implements ValidationRuleInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $minLength; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* MinLength constructor. | ||
* @param int $minLength | ||
* @param string|null $message | ||
*/ | ||
public function __construct( | ||
int $minLength, | ||
?string $message = null | ||
) | ||
{ | ||
if($minLength<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->minLength = $minLength; | ||
jwillp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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 strlen($v)>=$this->minLength; | ||
} | ||
|
||
/** | ||
* 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}' is supposed to be at least ".$this->minLength." characters long."; | ||
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. Can you change the message to: "'The value '{$v}' was expected to be at least {$this->minLength} characters long." |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,26 @@ | |
namespace Tests\Morebec\Validator\Rule; | ||
|
||
use Morebec\Validator\Rule\MaxLength; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MaxLengthTest extends TestCase | ||
{ | ||
public function testValidate() | ||
{ | ||
$firstRule = new MaxLength(5); | ||
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. According to my other comment with a negative value, it would require a test that makes sure the exception is thrown. |
||
$secondRule = new MaxLength(5, 'Custom message'); | ||
$this->assertTrue($firstRule->validate('test')); | ||
$this->assertFalse($firstRule->validate('long test')); | ||
$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') | ||
$firstRule->getMessage("arr") | ||
); | ||
$this->assertEquals( | ||
'Custom message', | ||
$secondRule->getMessage('arr') | ||
"Custom message", | ||
$secondRule->getMessage("arr") | ||
); | ||
$this->expectException(InvalidArgumentException::class); | ||
$thirdRule = new MaxLength(-1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Tests\Morebec\Validator\Rule; | ||
|
||
|
||
use InvalidArgumentException; | ||
use Morebec\Validator\Rule\MinLength; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MinLengthTest extends TestCase | ||
{ | ||
public function testValidate(){ | ||
$ruleFirst = new MinLength(4); | ||
jwillp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$ruleSecond = new MinLength(4,"Custom message"); | ||
|
||
$this->assertTrue($ruleFirst->validate("test string")); | ||
$this->assertFalse($ruleFirst->validate("tes")); | ||
$this->assertEquals("Custom message",$ruleSecond->getMessage("test")); | ||
$this->expectException(InvalidArgumentException::class); | ||
$ruleThird = new MinLength(-1); | ||
} | ||
} |
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.
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.