Create "False" rule

This commit is contained in:
Henrique Moody 2015-01-22 14:47:55 -02:00
parent dfcd4b2a20
commit 8b369fae97
5 changed files with 107 additions and 0 deletions

View file

@ -205,6 +205,7 @@ Reference
* [v::arr()](#varr)
* [v::bool()](#vbool)
* [v::date()](#vdate)
* [v::false()](#vfalse)
* [v::float()](#vfloat)
* [v::hexa()](#vhexa-deprecated) *(deprecated)*
* [v::instance()](#vinstanceinstancename)
@ -1009,6 +1010,23 @@ See also
* [v::directory()](#vdirectory)
* [v::file()](#vfile)
#### v::false()
Validates if a value is considered as `false`.
```php
v::false()->validate(false); //true
v::false()->validate(0); //true
v::false()->validate('0'); //true
v::false()->validate('false'); //true
v::false()->validate('off'); //true
v::false()->validate('no'); //true
```
See also
* [v::true()](#vtrue)
#### v::endsWith($value)
#### v::endsWith($value, boolean $identical=false)
@ -1892,6 +1910,10 @@ v::true()->validate('on'); //true
v::true()->validate('yes'); //true
```
See also
* [v::false()](#vfalse)
#### v::uploaded()
Validates if the given data is a file that was uploaded via HTTP POST.

View file

@ -0,0 +1,14 @@
<?php
namespace Respect\Validation\Exceptions;
class FalseException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} is not considered as "False"',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} is considered as "False"',
),
);
}

21
library/Rules/False.php Normal file
View file

@ -0,0 +1,21 @@
<?php
namespace Respect\Validation\Rules;
class False extends AbstractRule
{
public function validate($input)
{
if (! is_string($input) || is_numeric($input)) {
return ($input == false);
}
$validValues = array(
'false',
'no',
'off',
);
$filteredInput = strtolower($input);
return in_array($filteredInput, $validValues);
}
}

View file

@ -42,6 +42,7 @@ use Respect\Validation\Rules\AllOf;
* @method static Validator even()
* @method static Validator executable()
* @method static Validator exists()
* @method static Validator false()
* @method static Validator file()
* @method static Validator float()
* @method static Validator graph(string $additionalChars = null)

49
tests/Rules/FalseTest.php Normal file
View file

@ -0,0 +1,49 @@
<?php
namespace Respect\Validation\Rules;
class FalseTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider validFalseProvider
*/
public function testShouldValidatePatternAccordingToTheDefinedLocale($input)
{
$rule = new False();
$this->assertTrue($rule->validate($input));
}
public function validFalseProvider()
{
return array(
array(false),
array(0),
array('0'),
array('false'),
array('off'),
array('no'),
);
}
/**
* @dataProvider invalidFalseProvider
*/
public function testShouldNotValidatePatternAccordingToTheDefinedLocale($input)
{
$rule = new False();
$this->assertFalse($rule->validate($input));
}
public function invalidFalseProvider()
{
return array(
array(true),
array(1),
array('1'),
array('true'),
array('on'),
array('yes'),
);
}
}