-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added two more rules: Between and BetweenInclusive
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kontrolio\Rules\Core; | ||
|
||
use Kontrolio\Rules\AbstractRule; | ||
|
||
final class Between extends AbstractRule | ||
{ | ||
private $min; | ||
private $max; | ||
|
||
public function __construct($min, $max) | ||
{ | ||
$this->min = $min; | ||
$this->max = $max; | ||
} | ||
|
||
public function isValid($input = null) | ||
{ | ||
return (new GreaterThan($this->min))->isValid($input) && | ||
(new LessThan($this->max))->isValid($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kontrolio\Rules\Core; | ||
|
||
use Kontrolio\Rules\AbstractRule; | ||
|
||
final class BetweenInclusive extends AbstractRule | ||
{ | ||
private $min; | ||
private $max; | ||
|
||
public function __construct($min, $max) | ||
{ | ||
$this->min = $min; | ||
$this->max = $max; | ||
} | ||
|
||
public function isValid($input = null) | ||
{ | ||
return (new GreaterThanOrEqual($this->min))->isValid($input) && | ||
(new LessThanOrEqual($this->max))->isValid($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kontrolio\Tests\Rules; | ||
|
||
use Kontrolio\Rules\Core\BetweenInclusive; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class BetweenInclusiveTest extends TestCase | ||
{ | ||
public function testValidation() | ||
{ | ||
static::assertTrue((new BetweenInclusive(5, 10))->isValid(6)); | ||
static::assertTrue((new BetweenInclusive(5, 10))->isValid(5)); | ||
static::assertTrue((new BetweenInclusive(5, 10))->isValid(10)); | ||
static::assertFalse((new BetweenInclusive(5, 10))->isValid(4)); | ||
static::assertFalse((new BetweenInclusive(5, 10))->isValid(11)); | ||
static::assertTrue((new BetweenInclusive('a', 'm'))->isValid('c')); | ||
static::assertTrue((new BetweenInclusive('a', 'm'))->isValid('a')); | ||
static::assertTrue((new BetweenInclusive('a', 'm'))->isValid('m')); | ||
static::assertFalse((new BetweenInclusive('a', 'm'))->isValid('p')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kontrolio\Tests\Rules; | ||
|
||
use Kontrolio\Rules\Core\Between; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class BetweenTest extends TestCase | ||
{ | ||
public function testValidation() | ||
{ | ||
static::assertTrue((new Between(5, 10))->isValid(6)); | ||
static::assertFalse((new Between(5, 10))->isValid(5)); | ||
static::assertFalse((new Between(5, 10))->isValid(4)); | ||
static::assertFalse((new Between(5, 10))->isValid(10)); | ||
static::assertFalse((new Between(5, 10))->isValid(11)); | ||
static::assertTrue((new Between('a', 'm'))->isValid('c')); | ||
static::assertFalse((new Between('a', 'm'))->isValid('a')); | ||
static::assertFalse((new Between('a', 'm'))->isValid('m')); | ||
static::assertFalse((new Between('a', 'm'))->isValid('p')); | ||
} | ||
} |