Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 Morebec\Validator\ValidationRuleInterface;

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

/**
* ContainsNumericCharacters constructor.
* @param int|null $numberCharacters
* @param bool|null $strict
* @param string|null $message
*/
public function __construct(
?int $numberCharacters = 1,
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 ensure that the value of the parameter is always a positive integer or zero?

?bool $strict = false,
?string $message = null
Comment on lines +31 to +33
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.

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

)
{
$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
Copy Markdown
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?

}
else{
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.

Since the control flows rely on return, the else is not necessary.

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;
}
else if($this->strict){
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.

Since the control flow (if, elseif, else) relies on return statements, all the else workflows are unecessary, you can replace with if, if and a direct return.

return "Number of numeric characters exceeds ".${$this->numberCharacters};
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.

The message here should be:

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

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

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