mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Filter and sanitizing prototype update
This commit is contained in:
parent
50a15db9db
commit
41286de8b5
|
@ -4,11 +4,11 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Filterable;
|
||||
use Respect\Validation\Sanitizable;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
abstract class AbstractComposite extends AbstractRule implements Validatable, \Respect\Validation\Filterable
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
{
|
||||
|
||||
protected $rules = array();
|
||||
|
@ -28,11 +28,19 @@ abstract class AbstractComposite extends AbstractRule implements Validatable, \R
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function sanitize($input)
|
||||
{
|
||||
foreach ($this->getRules() as $f)
|
||||
if ($f instanceof Sanitizable)
|
||||
$input = $f->sanitize($input);
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
public function filter($input)
|
||||
{
|
||||
foreach ($this->getRules() as $f)
|
||||
if ($f instanceof Filterable)
|
||||
$input = $f->filter($input);
|
||||
$input = $f->filter($input);
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,11 @@ abstract class AbstractRule implements Validatable
|
|||
return $this->assert($input);
|
||||
}
|
||||
|
||||
public function filter($input)
|
||||
{
|
||||
return $this->validate($input) ? $input : null;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Filterable;
|
||||
use Respect\Validation\Sanitizable;
|
||||
|
||||
class Int extends AbstractRule implements Filterable
|
||||
class Int extends AbstractRule implements Sanitizable
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
|
@ -12,7 +12,7 @@ class Int extends AbstractRule implements Filterable
|
|||
return is_numeric($input) && (int) $input == $input;
|
||||
}
|
||||
|
||||
public function filter($input)
|
||||
public function sanitize($input)
|
||||
{
|
||||
return (int) $input;
|
||||
}
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
namespace Respect\Validation;
|
||||
|
||||
/**
|
||||
* Interface for filtering rules
|
||||
* Interface for sanitizing rules
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*/
|
||||
interface Filterable
|
||||
interface Sanitizable
|
||||
{
|
||||
public function filter($input);
|
||||
public function sanitize($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,6 +12,8 @@ interface Validatable
|
|||
public function assert($input);
|
||||
|
||||
public function check($input);
|
||||
|
||||
public function filter($input);
|
||||
|
||||
public function getName();
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($o->assert('any'));
|
||||
}
|
||||
|
||||
public function test_filtering_should_work_for_composite_filters()
|
||||
public function test_sanitizing_should_work_for_composite_rules()
|
||||
{
|
||||
$valid = new Digits();
|
||||
$filter = new Int();
|
||||
|
@ -60,7 +60,18 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($o->validate(12));
|
||||
$this->assertTrue($o->check(12));
|
||||
$this->assertTrue($o->assert(12));
|
||||
$this->assertSame(12, $o->filter('12 monkeys'));
|
||||
$this->assertSame(12, $o->sanitize('12 monkeys'));
|
||||
}
|
||||
|
||||
public function test_filter_should_work_for_composite_rules()
|
||||
{
|
||||
$valid = new Digits();
|
||||
$filter = new Int();
|
||||
$o = new AllOf($valid, $filter);
|
||||
$this->assertTrue($o->validate(12));
|
||||
$this->assertTrue($o->check(12));
|
||||
$this->assertTrue($o->assert(12));
|
||||
$this->assertSame(null, $o->filter('12 monkeys'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,11 +35,12 @@ class IntTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
|
||||
/**
|
||||
* @dataProvider providerForFilterInt
|
||||
* @dataProvider providerForSanitizeInt
|
||||
*/
|
||||
public function test_integer_filter_should_return_filtered_value($unfiltered, $expected)
|
||||
public function test_integer_filter_should_return_filtered_value($unfiltered, $expected, $filtered)
|
||||
{
|
||||
$this->assertSame($expected, $this->intValidator->filter($unfiltered));
|
||||
$this->assertSame($expected, $this->intValidator->sanitize($unfiltered));
|
||||
$this->assertSame($filtered, $this->intValidator->filter($unfiltered));
|
||||
}
|
||||
|
||||
public function providerForInt()
|
||||
|
@ -65,13 +66,14 @@ class IntTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function providerForFilterInt()
|
||||
public function providerForSanitizeInt()
|
||||
{
|
||||
return array(
|
||||
array(12, 12),
|
||||
array('12 rabbits', 12),
|
||||
array('1.44', 1),
|
||||
array(1e-5, 0),
|
||||
array(12, 12, 12),
|
||||
array('12 rabbits', 12, null),
|
||||
array('1.44', 1, null),
|
||||
array(1e-5, 0, null),
|
||||
array('ksdhbf', 0, null),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue