Skip to content

Contains numeric characters rule #22

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

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

namespace Morebec\Validator\Rule;


use InvalidArgumentException;
use Morebec\Validator\ValidationRuleInterface;

class ContainsNumericCharacters implements ValidationRuleInterface
{
/**
* @var int
*/
private $numberCharacters;
/**
* @var bool
*/
private $strict;
/**
* @var string|null
*/
private $message;

/**
* ContainsNumericCharacters constructor.
* @param int $numberCharacters
* @param bool $strict
* @param string|null $message
*/
public function __construct(
int $numberCharacters,
bool $strict,
?string $message = null
Comment on lines +31 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also set the default parameter values I had provided in the issue?

)
{
if($numberCharacters<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->numberCharacters = $numberCharacters;
$this->strict = $strict;
$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
{
if($this->strict){
return $this->countDigits($v)<=$this->numberCharacters;
Copy link
Contributor

Choose a reason for hiding this comment

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

There is an error here. Can you find it?

}
return $this->countDigits($v)>=$this->numberCharacters;

}

/**
* 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
{
if($this->message){
return $this->message;
}
if($this->strict){
return "Number of numeric characters exceeds ".${$this->numberCharacters};
Copy link
Contributor

Choose a reason for hiding this comment

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

The message here should be:

"The value '{$v}' should have exactly {$this->numberCharacters} numeric characters"

}
return "Number of numeric characters should exceed ".${$this->numberCharacters};
Copy link
Contributor

Choose a reason for hiding this comment

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

The message here should be:

"The value '{$v}' should have at least {$this->numberCharacters} numeric characters"

}

/**
* @param string $str
* @return int
*/
private function countDigits(string $str)
{
return preg_match_all( "/[0-9]/", $str )?:0;
}
}
22 changes: 22 additions & 0 deletions tests/Rule/ContainsNumericCharactersTest.php
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\ContainsNumericCharacters;
use PHPUnit\Framework\TestCase;

class ContainsNumericCharactersTest extends TestCase
{
public function testValidate(){
$ruleFirst = new ContainsNumericCharacters(1,true);
$ruleSecond = new ContainsNumericCharacters(1,false);
$ruleThird= new ContainsNumericCharacters(1,false,"Custom Message");
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

Testing with various values instead of only '1' would be better for test coverage.

$this->assertTrue($ruleFirst->validate("test string 1"));
$this->assertFalse($ruleFirst->validate("test string 12"));
$this->assertFalse($ruleSecond->validate('test string'));
$this->assertTrue($ruleSecond->validate('test string 12'));
$this->assertEquals( "Custom Message", $ruleThird->getMessage("test string"));
$this->expectException(InvalidArgumentException::class);
$ruleForth = new ContainsNumericCharacters(-1,true);
}
}