Refactored names for some validators

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-05 23:17:12 -02:00
parent f776bde6b8
commit 35aee77111
16 changed files with 86 additions and 173 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace Respect\Validation\Exceptions;
class AttributeException extends ValidationException
{
const INVALID_ATTRIBUTE= 'Attribute_1';
const INVALID_ATTRIBUTE_RELATED = 'Attribute_2';
public static $defaultTemplates = array(
self::INVALID_ATTRIBUTE => '"%2$s" is not present',
self::INVALID_ATTRIBUTE_RELATED => '"%2$s" is invalid',
);
public function chooseTemplate($input, $attributeName, $hasTheAttribute)
{
if (!$hasTheAttribute)
return self::INVALID_ATTRIBUTE;
else
return self::INVALID_ATTRIBUTE_RELATED;
}
}

View file

@ -1,22 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class HasAttributeException extends ValidationException
{
const INVALID_HAS_ATTRIBUTE= 'HasAttribute_1';
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasAttribute_2';
public static $defaultTemplates = array(
self::INVALID_HAS_ATTRIBUTE => '"%2$s" is not present',
self::INVALID_HAS_ATTRIBUTE_RELATED => '"%2$s" is invalid',
);
public function chooseTemplate($input, $attributeName, $hasTheAttribute)
{
if (!$hasTheAttribute)
return self::INVALID_HAS_ATTRIBUTE;
else
return self::INVALID_HAS_ATTRIBUTE_RELATED;
}
}

View file

@ -1,22 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class HasKeyException extends ValidationException
{
const INVALID_HAS_KEY= 'HasKey_1';
const INVALID_HAS_KEY_RELATED = 'HasKey_2';
public static $defaultTemplates = array(
self::INVALID_HAS_KEY => '"%2$s" is not present',
self::INVALID_HAS_KEY_RELATED => '"%2$s" is invalid',
);
public function chooseTemplate($input, $attributeName, $hasTheAttribute)
{
if (!$hasTheAttribute)
return self::INVALID_HAS_KEY;
else
return self::INVALID_HAS_KEY_RELATED;
}
}

View file

@ -1,12 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class HasOptionalAttributeException extends ValidationException
{
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasOptionalAttribute_1';
public static $defaultTemplates = array(
self::INVALID_HAS_ATTRIBUTE_RELATED => '"%2$s" is invalid',
);
}

View file

@ -1,12 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class HasOptionalKeyException extends ValidationException
{
const INVALID_HAS_KEY_RELATED = 'HasOptionalKey_1';
public static $defaultTemplates = array(
self::INVALID_HAS_Key_RELATED => '"%2$s" is invalid',
);
}

View file

@ -0,0 +1,22 @@
<?php
namespace Respect\Validation\Exceptions;
class KeyException extends ValidationException
{
const INVALID_KEY= 'Key_1';
const INVALID_KEY_RELATED = 'Key_2';
public static $defaultTemplates = array(
self::INVALID_KEY => '"%2$s" is not present',
self::INVALID_KEY_RELATED => '"%2$s" is invalid',
);
public function chooseTemplate($input, $attributeName, $TheAttribute)
{
if (!$TheAttribute)
return self::INVALID_KEY;
else
return self::INVALID_KEY_RELATED;
}
}

View file

@ -9,20 +9,23 @@ use Respect\Validation\Validatable;
use \ReflectionProperty;
use \ReflectionException;
abstract class AbstractRelated extends AllOf
abstract class AbstractRelated extends AbstractRule
{
const IS_OPTIONAL = false;
protected $reference = '';
public function __construct($reference, Validatable $referenceValidator=null)
protected $mandatory = true;
protected $reference = '';
protected $referenceValidator;
public function __construct($reference,
Validatable $referenceValidator=null, $mandatory=true)
{
if (!is_string($reference) || empty($reference))
throw new ComponentException(
'Invalid reference name'
);
$this->reference = $reference;
if (!is_null($referenceValidator))
$this->addRule($referenceValidator);
$this->referenceValidator = $referenceValidator;
$this->mandatory = $mandatory;
}
abstract protected function hasReference($input);
@ -43,17 +46,20 @@ abstract class AbstractRelated extends AllOf
public function validate($input)
{
if (!static::IS_OPTIONAL && !$this->hasReference($input))
if ($this->mandatory && !$this->hasReference($input))
return false;
return parent::validate($this->getReferenceValue($input));
if (!is_null($this->referenceValidator))
return $this->referenceValidator->validate($this->getReferenceValue($input));
return true;
}
public function assert($input)
{
if (!static::IS_OPTIONAL && !$this->hasReference($input))
if ($this->mandatory && !$this->hasReference($input))
throw $this->reportError($input);
try {
parent::assert($this->getReferenceValue($input));
if (!is_null($this->referenceValidator))
$this->referenceValidator->assert($this->getReferenceValue($input));
} catch (ValidationException $e) {
throw $this->reportError($input, $e);
} catch (ReflectionException $e) {

View file

@ -2,13 +2,12 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\HasAttributeException;
use Respect\Validation\Exceptions\AttributeException;
use Respect\Validation\Exceptions\ValidationException;
use \ReflectionProperty;
class HasAttribute extends AbstractRelated
class Attribute extends AbstractRelated
{
const IS_OPTIONAL = false;
protected function hasReference($input)
{
@ -24,7 +23,7 @@ class HasAttribute extends AbstractRelated
protected function createException()
{
return HasAttributeException::create();
return AttributeException::create();
}
}

View file

@ -1,16 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\HasOptionalAttributeException;
class HasOptionalAttribute extends HasAttribute
{
const IS_OPTIONAL = true;
protected function createException()
{
return HasOptionalAttributeException::create();
}
}

View file

@ -1,16 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\HasOptionalKeyException;
class HasOptionalKey extends HasKey
{
const IS_OPTIONAL = true;
protected function createException()
{
return HasOptionalKeyException::create();
}
}

View file

@ -2,13 +2,12 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\HasKeyException;
use Respect\Validation\Exceptions\KeyException;
use Respect\Validation\Exceptions\ValidationException;
use \ReflectionProperty;
class HasKey extends AbstractRelated
class Key extends AbstractRelated
{
const IS_OPTIONAL = false;
protected function hasReference($input)
{
@ -22,7 +21,7 @@ class HasKey extends AbstractRelated
protected function createException()
{
return HasKeyException::create();
return KeyException::create();
}
}

View file

@ -9,12 +9,12 @@ class PrivClass
}
class HasAttributeTest extends \PHPUnit_Framework_TestCase
class AttributeTest extends \PHPUnit_Framework_TestCase
{
public function testHasAttribute()
public function testAttribute()
{
$validator = new HasAttribute('bar');
$validator = new Attribute('bar');
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->assert($obj));
@ -25,7 +25,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
*/
public function testNotNull()
{
$validator = new HasAttribute('bar');
$validator = new Attribute('bar');
$obj = new \stdClass;
$obj->baraaaaa = 'foo';
$this->assertTrue($validator->assert($obj));
@ -37,7 +37,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidParameters($attributeName)
{
$validator = new HasAttribute($attributeName);
$validator = new Attribute($attributeName);
}
public function providerForInvalidAtrributeNames()
@ -52,7 +52,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasAttribute('bar', $subValidator);
$validator = new Attribute('bar', $subValidator);
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->assert($obj));
@ -61,7 +61,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
public function testValidatorPrivateAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasAttribute('bar', $subValidator);
$validator = new Attribute('bar', $subValidator);
$obj = new PrivClass;
$this->assertTrue($validator->assert($obj));
}

View file

@ -2,12 +2,12 @@
namespace Respect\Validation\Rules;
class HasKeyTest extends \PHPUnit_Framework_TestCase
class KeyTest extends \PHPUnit_Framework_TestCase
{
public function testHasKey()
public function testKey()
{
$validator = new HasKey('bar');
$validator = new Key('bar');
$obj = array();
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));
@ -18,7 +18,7 @@ class HasKeyTest extends \PHPUnit_Framework_TestCase
*/
public function testNotNull()
{
$validator = new HasKey('bar');
$validator = new Key('bar');
$obj = array();
$obj['baraaaaaa'] = 'foo';
$this->assertTrue($validator->assert($obj));
@ -29,13 +29,13 @@ class HasKeyTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidParameters()
{
$validator = new HasKey(array('invalid'));
$validator = new Key(array('invalid'));
}
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasKey('bar', $subValidator);
$validator = new Key('bar', $subValidator);
$obj = array();
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));

View file

@ -1,17 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class HasOptionalAttributeTest extends \PHPUnit_Framework_TestCase
{
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 10);
$validator = new HasOptionalAttribute('bar', $subValidator);
$obj = new \stdClass;
$obj->bar = 'foo';
$this->assertTrue($validator->assert($obj));
}
}

View file

@ -1,18 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class HasOptionalKeyTest extends \PHPUnit_Framework_TestCase
{
public function testValidatorAttribute()
{
$subValidator = new StringLength(1, 3);
$validator = new HasOptionalKey('bar', $subValidator);
$obj = array();
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));
}
}

View file

@ -68,21 +68,21 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
$validator = Validator::object()
->oneOf(
Validator::hasAttribute(
Validator::attribute(
'screen_name', Validator::alnum('_')->noWhitespace()
),
Validator::hasAttribute(
Validator::attribute(
'id', Validator::numeric()->between(1, 15)
)
)
->hasAttribute('created_at', Validator::date())
->hasAttribute('name', $v160 = Validator::stringLength(1, 160))
->hasAttribute('sex',
->attribute('created_at', Validator::date())
->attribute('name', $v160 = Validator::stringLength(1, 160))
->attribute('sex',
Validator::oneOf(
Validator::hexa(), Validator::float(), Validator::numeric()
))
->hasOptionalAttribute('description', $v160)
->hasOptionalAttribute('location', $v160);
->attribute('description', $v160, false)
->attribute('location', $v160, false);
try {
$validator->assert($target);
} catch (Exceptions\ValidationException $e) {