Allow to use when() rule without else

This commit is contained in:
Henrique Moody 2015-01-16 21:42:57 -02:00
parent 0b2e042c63
commit 35588c5340
5 changed files with 48 additions and 4 deletions

View file

@ -1932,11 +1932,12 @@ See also:
* [v::consonant()](#vconsonant)
#### v::when(v $if, v $then, v $else)
#### v::when(v $if, v $then)
A ternary validator that accepts three parameters.
When the $if validates, returns validation for $then.
When the $if doesn't validate, returns validation for $else.
When the `$if` validates, returns validation for `$then`.
When the `$if` doesn't validate, returns validation for `$else`, if defined.
```php
v::when(v::int(), v::positive(), v::notEmpty())->validate($input);
@ -1944,6 +1945,7 @@ v::when(v::int(), v::positive(), v::notEmpty())->validate($input);
In the sample above, if `$input` is an integer, then it must be positive.
If `$input` is not an integer, then it must not me empty.
When `$else` is not defined use [v::alwaysInvalid()](#valwaysinvalid) as default.
See also:

View file

@ -3,12 +3,16 @@ namespace Respect\Validation\Exceptions;
class AlwaysInvalidException extends ValidationException
{
const SIMPLE = 0;
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} is always invalid',
self::SIMPLE => '{{name}} is not valid',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} is always valid',
self::SIMPLE => '{{name}} is valid',
),
);
}

View file

@ -2,6 +2,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\AlwaysInvalidException;
class When extends AbstractRule
{
@ -9,10 +10,15 @@ class When extends AbstractRule
public $then;
public $else;
public function __construct(Validatable $when, Validatable $then, Validatable $else)
public function __construct(Validatable $when, Validatable $then, Validatable $else = null)
{
$this->when = $when;
$this->then = $then;
if (null === $else) {
$else = new AlwaysInvalid();
$else->setTemplate(AlwaysInvalidException::SIMPLE);
}
$this->else = $else;
}

View file

@ -93,7 +93,7 @@ use Respect\Validation\Rules\AllOf;
* @method static Validator uppercase()
* @method static Validator version()
* @method static Validator vowel()
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when)
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
* @method static Validator writable()
* @method static Validator xdigit(string $additionalChars = null)
* @method static Validator yes($useLocale = false)

View file

@ -1,6 +1,9 @@
<?php
namespace Respect\Validation\Rules;
/**
* @covers Respect\Validation\Rules\When
*/
class WhenTest extends \PHPUnit_Framework_TestCase
{
public function testWhenHappypath()
@ -9,11 +12,37 @@ class WhenTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($v->validate(3));
$this->assertTrue($v->validate('aaa'));
}
public function testWhenError()
{
$v = new When(new Int(), new Between(1,5), new NotEmpty());
$this->assertFalse($v->validate(15));
}
public function testWhenWithoutElseHappypath()
{
$v = new When(new Int(), new Between(1,5));
$this->assertTrue($v->validate(3));
}
public function testWhenWithoutElseError()
{
$v = new When(new String(), new Between(1,5));
$this->assertFalse($v->validate(15));
}
/**
* @expectedException Respect\Validation\Exceptions\AlwaysInvalidException
* @expectedExceptionMessage "15" is not valid
*/
public function testWhenWithoutElseAssert()
{
$v = new When(new String(), new Between(1,5));
$v->assert(15);
}
/**
* @expectedException Respect\Validation\Exceptions\BetweenException
*/
@ -22,6 +51,7 @@ class WhenTest extends \PHPUnit_Framework_TestCase
$v = new When(new Int(), new Between(1,5), new NotEmpty());
$this->assertFalse($v->assert(15));
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/
@ -30,6 +60,7 @@ class WhenTest extends \PHPUnit_Framework_TestCase
$v = new When(new Int(), new Between(1,5), new NotEmpty());
$this->assertFalse($v->assert(''));
}
/**
* @expectedException Respect\Validation\Exceptions\MaxException
*/
@ -38,6 +69,7 @@ class WhenTest extends \PHPUnit_Framework_TestCase
$v = new When(new Int(), new Between(1,5), new NotEmpty());
$this->assertFalse($v->check(15));
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/