mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Testes, fixed last merge.
This commit is contained in:
parent
34a85106cf
commit
cdafd4b1ff
|
@ -3,6 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use ReflectionClass;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Zend extends AbstractRule
|
||||
{
|
||||
|
@ -10,37 +11,38 @@ class Zend extends AbstractRule
|
|||
protected $messages = array();
|
||||
protected $zendValidator;
|
||||
|
||||
public function __construct($name, $params=array())
|
||||
public function __construct($validator, $params=array())
|
||||
{
|
||||
if (is_object($validatorName)) {
|
||||
$this->zendValidator = $validatorName;
|
||||
} else {
|
||||
if (mb_substr($validatorName, 0, 4) != 'Zend') {
|
||||
$validatorName = 'Zend\Validator\\' . $validatorName;
|
||||
} else {
|
||||
$validatorName = '\\'. $validatorName;
|
||||
}
|
||||
|
||||
$zendMirror = new ReflectionClass($validatorName);
|
||||
if ($zendMirror->hasMethod('__construct')) {
|
||||
$this->zendValidator = $zendMirror->newInstanceArgs($params);
|
||||
} else {
|
||||
$this->zendValidator = $zendMirror->newInstance();
|
||||
}
|
||||
}
|
||||
if (is_object($validator))
|
||||
return $this->zendValidator = $validator;
|
||||
|
||||
if (!is_string($validator))
|
||||
throw new ComponentException('Invalid Validator Construct');
|
||||
|
||||
if (false === stripos($validator, 'Zend'))
|
||||
$validator = "Zend\Validator\\{$validator}";
|
||||
else
|
||||
$validator = "\\{$validator}";
|
||||
|
||||
$zendMirror = new ReflectionClass($validator);
|
||||
|
||||
if ($zendMirror->hasMethod('__construct'))
|
||||
$this->zendValidator = $zendMirror->newInstanceArgs($params);
|
||||
else
|
||||
$this->zendValidator = $zendMirror->newInstance();
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
$exceptions = array();
|
||||
$validator = clone $this->zendValidator;
|
||||
|
||||
|
||||
if ($validator->isValid($input))
|
||||
return true;
|
||||
else
|
||||
foreach ($validator->getMessages() as $m)
|
||||
$exceptions[] = $this->reportError($m, get_object_vars($this));
|
||||
|
||||
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
<email>alexandre@gaigalas.net</email>
|
||||
<active>yes</active>
|
||||
</lead>
|
||||
<date>2011-05-02</date>
|
||||
<time>19:35:02</time>
|
||||
<date>2011-05-03</date>
|
||||
<time>10:31:25</time>
|
||||
<version>
|
||||
<release>0.2</release>
|
||||
<api>0.2</api>
|
||||
|
@ -116,7 +116,7 @@
|
|||
<file baseinstalldir="Respect/Validation" md5sum="74c428b8fc8430cd731b9078f72f030b" name="Rules/StartsWith.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="52fa7bda9b8ab3fceb20868468771392" name="Rules/String.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f6aeceea5d7b4522cd9f97b27088e351" name="Rules/Tld.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="afa7d33bee75d08ac2302eb5491b590b" name="Rules/Zend.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d92cd85514acce6870bc3c7abd3a82d3" name="Rules/Zend.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="904f06fb7ae379ec7f5a8af65a30e365" name="ExceptionIterator.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a19a2b951bb4aa67531fb6dc5a161e46" name="Validatable.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="03f21c4b124a2e64f3b0548564549172" name="Validator.php" role="php" />
|
||||
|
@ -143,7 +143,7 @@
|
|||
<release>alpha</release>
|
||||
<api>alpha</api>
|
||||
</stability>
|
||||
<date>2011-05-02</date>
|
||||
<date>2011-05-03</date>
|
||||
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
|
||||
<notes>
|
||||
.
|
||||
|
|
|
@ -14,14 +14,35 @@ class ZendTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testSimpleOk()
|
||||
{
|
||||
$v = new Zend('alnum');
|
||||
$v = new Zend('Alnum');
|
||||
$this->assertTrue($v->validate('wp2oiur'));
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
public function testFullNsOk()
|
||||
{
|
||||
$v = new Zend('Zend\\Validator\\Alnum');
|
||||
$this->assertTrue($v->validate('wp2oiur'));
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
public function testExtended()
|
||||
{
|
||||
$v = new Zend(new MyValidator);
|
||||
$this->assertTrue($v->validate('wp2oiur'));
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
public function testInstanceOk()
|
||||
{
|
||||
$v = new Zend(new \Zend\Validator\Alnum);
|
||||
$this->assertTrue($v->validate('wp2oiur'));
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
public function testNamespaceOk()
|
||||
{
|
||||
$v = new Zend('sitemap_lastmod');
|
||||
$v = new Zend('Sitemap\\Lastmod');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -36,7 +57,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testParamsOk()
|
||||
{
|
||||
$v = new Zend('stringLength', array('min' => 10, 'max' => 25));
|
||||
$v = new Zend('StringLength', array('min' => 10, 'max' => 25));
|
||||
$this->assertTrue($v->assert('owurhfojgboerjng'));
|
||||
}
|
||||
|
||||
|
@ -45,8 +66,13 @@ class ZendTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public function testParamsNot()
|
||||
{
|
||||
$v = new Zend('stringLength', array('min' => 10, 'max' => 25));
|
||||
$v = new Zend('StringLength', array('min' => 10, 'max' => 25));
|
||||
$this->assertFalse($v->assert('aw'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MyValidator extends \Zend\Validator\Alnum
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in a new issue