mirror of
https://github.com/Respect/Validation.git
synced 2024-06-22 07:16:32 +02:00
Added Zend Validation support
This commit is contained in:
parent
27c7739abd
commit
fc661bcfec
40
library/Respect/Validation/Rules/Zend.php
Normal file
40
library/Respect/Validation/Rules/Zend.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?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;
|
||||
|
||||
class Zend extends AbstractRule implements Validatable
|
||||
{
|
||||
|
||||
protected $messages = array();
|
||||
protected $zendValidator;
|
||||
|
||||
public function __construct($name, $params=array())
|
||||
{
|
||||
$zendMirror = new ReflectionClass('Zend_Validate_' . ucfirst($name));
|
||||
$this->zendValidator = $zendMirror->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $this->zendValidator->isValid($input);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input)) {
|
||||
$exceptions = array();
|
||||
foreach ($this->zendValidator->getMessages() as $m) {
|
||||
$exceptions[] = new InvalidException($m);
|
||||
}
|
||||
throw new InvalidException($exceptions);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
38
tests/library/Respect/Validation/Rules/ZendTest.php
Normal file
38
tests/library/Respect/Validation/Rules/ZendTest.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class ZendTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testSimpleOk()
|
||||
{
|
||||
$v = new Zend('alnum');
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidException
|
||||
*/
|
||||
public function testSimpleNot()
|
||||
{
|
||||
$v = new Zend('alnum');
|
||||
$this->assertTrue($v->assert('#$%#$%'));
|
||||
}
|
||||
|
||||
public function testParamsOk()
|
||||
{
|
||||
$v = new Zend('stringLength', array('min' => 10, 'max' => 25));
|
||||
$this->assertTrue($v->assert('owurhfojgboerjng'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidException
|
||||
*/
|
||||
public function testParamsNot()
|
||||
{
|
||||
$v = new Zend('stringLength', array('min' => 10, 'max' => 25));
|
||||
$this->assertTrue($v->assert('aw'));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue