Optional key and attribute validators

This commit is contained in:
Alexandre Gomes Gaigalas 2010-11-08 23:56:28 -02:00
parent 0d4e686a26
commit f9b98679b2
5 changed files with 117 additions and 1 deletions

View file

@ -0,0 +1,16 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\HasAttribute;
class HasOptionalAttribute extends HasAttribute
{
public function validate($input)
{
return @!property_exists($input, $this->attribute)
|| parent::validate($input->{$this->attribute});
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\HasKey;
class HasOptionalKey extends HasKey
{
public function validate($input)
{
return!@array_key_exists($this->key, $input)
|| parent::validate($input[$this->key]);
}
}

View file

@ -11,7 +11,7 @@
<active>yes</active>
</lead>
<date>2010-11-08</date>
<time>22:37:41</time>
<time>23:56:20</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -66,6 +66,8 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="5ebc3ea7c4b1f970eb30ce4dfedf7f9c" name="Rules/Float.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e4440f96d078c95bea25d5449df549f4" name="Rules/HasAttribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="6f8cc9b5671f80cd8f8faddb8314e896" name="Rules/HasKey.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9034f4c1f69241ed53461de52a94cc7a" name="Rules/HasOptionalAttribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="409bd3ef97ec1e4d80fe237b45215ee1" name="Rules/HasOptionalKey.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2df96e8e150278b96cf8b34ee1a71c4d" name="Rules/Hexa.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9ea7eaa05496bfeee36a17db9c6f84c2" name="Rules/Instance.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="60417393f6f6ec334e7d165a7d56dc32" name="Rules/Ip.php" role="php" />

View file

@ -0,0 +1,41 @@
<?php
namespace Respect\Validation\Rules;
class HasOptionalAttributeTest extends \PHPUnit_Framework_TestCase
{
public function testHasOptionalAttribute()
{
$validator = new HasOptionalAttribute('bar');
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->assert($obj));
}
public function testNotNull()
{
$validator = new HasOptionalAttribute('bar');
$obj = new \stdClass;
$obj->baraaaaa = 'foo';
$this->assertTrue($validator->assert($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testInvalidParameters()
{
$validator = new HasOptionalAttribute(array('invalid'));
}
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasOptionalAttribute('bar', $subValidator);
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->assert($obj));
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Respect\Validation\Rules;
class HasOptionalKeyTest extends \PHPUnit_Framework_TestCase
{
public function testHasOptionalKey()
{
$validator = new HasOptionalKey('bar');
$obj = array();
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));
}
public function testNotNull()
{
$validator = new HasOptionalKey('bar');
$obj = array();
$obj['baraaaaaa'] = 'foo';
$this->assertTrue($validator->assert($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testInvalidParameters()
{
$validator = new HasOptionalKey(array('invalid'));
}
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasOptionalKey('bar', $subValidator);
$obj = array();
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));
}
}