mirror of
https://github.com/Respect/Validation.git
synced 2024-06-11 10:12:16 +02:00
Create "FilterVar" rule
This commit is contained in:
parent
735411e886
commit
a0e7e834fb
16
README.md
16
README.md
|
@ -221,6 +221,7 @@ Reference
|
|||
|
||||
* [v::call()](#vcallcallable-callback)
|
||||
* [v::callback()](#vcallbackcallable-callback)
|
||||
* [v::filterVar()](#vfiltervarint-filter)
|
||||
* [v::not()](#vnotv-negatedvalidator)
|
||||
* [v::when()](#vwhenv-if-v-then-v-else)
|
||||
* [v::alwaysValid()](#valwaysvalid)
|
||||
|
@ -671,6 +672,7 @@ As in `v::call()`, you can pass a method or closure to it.
|
|||
See also:
|
||||
|
||||
* [v::call()](#vcallcallable-callback) - A more elaborated building block validator
|
||||
* [v::filterVar()](#vfiltervarint-filter)
|
||||
|
||||
#### v::charset()
|
||||
|
||||
|
@ -1113,6 +1115,20 @@ See also
|
|||
* [v::directory()](#vdirectory)
|
||||
* [v::exists()](#vexists)
|
||||
|
||||
#### v::filterVar(int $filter)
|
||||
#### v::filterVar(int $filter, mixed $options)
|
||||
|
||||
A wrapper for PHP's [filter_var()](http://php.net/filter_var) function.
|
||||
|
||||
```php
|
||||
v::filterVar(FILTER_VALIDATE_EMAIL)->validate('bob@example.com'); //true
|
||||
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->validate('http://example.com'); //true
|
||||
```
|
||||
|
||||
See also
|
||||
|
||||
* [v::callback()](#vcallbackcallable-callback)
|
||||
|
||||
#### v::float()
|
||||
|
||||
Validates a floating point number.
|
||||
|
|
14
library/Exceptions/FilterVarException.php
Normal file
14
library/Exceptions/FilterVarException.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FilterVarException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be valid',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be valid',
|
||||
),
|
||||
);
|
||||
}
|
38
library/Rules/FilterVar.php
Normal file
38
library/Rules/FilterVar.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class FilterVar extends Callback
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$arguments = func_get_args();
|
||||
if (! isset($arguments[0])) {
|
||||
throw new ComponentException('Cannot validate without filter flag');
|
||||
}
|
||||
|
||||
if (! $this->isValidFilter($arguments[0])) {
|
||||
throw new ComponentException('Cannot accept the given filter');
|
||||
}
|
||||
|
||||
$this->callback = 'filter_var';
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
private function isValidFilter($filter)
|
||||
{
|
||||
return in_array(
|
||||
$filter,
|
||||
array(
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_VALIDATE_EMAIL,
|
||||
FILTER_VALIDATE_FLOAT,
|
||||
FILTER_VALIDATE_INT,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_VALIDATE_REGEXP,
|
||||
FILTER_VALIDATE_URL
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -44,6 +44,7 @@ use Respect\Validation\Rules\AllOf;
|
|||
* @method static Validator exists()
|
||||
* @method static Validator false()
|
||||
* @method static Validator file()
|
||||
* @method static Validator filterVar(int $filter, mixed $options = null)
|
||||
* @method static Validator float()
|
||||
* @method static Validator graph(string $additionalChars = null)
|
||||
* @method static Validator hexRgbColor()
|
||||
|
|
60
tests/Rules/FilterVarTest.php
Normal file
60
tests/Rules/FilterVarTest.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\FilterVar
|
||||
*/
|
||||
class FilterVarTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
* @expectedExceptionMessage Cannot validate without filter flag
|
||||
*/
|
||||
public function testShouldThrowsExceptionWhenFilterIsNotDefined()
|
||||
{
|
||||
new FilterVar();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
* @expectedExceptionMessage Cannot accept the given filter
|
||||
*/
|
||||
public function testShouldThrowsExceptionWhenFilterIsNotValid()
|
||||
{
|
||||
new FilterVar(FILTER_SANITIZE_EMAIL);
|
||||
}
|
||||
|
||||
public function testShouldDefineFilterOnConstructor()
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_REGEXP);
|
||||
|
||||
$actualArguments = $rule->arguments;
|
||||
$expectedArguments = array(FILTER_VALIDATE_REGEXP);
|
||||
|
||||
$this->assertEquals($expectedArguments, $actualArguments);
|
||||
}
|
||||
|
||||
public function testShouldDefineFilterOptionsOnConstructor()
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
|
||||
|
||||
$actualArguments = $rule->arguments;
|
||||
$expectedArguments = array(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
|
||||
|
||||
$this->assertEquals($expectedArguments, $actualArguments);
|
||||
}
|
||||
|
||||
public function testShouldUseDefineFilterToValidate()
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_EMAIL);
|
||||
|
||||
$this->assertTrue($rule->validate('henriquemoody@users.noreply.github.com'));
|
||||
}
|
||||
|
||||
public function testShouldUseDefineFilterOptionsToValidate()
|
||||
{
|
||||
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
|
||||
|
||||
$this->assertTrue($rule->validate('http://example.com?foo=bar'));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue