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

Matches regex rule #26

Open
wants to merge 5 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
52 changes: 52 additions & 0 deletions src/Rule/MatchesRegex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Morebec\Validator\Rule;


use Morebec\Validator\ValidationRuleInterface;

class MatchesRegex implements ValidationRuleInterface
{
/**
* @var string
*/
private $regex;
/**
* @var string|null
*/
private $message;

/**
* MatchesRegex constructor.
* @param string $regex
* @param string|null $message
*/
public function __construct(
string $regex,
?string $message = null
)
{
$this->regex = $regex;
$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 preg_match($this->regex, $v) != 0?true:false;
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to return true or false, here.
The following line:

preg_match($this->regex, $v) != 0  

Will already return a boolean.

}

/**
* 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}' does not match provided regex";
}
}
2 changes: 1 addition & 1 deletion src/Rule/MaxLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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";
}
}
18 changes: 18 additions & 0 deletions tests/Rule/MatchesRegexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Morebec\Validator\Rule;
use Morebec\Validator\Rule\MatchesRegex;
use PHPUnit\Framework\TestCase;


class MatchesRegexTest extends TestCase
{
public function testValidate(){
$ruleFirst = new MatchesRegex("/[0-9]/");
$ruleSecond = new MatchesRegex("/[0-9]/","Custom message");

$this->assertTrue($ruleFirst->validate("hello1"));
$this->assertFalse($ruleFirst->validate("hello"));
$this->assertEquals("Custom message",$ruleSecond->getMessage("hello"));
}
}
14 changes: 7 additions & 7 deletions tests/Rule/MaxLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ 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")
);
}
}
}