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

Contains lowercase characters rule #21

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

namespace Morebec\Validator\Rule;


use Morebec\Validator\ValidationRuleInterface;

class ContainsLowercaseCharacters implements ValidationRuleInterface
{
/**
* @var int
*/
private $numberCharacters;

/**
* @var bool
*/
private $strict;

/**
* @var string|null
*/
private $message;

/**
* ContainsLowercaseCharacters constructor.
* @param int|null $numberCharacters
* @param bool|null $strict
* @param string|null $message
*/
public function __construct(
?int $numberCharacters = 1,
?bool $strict = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comments as for uppercase.

?string $message = null
)
{
$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->countLowerCase($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 a mistake here actually.
Can you find it?

}
else{
return $this->countLowerCase($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){
return "Number of lowercase 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} lower cased characters"

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

/**
* @param string $message
* @return int
*/
private function countLowerCase(string $message) :int {
$upperCase = strtoupper($message);
$similar = similar_text($message,$upperCase);
return strlen($message)-$similar;
Comment on lines +78 to +81
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice solution again :)

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

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


class ContainsLowercaseCharactersTest extends TestCase
{
public function testValidate(){
$ruleFirst = new ContainsLowercaseCharacters(1,true);
$ruleSecond = new ContainsLowercaseCharacters(1,false);
$ruleThird= new ContainsLowercaseCharacters(1,false,"Custom Message");
Comment on lines +12 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to test using different number of character values.
The value 1 here causes some false positives.

$this->assertTrue($ruleFirst->validate("tEST STRING"));
$this->assertFalse($ruleFirst->validate("tEST sTRING"));
$this->assertFalse($ruleSecond->validate('TEST STRING'));
$this->assertTrue($ruleSecond->validate('tEST sTRING'));
$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")
);
}
}