Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Rule/MaxLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ class MaxLength implements ValidationRuleInterface
*/
private $length;

public function __construct(int $length)
/**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not be part of the PR.
Although seeing, this it makes me think that a negative integer should not be passed to the function, but it is currently possible.

Could you make it throw an InvalidArgumentException in that case?

* @var int
*/
private $message;

public function __construct(
int $length,
?string $message = null
)
{
$this->length = $length;
$this->message = $message;
}

/**
Expand All @@ -31,6 +40,6 @@ public function validate($v): bool
*/
public function getMessage($v): string
{
return "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";
}
}
53 changes: 53 additions & 0 deletions src/Rule/MinLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Morebec\Validator\Rule;


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
)
{
$this->minLength = $minLength;
Comment thread
jwillp marked this conversation as 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.";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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."

}
}
26 changes: 26 additions & 0 deletions tests/Rule/MaxLengthTest.php
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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"));
$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")
);
}
}
19 changes: 19 additions & 0 deletions tests/Rule/MinLengthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Morebec\Validator\Rule;


use Morebec\Validator\Rule\MinLength;
use PHPUnit\Framework\TestCase;

class MinLengthTest extends TestCase
{
public function testValidate(){
$ruleFirst = new MinLength(4);
Comment thread
jwillp marked this conversation as 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"));
}
}