From df3533c9c891d6e6562f5d8e8befe2b019e6a359 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Gaigalas Date: Wed, 6 Oct 2010 18:27:14 -0300 Subject: [PATCH] Refactored all mentions of "message" to "message template" for further refactorings --- .../Validation/Rules/AbstractComposite.php | 6 +++--- .../Respect/Validation/Rules/AbstractRule.php | 20 +++++++++---------- library/Respect/Validation/Rules/Alnum.php | 6 +++--- library/Respect/Validation/Rules/Callback.php | 4 ++-- library/Respect/Validation/Rules/Date.php | 7 ++++--- .../Respect/Validation/Rules/DateBetween.php | 4 ++-- .../Respect/Validation/Rules/HasAttribute.php | 4 ++-- .../Respect/Validation/Rules/NoWhitespace.php | 4 ++-- .../Respect/Validation/Rules/NullValue.php | 4 ++-- library/Respect/Validation/Rules/Numeric.php | 4 ++-- .../Respect/Validation/Rules/StringLength.php | 8 +++++--- .../Validation/Rules/StringNotEmpty.php | 4 ++-- library/Respect/Validation/Validatable.php | 8 ++++++-- .../Respect/Validation/Rules/SfTest.php | 5 ++--- .../Respect/Validation/ValidatorTestCase.php | 9 +++++---- 15 files changed, 52 insertions(+), 45 deletions(-) diff --git a/library/Respect/Validation/Rules/AbstractComposite.php b/library/Respect/Validation/Rules/AbstractComposite.php index 9c7147d2..12bba243 100644 --- a/library/Respect/Validation/Rules/AbstractComposite.php +++ b/library/Respect/Validation/Rules/AbstractComposite.php @@ -22,9 +22,9 @@ abstract class AbstractComposite extends AbstractRule implements Validatable protected function appendRule(Validatable $validator) { $this->rules[spl_object_hash($validator)] = $validator; - $this->messages = array_merge( - $this->messages, $validator->getMessages() - ); + $this->setMessageTemplates(array_merge( + $this->getMessageTemplates(), $validator->getMessageTemplates() + )); } public function addRule($validator, $arguments=array()) diff --git a/library/Respect/Validation/Rules/AbstractRule.php b/library/Respect/Validation/Rules/AbstractRule.php index b2044d57..ed222d75 100644 --- a/library/Respect/Validation/Rules/AbstractRule.php +++ b/library/Respect/Validation/Rules/AbstractRule.php @@ -5,27 +5,27 @@ namespace Respect\Validation\Rules; abstract class AbstractRule { - protected $messages = array(); + protected $messageTemplates = array(); - public function setMessage($code, $message) + public function setMessageTemplate($code, $template) { - $this->messages[$code] = $message; + $this->messageTemplates[$code] = $template; } - public function getMessage($code) + public function getMessageTemplate($code) { - return $this->messages[$code]; + return $this->messageTemplates[$code]; } - public function setMessages(array $messages) + public function setMessageTemplates(array $templates) { - foreach ($messages as $code => $message) - $this->setMessage($code, $message); + foreach ($templates as $code => $message) + $this->setMessageTemplate($code, $message); } - public function getMessages() + public function getMessageTemplates() { - return $this->messages; + return $this->messageTemplates; } protected function getStringRepresentation($mixed) diff --git a/library/Respect/Validation/Rules/Alnum.php b/library/Respect/Validation/Rules/Alnum.php index f08ab74b..e7832164 100644 --- a/library/Respect/Validation/Rules/Alnum.php +++ b/library/Respect/Validation/Rules/Alnum.php @@ -10,7 +10,7 @@ class Alnum extends AbstractRule implements Validatable { const MSG_NOT_ALPHANUMERIC = 'Alnum_1'; const MSG_NOT_ALPHANUMERIC_ADDITIONAL = 'Alnum_2'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_NOT_ALPHANUMERIC => '%s does not contains only letters and digits', self::MSG_NOT_ALPHANUMERIC_ADDITIONAL => '%s does not contains only letters and digits (including %s)' ); @@ -33,13 +33,13 @@ class Alnum extends AbstractRule implements Validatable if (!$this->validate($input)) if (empty($this->additionalChars)) throw new NotAlphanumericException( - sprintf($this->getMessage(self::MSG_NOT_ALPHANUMERIC), + sprintf($this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC), $input) ); else throw new NotAlphanumericException( sprintf( - $this->getMessage(self::MSG_NOT_ALPHANUMERIC_ADDITIONAL), + $this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC_ADDITIONAL), $input, $this->additionalChars ) ); diff --git a/library/Respect/Validation/Rules/Callback.php b/library/Respect/Validation/Rules/Callback.php index 1d6ffb74..84a43372 100644 --- a/library/Respect/Validation/Rules/Callback.php +++ b/library/Respect/Validation/Rules/Callback.php @@ -9,7 +9,7 @@ use Respect\Validation\Exceptions\CallbackException; class Callback extends AbstractRule implements Validatable { const MSG_CALLBACK = 'Callback_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_CALLBACK => '%s does not validate against the provided callback.' ); protected $callback; @@ -28,7 +28,7 @@ class Callback extends AbstractRule implements Validatable { if (!$this->validate($input)) throw new CallbackException( - sprintf($this->getMessage(self::MSG_CALLBACK), + sprintf($this->getMessageTemplate(self::MSG_CALLBACK), $this->getStringRepresentation($input) ) ); diff --git a/library/Respect/Validation/Rules/Date.php b/library/Respect/Validation/Rules/Date.php index 46cb9e18..bbe16b27 100644 --- a/library/Respect/Validation/Rules/Date.php +++ b/library/Respect/Validation/Rules/Date.php @@ -10,7 +10,7 @@ class Date extends AbstractDate implements Validatable { const MSG_INVALID_DATE = 'Date_1'; const MSG_INVALID_FORMAT = 'Date_2'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_INVALID_DATE => '%s is not a valid date reference', self::MSG_INVALID_FORMAT => '%s is not a valid date in the %s format', ); @@ -33,12 +33,13 @@ class Date extends AbstractDate implements Validatable if (!$this->validate($input)) if (is_null($this->format)) throw new InvalidDate( - sprintf($this->getMessage(static::MSG_INVALID_DATE), $input) + sprintf($this->getMessageTemplate(static::MSG_INVALID_DATE), + $input) ); else throw new InvalidDate( sprintf( - $this->getMessage(static::MSG_INVALID_FORMAT), $input, + $this->getMessageTemplate(static::MSG_INVALID_FORMAT), $input, $this->format ) ); diff --git a/library/Respect/Validation/Rules/DateBetween.php b/library/Respect/Validation/Rules/DateBetween.php index 34ed232a..e2213e41 100644 --- a/library/Respect/Validation/Rules/DateBetween.php +++ b/library/Respect/Validation/Rules/DateBetween.php @@ -15,7 +15,7 @@ class DateBetween extends AbstractDate implements Validatable protected $min; protected $max; - protected $messages = array( + protected $messageTemplates = array( self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.' ); @@ -48,7 +48,7 @@ class DateBetween extends AbstractDate implements Validatable if (!$this->validate($target)) throw new DateOutOfBoundsException( sprintf( - $this->getMessage(self::MSG_OUT_OF_BOUNDS), + $this->getMessageTemplate(self::MSG_OUT_OF_BOUNDS), $this->formatDate($target), $this->formatDate($this->min), $this->formatDate($this->max) ) diff --git a/library/Respect/Validation/Rules/HasAttribute.php b/library/Respect/Validation/Rules/HasAttribute.php index 77612f12..b6ad58c4 100644 --- a/library/Respect/Validation/Rules/HasAttribute.php +++ b/library/Respect/Validation/Rules/HasAttribute.php @@ -10,7 +10,7 @@ use Respect\Validation\Rules\All; class HasAttribute extends All implements Validatable { const MSG_ATTRIBUTE_NOT_PRESENT = 'NullValue_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_ATTRIBUTE_NOT_PRESENT => 'Object does not have the attribute %s' ); protected $attribute = ''; @@ -33,7 +33,7 @@ class HasAttribute extends All implements Validatable if (!$this->validate($input)) throw new AttributeNotPresentException( sprintf( - $this->getMessage(self::MSG_ATTRIBUTE_NOT_PRESENT), + $this->getMessageTemplate(self::MSG_ATTRIBUTE_NOT_PRESENT), $this->attribute ) ); diff --git a/library/Respect/Validation/Rules/NoWhitespace.php b/library/Respect/Validation/Rules/NoWhitespace.php index ac21d1de..29b9c6be 100644 --- a/library/Respect/Validation/Rules/NoWhitespace.php +++ b/library/Respect/Validation/Rules/NoWhitespace.php @@ -9,7 +9,7 @@ use Respect\Validation\Exceptions\WhitespaceFoundException; class NoWhitespace extends AbstractRule implements Validatable { const MSG_WHITESPACE_FOUND = 'NoWhitespace_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_WHITESPACE_FOUND => '%s contains spaces, tabs, line breaks or other not allowed charaters.' ); @@ -22,7 +22,7 @@ class NoWhitespace extends AbstractRule implements Validatable { if (!$this->validate($input)) throw new WhitespaceFoundException( - sprintf($this->getMessage(self::MSG_WHITESPACE_FOUND), $input) + sprintf($this->getMessageTemplate(self::MSG_WHITESPACE_FOUND), $input) ); return true; } diff --git a/library/Respect/Validation/Rules/NullValue.php b/library/Respect/Validation/Rules/NullValue.php index 5e34d558..04c13580 100644 --- a/library/Respect/Validation/Rules/NullValue.php +++ b/library/Respect/Validation/Rules/NullValue.php @@ -9,7 +9,7 @@ use Respect\Validation\Exceptions\NotNullException; class NullValue extends AbstractRule implements Validatable { const MSG_NOT_NULL = 'NullValue_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_NOT_NULL => '%s is not null' ); @@ -22,7 +22,7 @@ class NullValue extends AbstractRule implements Validatable { if (!$this->validate($input)) throw new NotNullException( - sprintf($this->getMessage(self::MSG_NOT_NULL), $input) + sprintf($this->getMessageTemplate(self::MSG_NOT_NULL), $input) ); return true; } diff --git a/library/Respect/Validation/Rules/Numeric.php b/library/Respect/Validation/Rules/Numeric.php index eeba1a96..ab5c43f6 100644 --- a/library/Respect/Validation/Rules/Numeric.php +++ b/library/Respect/Validation/Rules/Numeric.php @@ -9,7 +9,7 @@ use Respect\Validation\Exceptions\NotNumericException; class Numeric extends AbstractRule implements Validatable { const MSG_NOT_NUMERIC = 'Numeric_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_NOT_NUMERIC => '%s is not a numeric value' ); @@ -22,7 +22,7 @@ class Numeric extends AbstractRule implements Validatable { if (!$this->validate($input)) throw new NotNumericException( - sprintf($this->getMessage(self::MSG_NOT_NUMERIC), $input) + sprintf($this->getMessageTemplate(self::MSG_NOT_NUMERIC), $input) ); return true; } diff --git a/library/Respect/Validation/Rules/StringLength.php b/library/Respect/Validation/Rules/StringLength.php index 6fbe873d..5a394b0c 100644 --- a/library/Respect/Validation/Rules/StringLength.php +++ b/library/Respect/Validation/Rules/StringLength.php @@ -14,7 +14,7 @@ class StringLength extends AbstractRule implements Validatable const MSG_LENGTH_MIN = 'StringLength_1'; const MSG_LENGTH_MAX = 'StringLength_2'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_LENGTH_MIN => '%s does not have at least %s characters', self::MSG_LENGTH_MAX => '%s exceeds the maximum of %s characters' ); @@ -60,11 +60,13 @@ class StringLength extends AbstractRule implements Validatable { if (!$this->validateMin($input)) throw new StringLengthException( - sprintf($this->getMessage(self::MSG_LENGTH_MIN), $input, $this->min) + sprintf($this->getMessageTemplate(self::MSG_LENGTH_MIN), $input, + $this->min) ); if (!$this->validateMax($input)) throw new StringLengthException( - sprintf($this->getMessage(self::MSG_LENGTH_MAX), $input, $this->max) + sprintf($this->getMessageTemplate(self::MSG_LENGTH_MAX), $input, + $this->max) ); return true; } diff --git a/library/Respect/Validation/Rules/StringNotEmpty.php b/library/Respect/Validation/Rules/StringNotEmpty.php index 7a780811..792a4dce 100644 --- a/library/Respect/Validation/Rules/StringNotEmpty.php +++ b/library/Respect/Validation/Rules/StringNotEmpty.php @@ -10,7 +10,7 @@ class StringNotEmpty extends AbstractRule implements Validatable { const MSG_EMPTY_STRING = 'StringNotEmpty_1'; - protected $messages = array( + protected $messageTemplates = array( self::MSG_EMPTY_STRING => 'You provided an empty string' ); @@ -24,7 +24,7 @@ class StringNotEmpty extends AbstractRule implements Validatable { if (!$this->validate($input)) throw new EmptyStringException( - $this->getMessage(self::MSG_EMPTY_STRING) + $this->getMessageTemplate(self::MSG_EMPTY_STRING) ); return true; } diff --git a/library/Respect/Validation/Validatable.php b/library/Respect/Validation/Validatable.php index 031615ca..e6508c0d 100644 --- a/library/Respect/Validation/Validatable.php +++ b/library/Respect/Validation/Validatable.php @@ -9,7 +9,11 @@ interface Validatable public function validate($input); - public function getMessages(); + public function getMessageTemplates(); - public function setMessages(array $messages); + public function setMessageTemplates(array $templates); + + public function setMessageTemplate($code, $templage); + + public function getMessageTemplate($code); } \ No newline at end of file diff --git a/tests/library/Respect/Validation/Rules/SfTest.php b/tests/library/Respect/Validation/Rules/SfTest.php index 00ee94bf..6aee9cf7 100644 --- a/tests/library/Respect/Validation/Rules/SfTest.php +++ b/tests/library/Respect/Validation/Rules/SfTest.php @@ -5,10 +5,9 @@ namespace Respect\Validation\Rules; class SfTest extends \PHPUnit_Framework_TestCase { - public function testParamsOk() { - $v = new Sf('minLength', array('limit'=>3)); + $v = new Sf('minLength', array('limit' => 3)); $this->assertTrue($v->assert('wp2oiur')); } @@ -17,7 +16,7 @@ class SfTest extends \PHPUnit_Framework_TestCase */ public function testParamsNot() { - $v = new Sf('minLength', array('limit'=>3)); + $v = new Sf('minLength', array('limit' => 3)); $this->assertTrue($v->assert('a')); } diff --git a/tests/library/Respect/Validation/ValidatorTestCase.php b/tests/library/Respect/Validation/ValidatorTestCase.php index 9cd77f81..f083998d 100644 --- a/tests/library/Respect/Validation/ValidatorTestCase.php +++ b/tests/library/Respect/Validation/ValidatorTestCase.php @@ -31,18 +31,19 @@ abstract class ValidatorTestCase extends \PHPUnit_Framework_TestCase $validator->shouldReceive('validate')->andReturn(true); $validator->shouldReceive('assert')->andReturn(true); } - $validator->shouldReceive('getMessages')->andReturn( + $validator->shouldReceive('getMessageTemplates')->andReturn( $messages ); $className = 'Respect\Validation\Rules\\' . $name; if (!class_exists($className, false)) { eval(" namespace Respect\Validation\Rules; - class $name implements \Respect\Validation\Validatable { + class $name + extends \Respect\Validation\Rules\AbstractRule + implements \Respect\Validation\Validatable { public function assert(\$input) {} public function validate(\$input) {} - public function setMessages(array \$messages) {} - public function getMessages() { + public function getMessageTemplates() { return " . var_export($messages, true) . "; }