Merge pull request #263 from henriquemoody/true_and_false

Create "True" and "False" rules
This commit is contained in:
Henrique Moody 2015-01-23 02:50:39 -02:00
commit 8310c50527
8 changed files with 206 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)
@ -213,6 +214,7 @@ Reference
* [v::numeric()](#vnumeric)
* [v::object()](#vobject)
* [v::string()](#vstring)
* [v::true()](#vtrue)
* [v::xdigit()](#vxdigit)
### Generics
@ -1008,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)
@ -1878,6 +1897,23 @@ See also
* [v::domain()](#vdomain) - Validates domain names
* [v::countryCode()](#vcountrycode) - Validates ISO country codes
#### v::true()
Validates if a value is considered as `true`.
```php
v::true()->validate(true); //true
v::true()->validate(1); //true
v::true()->validate('1'); //true
v::true()->validate('true'); //true
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"',
),
);
}

View file

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

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);
}
}

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

@ -0,0 +1,21 @@
<?php
namespace Respect\Validation\Rules;
class True extends AbstractRule
{
public function validate($input)
{
if (! is_string($input) || is_numeric($input)) {
return ($input == true);
}
$validValues = array(
'on',
'true',
'yes',
);
$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)
@ -89,6 +90,7 @@ use Respect\Validation\Rules\AllOf;
* @method static Validator string()
* @method static Validator symbolicLink()
* @method static Validator tld()
* @method static Validator true()
* @method static Validator uploaded()
* @method static Validator uppercase()
* @method static Validator version()

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'),
);
}
}

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

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