diff --git a/src/Rule/MaxLength.php b/src/Rule/MaxLength.php index 7f6f7c1..ad57558 100644 --- a/src/Rule/MaxLength.php +++ b/src/Rule/MaxLength.php @@ -2,6 +2,7 @@ namespace Morebec\Validator\Rule; +use InvalidArgumentException; use Morebec\Validator\ValidationRuleInterface; class MaxLength implements ValidationRuleInterface @@ -16,8 +17,13 @@ class MaxLength implements ValidationRuleInterface */ private $message; - public function __construct(int $length, ?string $message = null) + public function __construct( + int $length, + ?string $message = null + ) { + if($length<0) + throw new InvalidArgumentException(); $this->length = $length; $this->message = $message; } @@ -35,6 +41,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 $this->message?:"The length of '{$v}' was expected to be at most {$this->length} characters long"; } } diff --git a/src/Rule/MinLength.php b/src/Rule/MinLength.php new file mode 100644 index 0000000..8e951bf --- /dev/null +++ b/src/Rule/MinLength.php @@ -0,0 +1,56 @@ +minLength = $minLength; + $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."; + } +} \ No newline at end of file diff --git a/tests/Rule/MaxLengthTest.php b/tests/Rule/MaxLengthTest.php index 1125fc1..b6c687c 100644 --- a/tests/Rule/MaxLengthTest.php +++ b/tests/Rule/MaxLengthTest.php @@ -3,6 +3,7 @@ namespace Tests\Morebec\Validator\Rule; use Morebec\Validator\Rule\MaxLength; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; class MaxLengthTest extends TestCase @@ -10,16 +11,18 @@ 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')); + $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); } } diff --git a/tests/Rule/MinLengthTest.php b/tests/Rule/MinLengthTest.php new file mode 100644 index 0000000..4d43982 --- /dev/null +++ b/tests/Rule/MinLengthTest.php @@ -0,0 +1,22 @@ +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); + } +} \ No newline at end of file