mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
Merge pull request #247 from henriquemoody/when
Allow to use `when()` rule without else
This commit is contained in:
commit
81cfba9a6c
|
@ -1968,11 +1968,12 @@ See also:
|
||||||
* [v::consonant()](#vconsonant)
|
* [v::consonant()](#vconsonant)
|
||||||
|
|
||||||
#### v::when(v $if, v $then, v $else)
|
#### v::when(v $if, v $then, v $else)
|
||||||
|
#### v::when(v $if, v $then)
|
||||||
|
|
||||||
A ternary validator that accepts three parameters.
|
A ternary validator that accepts three parameters.
|
||||||
|
|
||||||
When the $if validates, returns validation for $then.
|
When the `$if` validates, returns validation for `$then`.
|
||||||
When the $if doesn't validate, returns validation for $else.
|
When the `$if` doesn't validate, returns validation for `$else`, if defined.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
v::when(v::int(), v::positive(), v::notEmpty())->validate($input);
|
v::when(v::int(), v::positive(), v::notEmpty())->validate($input);
|
||||||
|
@ -1980,6 +1981,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.
|
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.
|
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:
|
See also:
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,16 @@ namespace Respect\Validation\Exceptions;
|
||||||
|
|
||||||
class AlwaysInvalidException extends ValidationException
|
class AlwaysInvalidException extends ValidationException
|
||||||
{
|
{
|
||||||
|
const SIMPLE = 0;
|
||||||
|
|
||||||
public static $defaultTemplates = array(
|
public static $defaultTemplates = array(
|
||||||
self::MODE_DEFAULT => array(
|
self::MODE_DEFAULT => array(
|
||||||
self::STANDARD => '{{name}} is always invalid',
|
self::STANDARD => '{{name}} is always invalid',
|
||||||
|
self::SIMPLE => '{{name}} is not valid',
|
||||||
),
|
),
|
||||||
self::MODE_NEGATIVE => array(
|
self::MODE_NEGATIVE => array(
|
||||||
self::STANDARD => '{{name}} is always valid',
|
self::STANDARD => '{{name}} is always valid',
|
||||||
|
self::SIMPLE => '{{name}} is valid',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
use Respect\Validation\Validatable;
|
use Respect\Validation\Validatable;
|
||||||
|
use Respect\Validation\Exceptions\AlwaysInvalidException;
|
||||||
|
|
||||||
class When extends AbstractRule
|
class When extends AbstractRule
|
||||||
{
|
{
|
||||||
|
@ -9,10 +10,15 @@ class When extends AbstractRule
|
||||||
public $then;
|
public $then;
|
||||||
public $else;
|
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->when = $when;
|
||||||
$this->then = $then;
|
$this->then = $then;
|
||||||
|
if (null === $else) {
|
||||||
|
$else = new AlwaysInvalid();
|
||||||
|
$else->setTemplate(AlwaysInvalidException::SIMPLE);
|
||||||
|
}
|
||||||
|
|
||||||
$this->else = $else;
|
$this->else = $else;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ use Respect\Validation\Rules\AllOf;
|
||||||
* @method static Validator uppercase()
|
* @method static Validator uppercase()
|
||||||
* @method static Validator version()
|
* @method static Validator version()
|
||||||
* @method static Validator vowel()
|
* @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 writable()
|
||||||
* @method static Validator xdigit(string $additionalChars = null)
|
* @method static Validator xdigit(string $additionalChars = null)
|
||||||
* @method static Validator yes($useLocale = false)
|
* @method static Validator yes($useLocale = false)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Respect\Validation\Rules\When
|
||||||
|
*/
|
||||||
class WhenTest extends \PHPUnit_Framework_TestCase
|
class WhenTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testWhenHappypath()
|
public function testWhenHappypath()
|
||||||
|
@ -9,11 +12,37 @@ class WhenTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertTrue($v->validate(3));
|
$this->assertTrue($v->validate(3));
|
||||||
$this->assertTrue($v->validate('aaa'));
|
$this->assertTrue($v->validate('aaa'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWhenError()
|
public function testWhenError()
|
||||||
{
|
{
|
||||||
$v = new When(new Int(), new Between(1,5), new NotEmpty());
|
$v = new When(new Int(), new Between(1,5), new NotEmpty());
|
||||||
$this->assertFalse($v->validate(15));
|
$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
|
* @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());
|
$v = new When(new Int(), new Between(1,5), new NotEmpty());
|
||||||
$this->assertFalse($v->assert(15));
|
$this->assertFalse($v->assert(15));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Respect\Validation\Exceptions\NotEmptyException
|
* @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());
|
$v = new When(new Int(), new Between(1,5), new NotEmpty());
|
||||||
$this->assertFalse($v->assert(''));
|
$this->assertFalse($v->assert(''));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Respect\Validation\Exceptions\MaxException
|
* @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());
|
$v = new When(new Int(), new Between(1,5), new NotEmpty());
|
||||||
$this->assertFalse($v->check(15));
|
$this->assertFalse($v->check(15));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Respect\Validation\Exceptions\NotEmptyException
|
* @expectedException Respect\Validation\Exceptions\NotEmptyException
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue