Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create "True" and "False" rules #263

Merged
merged 2 commits into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -213,6 +214,7 @@ Reference
* [v::numeric()](#vnumeric)
* [v::object()](#vobject)
* [v::string()](#vstring)
* [v::true()](#vtrue)
* [v::xdigit()](#vxdigit)

### Generics
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions library/Exceptions/FalseException.php
Original file line number Diff line number Diff line change
@@ -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"',
),
);
}
14 changes: 14 additions & 0 deletions library/Exceptions/TrueException.php
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions library/Rules/False.php
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions library/Rules/True.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @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)
Expand Down Expand Up @@ -89,6 +90,7 @@
* @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()
Expand Down
49 changes: 49 additions & 0 deletions tests/Rules/FalseTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions tests/Rules/TrueTest.php
Original file line number Diff line number Diff line change
@@ -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'),
);
}
}