New Sf valitador that uses Symfony 2.0 Validators and Constraints. Moved Zend validator to support only the 2.x ZF.

This commit is contained in:
Alexandre Gomes Gaigalas 2010-10-06 18:06:55 -03:00
parent fc661bcfec
commit a2fbdc340f
4 changed files with 79 additions and 1 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
library/Zend/
library/Symfony/

View file

@ -0,0 +1,52 @@
<?php
namespace Respect\Validation\Rules;
use ReflectionClass;
use Respect\Validation\Validatable;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\CallbackException;
use Respect\Validation\Exceptions\InvalidException;
use Symfony\Component\Validator\ConstraintViolation;
class Sf extends AbstractRule implements Validatable
{
protected $messages = array();
protected $constraint;
protected $validator;
protected $name;
public function __construct($name, $params=array())
{
$this->name = ucfirst($name);
$sfMirrorConstraint = new ReflectionClass(
'Symfony\Component\Validator\Constraints\\' . $this->name
);
$this->constraint = $sfMirrorConstraint->newInstanceArgs($params);
}
public function validate($input)
{
$validatorName = 'Symfony\Component\Validator\Constraints\\'
. $this->name . 'Validator';
$this->validator = new $validatorName;
return $this->validator->isValid($input, $this->constraint);
}
public function assert($input)
{
if (!$this->validate($input)) {
$violation = new ConstraintViolation(
$this->validator->getMessageTemplate(),
$this->validator->getMessageParameters(),
'',
'',
$input
);
throw new InvalidException($violation->getMessage());
}
return true;
}
}

View file

@ -16,7 +16,7 @@ class Zend extends AbstractRule implements Validatable
public function __construct($name, $params=array())
{
$zendMirror = new ReflectionClass('Zend_Validate_' . ucfirst($name));
$zendMirror = new ReflectionClass('Zend\Validator\\' . ucfirst($name));
$this->zendValidator = $zendMirror->newInstanceArgs($params);
}

View file

@ -0,0 +1,24 @@
<?php
namespace Respect\Validation\Rules;
class SfTest extends \PHPUnit_Framework_TestCase
{
public function testParamsOk()
{
$v = new Sf('minLength', array('limit'=>3));
$this->assertTrue($v->assert('wp2oiur'));
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
*/
public function testParamsNot()
{
$v = new Sf('minLength', array('limit'=>3));
$this->assertTrue($v->assert('a'));
}
}