From: Georg Ringer Date: Fri, 13 Apr 2012 20:28:27 +0000 (+0200) Subject: [TASK] Backport changes to Extbase_Error_Message X-Git-Tag: TYPO3_6-2-0alpha1~21^2~280^2 X-Git-Url: http://git.typo3.org/Packages/TYPO3.CMS.git/commitdiff_plain/ed9c34e046e2f58af8a9cd5fed0d537ccd47de8d [TASK] Backport changes to Extbase_Error_Message In FLOW3 the Message class contains the property arguments which is needed to be able later to translate error messages. Change-Id: I0b83615d514178f1c9c6c2c90915a0f133244884 Resolves: #35255 Releases: 6.0 --- diff --git a/typo3/sysext/extbase/Classes/Error/Message.php b/typo3/sysext/extbase/Classes/Error/Message.php index d26c7d069de2..915d8af2c0c2 100644 --- a/typo3/sysext/extbase/Classes/Error/Message.php +++ b/typo3/sysext/extbase/Classes/Error/Message.php @@ -43,23 +43,37 @@ class Tx_Extbase_Error_Message { */ protected $code; + /** + * The message arguments. Will be replaced in the message body. + * @var array + */ + protected $arguments = array(); + + /** + * An optional title for the message (used eg. in flashMessages). + * @var string + */ + protected $title = ''; + /** * Constructs this error * * @param string $message An english error message which is used if no other error message can be resolved * @param integer $code A unique error code - * @author Robert Lemke + * @param array $arguments Array of arguments to be replaced in message + * @param string $title optional title for the message * @api */ - public function __construct($message, $code) { + public function __construct($message, $code, array $arguments = array(), $title = '') { $this->message = $message; $this->code = $code; + $this->arguments = $arguments; + $this->title = $title; } /** * Returns the error message * @return string The error message - * @author Andreas Förthner * @api */ public function getMessage() { @@ -69,22 +83,54 @@ class Tx_Extbase_Error_Message { /** * Returns the error code * @return string The error code - * @author Andreas Förthner * @api */ public function getCode() { return $this->code; } + /** + * Get arguments + * + * @return array + * @api + */ + public function getArguments() { + return $this->arguments; + } + + /** + * Get title + * + * @return string + * @api + */ + public function getTitle() { + return $this->title; + } + + /** + * Return the rendered message + * + * @return string + * @api + */ + public function render() { + if (!empty($this->arguments)) { + return vsprintf($this->message, $this->arguments); + } else { + return $this->message; + } + } + /** * Converts this error into a string * * @return string - * @author Robert Lemke * @api */ public function __toString() { - return $this->message . ' (#' . $this->code . ')'; + return $this->render(); } } diff --git a/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php b/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php index 3a08d59ddef9..58c6910a1dc1 100644 --- a/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php +++ b/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php @@ -461,7 +461,7 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll $message = 'An error occurred while trying to call ' . get_class($this) . '->' . $this->actionMethodName . '().' . PHP_EOL; foreach ($this->arguments->getValidationResults()->getFlattenedErrors() as $propertyPath => $errors) { foreach ($errors as $error) { - $message .= 'Error for ' . $propertyPath . ': ' . $error->getMessage() . PHP_EOL; + $message .= 'Error for ' . $propertyPath . ': ' . $error->render() . PHP_EOL; } } @@ -553,4 +553,4 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll } } -?> \ No newline at end of file +?> diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/AbstractValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/AbstractValidator.php index 6f7b5f7d06c7..62e35b7b057d 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/AbstractValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/AbstractValidator.php @@ -108,16 +108,18 @@ abstract class Tx_Extbase_Validation_Validator_AbstractValidator implements Tx_E * * @param string $message The error message * @param integer $code The error code (a unix timestamp) + * @param array $arguments Arguments to be replaced in message + * @param string $title title of the error * @return void */ - protected function addError($message, $code) { + protected function addError($message, $code, array $arguments = array(), $title = '') { if ($this->result !== NULL) { // backwards compatibility before Extbase 1.4.0: we cannot expect the "result" object to be there. - $this->result->addError(new Tx_Extbase_Validation_Error($message, $code)); + $this->result->addError(new Tx_Extbase_Validation_Error($message, $code, $arguments, $title)); } // the following is @deprecated since Extbase 1.4.0: - $this->errors[] = new Tx_Extbase_Validation_Error($message, $code); + $this->errors[] = new Tx_Extbase_Validation_Error($message, $code, $arguments, $title); } } -?> \ No newline at end of file +?> diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/DateTimeValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/DateTimeValidator.php index 25c970ecd48a..5bb6f1261b73 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/DateTimeValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/DateTimeValidator.php @@ -5,7 +5,7 @@ * (c) 2009 Jochen Rau * All rights reserved * -* This class is a backport of the corresponding class of FLOW3. +* This class is a backport of the corresponding class of FLOW3. * All credits go to the v5 team. * * This script is part of the TYPO3 project. The TYPO3 project is @@ -46,7 +46,7 @@ class Tx_Extbase_Validation_Validator_DateTimeValidator extends Tx_Extbase_Valid public function isValid($value) { $this->errors = array(); if ($value instanceof DateTime) return TRUE; - $this->addError('The given subject was not a valid DateTime. Got: "' .gettype($value) . '"', 1238087674); + $this->addError('The given subject was not a valid DateTime. Got: "%1$d"', 1238087674, array(gettype($value))); return FALSE; } } diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/GenericObjectValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/GenericObjectValidator.php index 2e8e37798a03..e9c93838501a 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/GenericObjectValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/GenericObjectValidator.php @@ -68,7 +68,7 @@ class Tx_Extbase_Validation_Validator_GenericObjectValidator extends Tx_Extbase_ } if (!is_object($object)) { - $messages->addError(new Tx_Extbase_Validation_Error('Object expected, ' . gettype($object) . ' given.', 1241099149)); + $messages->addError(new Tx_Extbase_Validation_Error('Object expected, "%1$d" given.', 1241099149, array(gettype($object)))); return $messages; } @@ -213,4 +213,4 @@ class Tx_Extbase_Validation_Validator_GenericObjectValidator extends Tx_Extbase_ } } -?> \ No newline at end of file +?> diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/NumberRangeValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/NumberRangeValidator.php index 47a3c564634a..7cc0d5202438 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/NumberRangeValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/NumberRangeValidator.php @@ -66,7 +66,7 @@ class Tx_Extbase_Validation_Validator_NumberRangeValidator extends Tx_Extbase_Va } if ($value >= $startRange && $value <= $endRange) return TRUE; - $this->addError('The given subject was not in the valid range (' . $startRange . ' - ' . $endRange . ').', 1221561046); + $this->addError('The given subject was not in the valid range (%1$d - %2$d).', 1221561046, array($startRange, $endRange)); return FALSE; } } diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/RegularExpressionValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/RegularExpressionValidator.php index 440be30edb81..fc6acda9911c 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/RegularExpressionValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/RegularExpressionValidator.php @@ -5,7 +5,7 @@ * (c) 2009 Jochen Rau * All rights reserved * -* This class is a backport of the corresponding class of FLOW3. +* This class is a backport of the corresponding class of FLOW3. * All credits go to the v5 team. * * This script is part of the TYPO3 project. The TYPO3 project is @@ -57,7 +57,7 @@ class Tx_Extbase_Validation_Validator_RegularExpressionValidator extends Tx_Extb return FALSE; } if ($result === FALSE) { - $this->addError('The regular expression "' . $this->options['regularExpression'] . '" contained an error.', 1221565131); + $this->addError('The regular expression "%1$d" contained an error.', 1221565131, array($this->options['regularExpression'])); return FALSE; } return TRUE; diff --git a/typo3/sysext/extbase/Classes/Validation/Validator/StringLengthValidator.php b/typo3/sysext/extbase/Classes/Validation/Validator/StringLengthValidator.php index 33149b4fdc9e..50766dc5b015 100644 --- a/typo3/sysext/extbase/Classes/Validation/Validator/StringLengthValidator.php +++ b/typo3/sysext/extbase/Classes/Validation/Validator/StringLengthValidator.php @@ -5,7 +5,7 @@ * (c) 2009 Jochen Rau * All rights reserved * -* This class is a backport of the corresponding class of FLOW3. +* This class is a backport of the corresponding class of FLOW3. * All credits go to the v5 team. * * This script is part of the TYPO3 project. The TYPO3 project is @@ -62,11 +62,11 @@ class Tx_Extbase_Validation_Validator_StringLengthValidator extends Tx_Extbase_V if ($isValid === FALSE) { if (isset($this->options['minimum']) && isset($this->options['maximum'])) { - $this->addError('The length of the given string was not between ' . $this->options['minimum'] . ' and ' . $this->options['maximum'] . ' characters.', 1238108067); + $this->addError('The length of the given string was not between %1$d and %2$d characters.', 1238108067, array($this->options['minimum'], $this->options['maximum'])); } elseif (isset($this->options['minimum'])) { - $this->addError('The length of the given string less than ' . $this->options['minimum'] . ' characters.', 1238108068); + $this->addError('The length of the given string less than %1$d characters.', 1238108068, array($this->options['minimum'])); } else { - $this->addError('The length of the given string exceeded ' . $this->options['maximum'] . ' characters.', 1238108069); + $this->addError('The length of the given string exceeded %1$d characters.', 1238108069, array($this->options['maximum'])); } }