Skip to content
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

Length rule #24

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions src/Rule/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Morebec\Validator\Rule;


use InvalidArgumentException;
use Morebec\Validator\ValidationRuleInterface;

class Length implements ValidationRuleInterface
{
/**
* @var int
*/
private $length;
/**
* @var string|null
*/
private $message;

/**
* Length constructor.
* @param int $length
* @param string|null $message
*/
public function __construct(
int $length,
?string $message = null
)
{
if($length <0)
throw new InvalidArgumentException();
Copy link
Contributor

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.

$this->length = $length;
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->length;
Copy link
Contributor

Choose a reason for hiding this comment

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

In PHP, it is a best practice to test with a strict comparison: !== and ===

}

/**
* 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 exactly ".$this->length." characters long";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change supposed to 'expected'?

}
}
23 changes: 23 additions & 0 deletions tests/Rule/LengthTest.php
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\Length;
use PHPUnit\Framework\TestCase;

class LengthTest extends TestCase
{
public function testValidate(){
$ruleFirst = new Length(4);
$ruleSecond = new Length(4,"Custom message");

$this->assertTrue($ruleFirst->validate("test"));
$this->assertFalse($ruleFirst->validate("test message"));
$this->assertEquals("Custom message",$ruleSecond->getMessage("test"));
$this->expectException(InvalidArgumentException::class);

$ruleThird = new Length(-1);
}
}