Skip to content

Commit

Permalink
Added two more rules: Between and BetweenInclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
franzose committed Apr 6, 2018
1 parent ce65b62 commit 5c48679
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
'alphadash' => \Kontrolio\Rules\Core\Alphadash::class,
'alphanum' => \Kontrolio\Rules\Core\Alphanum::class,
'blank' => \Kontrolio\Rules\Core\Blank::class,
'between' => \Kontrolio\Rules\Core\Between::class,
'between_inclusive' => \Kontrolio\Rules\Core\BetweenInclusive::class,
'card_scheme' => \Kontrolio\Rules\Core\CardScheme::class,
'date' => \Kontrolio\Rules\Core\Date::class,
'date_time' => \Kontrolio\Rules\Core\DateTime::class,
Expand Down
24 changes: 24 additions & 0 deletions src/Rules/Core/Between.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Rules/Core/BetweenInclusive.php
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);
}
}
23 changes: 23 additions & 0 deletions tests/Rules/BetweenInclusiveTest.php
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'));
}
}
23 changes: 23 additions & 0 deletions tests/Rules/BetweenTest.php
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'));
}
}

0 comments on commit 5c48679

Please sign in to comment.