Revert "Revert "Message modes for exceptions (in draft)""

This reverts commit 2041d4a33a.
This commit is contained in:
Alexandre 2011-04-09 20:34:21 -03:00
parent 2041d4a33a
commit cc278caffd
45 changed files with 317 additions and 79 deletions

View file

@ -7,8 +7,14 @@ class AbstractGroupedException extends AbstractNestedException
const NONE = 0;
const SOME = 1;
public static $defaultTemplates = array(
self::NONE => 'All of the required rules must pass for {{name}}',
self::SOME => 'These rules must pass for {{name}}',
self::MODE_DEFAULT => array(
self::NONE => 'All of the required rules must pass for {{name}}',
self::SOME => 'These rules must pass for {{name}}',
),
self::MODE_NEGATIVE => array(
self::NONE => 'None of there rules must pass for {{name}}',
self::SOME => 'These rules must not pass for {{name}}',
)
);
public function chooseTemplate()

View file

@ -6,8 +6,14 @@ class AllOfException extends AbstractGroupedException
{
public static $defaultTemplates = array(
self::NONE => 'All of the {{failed}} required rules must pass for {{name}}',
self::SOME => 'These {{failed}} rules must pass for {{name}}',
self::MODE_DEFAULT => array(
self::NONE => 'All of the {{failed}} required rules must pass for {{name}}',
self::SOME => 'These {{failed}} rules must pass for {{name}}',
),
self::MODE_NEGATIVE => array(
self::NONE => 'None of these {{failed}} rules must pass for {{name}}',
self::SOME => 'These {{failed}} rules must not pass for {{name}}',
)
);
}

View file

@ -6,8 +6,14 @@ class AlnumException extends AlphaException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and "{{additionalChars}}"'
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and "{{additionalChars}}"'
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)',
self::EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or "{{additionalChars}}"'
)
);
}

View file

@ -7,8 +7,14 @@ class AlphaException extends ValidationException
const EXTRA = 1;
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must contain only letters (a-z)',
self::EXTRA => '{{name}} must contain only letters (a-z) and "{{additionalChars}}"'
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must contain only letters (a-z)',
self::EXTRA => '{{name}} must contain only letters (a-z) and "{{additionalChars}}"'
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not contain letters (a-z)',
self::EXTRA => '{{name}} must not contain letters (a-z) or "{{additionalChars}}"'
)
);
public function chooseTemplate()

View file

@ -6,7 +6,12 @@ class ArrException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be an array',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an array',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an array',
)
);
}

View file

@ -6,8 +6,14 @@ class AtLeastException extends AbstractGroupedException
{
public static $defaultTemplates = array(
self::NONE => 'At least {{howMany}} of the {{failed}} required rules must pass for {{name}}',
self::SOME => 'At least {{howMany}} of the {{failed}} required rules must pass for {{name}}, only {{passed}} passed.',
self::MODE_DEFAULT => array(
self::NONE => 'At least {{howMany}} of the {{failed}} required rules must pass for {{name}}',
self::SOME => 'At least {{howMany}} of the {{failed}} required rules must pass for {{name}}, only {{passed}} passed.',
),
self::MODE_NEGATIVE => array(
self::NONE => 'At least {{howMany}} of the {{failed}} required rules must not pass for {{name}}',
self::SOME => 'At least {{howMany}} of the {{failed}} required rules must not pass for {{name}}, only {{passed}} passed.',
)
);
}

View file

@ -7,8 +7,14 @@ class AttributeException extends AbstractNestedException
const NOT_PRESENT = 0;
const INVALID = 1;
public static $defaultTemplates = array(
self::NOT_PRESENT => 'Attribute {{name}} must be present',
self::INVALID => 'Attribute {{name}} must be valid',
self::MODE_DEFAULT => array(
self::NOT_PRESENT => 'Attribute {{name}} must be present',
self::INVALID => 'Attribute {{name}} must be valid',
),
self::MODE_NEGATIVE => array(
self::NOT_PRESENT => 'Attribute {{name}} must not be present',
self::INVALID => 'Attribute {{name}} must not validate',
)
);
public function chooseTemplate()

View file

@ -9,9 +9,16 @@ class BetweenException extends AbstractNestedException
const GREATER = 2;
public static $defaultTemplates = array(
self::BOTH => '{{name}} must be between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must be greater than {{minValue}}',
self::GREATER => '{{name}} must be lower than {{maxValue}}',
self::MODE_DEFAULT => array(
self::BOTH => '{{name}} must be between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must be greater than {{minValue}}',
self::GREATER => '{{name}} must be lower than {{maxValue}}',
),
self::MODE_NEGATIVE => array(
self::BOTH => '{{name}} must not be between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must not be greater than {{minValue}}',
self::GREATER => '{{name}} must not be lower than {{maxValue}}',
)
);
public function configure($name, array $params=array())

View file

@ -6,7 +6,12 @@ class CallbackException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be valid',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be valid',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be valid',
)
);
}

View file

@ -6,7 +6,12 @@ class ContainsException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must contain the value "{{containsValue}}"',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must contain the value "{{containsValue}}"',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not contain the value "{{containsValue}}"',
)
);
}

View file

@ -7,8 +7,14 @@ class DateException extends ValidationException
const FORMAT = 1;
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be a valid date',
self::FORMAT => '{{name}} must be a valid date. Sample format: {{format}}'
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a valid date',
self::FORMAT => '{{name}} must be a valid date. Sample format: {{format}}'
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid date',
self::FORMAT => '{{name}} must not be a valid date in the format {{format}}'
)
);
public function configure($name, array $params=array())

View file

@ -6,8 +6,14 @@ class DigitsException extends AlphaException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must contain only digits (0-9)',
self::EXTRA => '{{name}} must contain only digits (0-9) and "{{additionalChars}}"'
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must contain only digits (0-9)',
self::EXTRA => '{{name}} must contain only digits (0-9) and "{{additionalChars}}"'
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not contain digits (0-9)',
self::EXTRA => '{{name}} must not contain digits (0-9) or "{{additionalChars}}"'
)
);
}

View file

@ -6,7 +6,12 @@ class DomainException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be a valid domain',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a valid domain',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid domain',
)
);
}

View file

@ -6,7 +6,12 @@ class EachException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => 'Each item in {{name}} must be valid',
self::MODE_DEFAULT => array(
self::STANDARD => 'Each item in {{name}} must be valid',
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'Each item in {{name}} must not validate',
)
);
}

View file

@ -6,7 +6,12 @@ class EndsWithException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must end with ({{endValue}})',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must end with ({{endValue}})',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not end with ({{endValue}})',
)
);
}

View file

@ -8,8 +8,14 @@ class EqualsException extends ValidationException
const IDENTICAL = 0;
public static $defaultTemplates = array(
self::EQUALS => '{{name}} must be equals {{compareTo}}',
self::IDENTICAL => '{{name}} must be identical as {{compareTo}}',
self::MODE_DEFAULT => array(
self::EQUALS => '{{name}} must be equals {{compareTo}}',
self::IDENTICAL => '{{name}} must be identical as {{compareTo}}',
),
self::MODE_NEGATIVE => array(
self::EQUALS => '{{name}} must not be equals {{compareTo}}',
self::IDENTICAL => '{{name}} must not be identical as {{compareTo}}',
)
);
public function chooseTemplate()

View file

@ -6,7 +6,12 @@ class FloatException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be a float number',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a float number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a float number',
)
);
}

View file

@ -6,7 +6,12 @@ class HexaException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be a hexadecimal number',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a hexadecimal number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a hexadecimal number',
)
);
}

View file

@ -6,7 +6,12 @@ class InException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be in ({{haystack}})',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be in ({{haystack}})',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be in ({{haystack}})',
)
);
}

View file

@ -6,7 +6,12 @@ class InstanceException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be an instance of {{instanceName}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an instance of {{instanceName}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an instance of {{instanceName}}',
)
);
}

View file

@ -6,7 +6,12 @@ class IntException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be an integer number',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an integer number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an integer number',
)
);
}

View file

@ -6,7 +6,12 @@ class IpException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be an IP address',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an IP address',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an IP address',
)
);
}

View file

@ -6,8 +6,14 @@ class KeyException extends AttributeException
{
public static $defaultTemplates = array(
self::NOT_PRESENT => 'Key {{reference}} must be present on {{name}}',
self::INVALID => 'Key {{reference}} must be valid on {{name}}',
self::MODE_DEFAULT => array(
self::NOT_PRESENT => 'Key {{reference}} must be present on {{name}}',
self::INVALID => 'Key {{reference}} must be valid on {{name}}',
),
self::MODE_NEGATIVE => array(
self::NOT_PRESENT => 'Key {{reference}} must not be present on {{name}}',
self::INVALID => 'Key {{reference}} must not be valid on {{name}}',
)
);
}

View file

@ -8,6 +8,19 @@ class LengthException extends ValidationException
const LOWER = 1;
const GREATER = 2;
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must have a length greater than {{minValue}}',
self::GREATER => '{{name}} must have a length lower than {{maxValue}}',
),
self::MODE_NEGATIVE => array(
self::BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must not have a length greater than {{minValue}}',
self::GREATER => '{{name}} must not have a length lower than {{maxValue}}',
)
);
public function configure($name, array $params=array())
{
$params['minValue'] = static::stringify($params['minValue']);
@ -25,12 +38,6 @@ class LengthException extends ValidationException
return static::BOTH;
}
public static $defaultTemplates = array(
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
self::LOWER => '{{name}} must have a length greater than {{minValue}}',
self::GREATER => '{{name}} must have a length lower than {{maxValue}}',
);
}
/**

View file

@ -7,8 +7,14 @@ class MaxException extends ValidationException
const INCLUSIVE = 1;
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be lower than {{maxValue}}',
self::INCLUSIVE => '{{name}} must be lower than or equals {{maxValue}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be lower than {{maxValue}}',
self::INCLUSIVE => '{{name}} must be lower than or equals {{maxValue}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be lower than {{maxValue}}',
self::INCLUSIVE => '{{name}} must not be lower than or equals {{maxValue}}',
)
);
public function chooseTemplate()

View file

@ -7,8 +7,14 @@ class MinException extends ValidationException
const INCLUSIVE = 1;
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be greater than {{minValue}}',
self::INCLUSIVE => '{{name}} must be greater than or equals {{minValue}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be greater than {{minValue}}',
self::INCLUSIVE => '{{name}} must be greater than or equals {{minValue}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be greater than {{minValue}}',
self::INCLUSIVE => '{{name}} must not be greater than or equals {{minValue}}',
)
);
public function chooseTemplate()

View file

@ -6,7 +6,12 @@ class NegativeException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be negative',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be negative',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be negative',
)
);
}

View file

@ -6,7 +6,12 @@ class NoWhitespaceException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must not contain whitespace',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must not contain whitespace',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not not contain whitespace',
)
);
}

View file

@ -6,9 +6,13 @@ class NoneOfException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => 'None of these rules must pass for {{name}}',
self::MODE_DEFAULT => array(
self::STANDARD => 'None of these rules must pass for {{name}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'All of these rules must pass for {{name}}',
)
);
}

View file

@ -7,8 +7,14 @@ class NotEmptyException extends ValidationException
const STANDARD = 0;
const NAMED = 1;
public static $defaultTemplates = array(
self::STANDARD => 'The value must not be empty',
self::NAMED => '{{name}} must not be empty',
self::MODE_DEFAULT => array(
self::STANDARD => 'The value must not be empty',
self::NAMED => '{{name}} must not be empty',
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'The value must be empty',
self::NAMED => '{{name}} must be empty',
)
);
public function chooseTemplate()

View file

@ -6,7 +6,12 @@ class NullValueException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be null',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be null',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be null',
)
);
}

View file

@ -6,7 +6,12 @@ class NumericException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be numeric',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be numeric',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be numeric',
)
);
}

View file

@ -6,7 +6,12 @@ class ObjectException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be an object',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an object',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an object',
)
);
}

View file

@ -6,7 +6,12 @@ class OneOfException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => 'At least one of these rules must pass for {{name}}',
self::MODE_DEFAULT => array(
self::STANDARD => 'At least one of these rules must pass for {{name}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'At least one of these rules must not pass for {{name}}',
)
);
}

View file

@ -6,7 +6,12 @@ class PositiveException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be positive',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be positive',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not MODE_be positive',
)
);
}

View file

@ -6,7 +6,12 @@ class RegexException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must validate against {{regex}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must validate against {{regex}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not validate against {{regex}}',
)
);
}

View file

@ -6,7 +6,12 @@ class SfException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}}',
)
);
}

View file

@ -6,7 +6,12 @@ class StartsWithException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must start with ({{startValue}})',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must start with ({{startValue}})',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not start with ({{startValue}})',
)
);
}

View file

@ -6,7 +6,12 @@ class StringException extends ValidationException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}} must be a string',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a string',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be string',
)
);
}

View file

@ -6,7 +6,12 @@ class TldException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT =>array(
self::STANDARD => '{{name}} must be a valid top-level domain name',
) ,
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid top-level domain name',
)
);
}

View file

@ -9,11 +9,19 @@ use Respect\Validation\Validatable;
class ValidationException extends InvalidArgumentException
{
const MODE_DEFAULT = 1;
const MODE_NEGATIVE = 2;
const STANDARD = 0;
public static $defaultTemplates = array(
self::STANDARD => 'Data validation failed for %s'
self::MODE_DEFAULT => array(
self::STANDARD => 'Data validation failed for %s'
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'Data validation failed for %s'
)
);
protected $id = 'validation';
protected $mode = self::MODE_DEFAULT;
protected $name = '';
protected $template = '';
protected $params = array();
@ -55,7 +63,7 @@ class ValidationException extends InvalidArgumentException
public function chooseTemplate()
{
return key(static::$defaultTemplates);
return key(static::$defaultTemplates[$this->mode]);
}
public function configure($name, array $params = array())
@ -119,26 +127,36 @@ class ValidationException extends InvalidArgumentException
return $this;
}
public function setMode($mode)
{
$this->mode = $mode;
$this->template = $this->buildTemplate();
return $this;
}
public function setParam($key, $value)
{
$this->params[$key] = static::stringify($value);
return $this;
}
public function setParams(array $params)
{
foreach ($params as $key => $value)
$this->setParam($key, $value);
return $this;
}
public function setTemplate($template)
{
$this->template = $template;
return $this;
}
protected function buildTemplate()
{
$templateKey = $this->chooseTemplate();
return static::$defaultTemplates[$templateKey];
return static::$defaultTemplates[$this->mode][$templateKey];
}
protected function guessId()

View file

@ -6,7 +6,12 @@ class ZendException extends AbstractNestedException
{
public static $defaultTemplates = array(
self::STANDARD => '{{name}}',
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}}',
)
);
}
@ -20,7 +25,7 @@ class ZendException extends AbstractNestedException
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* * Redistributions of source code must retain the above copyright notice,.
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,

View file

@ -27,13 +27,13 @@ class Not extends AbstractRule
} catch (ValidationException $e) {
return true;
}
$e = $this->rule->reportError($input);
//TODO very very very nasty hack. Need to think of a better solution
$e->setTemplate(str_replace('must', 'must not', $e->getTemplate()));
throw $e;
throw $this->rule
->reportError($input)
->setMode(ValidationException::MODE_NEGATIVE);
}
}
/**
* LICENSE
*

View file

@ -46,16 +46,6 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo', $x->getTemplate());
}
public function testGetTemplateChosen()
{
$x = new ValidationException();
$x->configure('bar');
$this->assertEquals(
ValidationException::$defaultTemplates[0],
$x->getTemplate()
);
}
/**
* @dataProvider providerForStringify
*/

View file

@ -137,6 +137,15 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
v::notEmpty()->assert('aaa');
}
public function testNot()
{
try {
v::not(v::ip())->assert('192.168.0.1');
} catch (\Exception $e) {
echo $e->getFullMessage();
}
}
public function testNullValue()
{
v::nullValue()->assert(null);