From 6a14b119c796dd05af10f2f0d67264ec29374109 Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Mon, 11 Dec 2017 12:23:14 +0000 Subject: [PATCH] Improved redefinition of messages --- src/Config.php | 8 +++---- src/Messages.php | 44 +++++++++++++++++++++++-------------- tests/unit/MessagesTest.php | 7 ++++++ 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/Config.php b/src/Config.php index 8f54ff7..61c2ea5 100644 --- a/src/Config.php +++ b/src/Config.php @@ -41,13 +41,13 @@ public function setTemplatePack(string $template_pack) } /** - * Set messages class. + * Redefine default messages. * - * @param string Class name of Renderer. + * @param array Messages array. */ - public function setMessages(string $messages_class) + public function setMessages(array $messages) { - $this->messages_class = $messages_class; + $this->messages_class::setMessages($messages); } /** diff --git a/src/Messages.php b/src/Messages.php index 12c93e1..ea17263 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -8,22 +8,32 @@ class Messages { - const REQUIRED = 'This field is required.'; - const INVALID_CHOICE = 'Select a valid choice. "{choice}" is not one of the available choices.'; - const INVALID_LIST = 'Enter a list of values.'; - const INVALID_DATE = 'Enter a valid date.'; - const INVALID_DATETIME = 'Enter a valid date/time.'; - const INVALID_NUMBER = 'Enter a whole number.'; - const INVALID_EMAIL = 'Enter a valid email address.'; - const INVALID_URL = 'Enter a valid URL.'; - const INVALID_FILE = 'Invalid file submitted.'; - const EMPTY_FILE = 'The submitted file is empty.'; - const INVALID_FILE_MAX_SIZE = 'Ensure the file has at most {limit} bytes (it has {value} bytes).'; - const INVALID_FILE_TYPE = 'Ensure the file is one of "{valid_types}" types (it has {type}).'; - const INVALID_MAX_LENGTH = 'Ensure this value has at most {limit} character (it has {value}).'; - const INVALID_MAX_VALUE = 'Ensure this value is less than or equal to {limit}.'; - const INVALID_MIN_LENGTH = 'Ensure this value has at least {limit} character (it has {value}).'; - const INVALID_MIN_VALUE = 'Ensure this value is greater than or equal to {limit}.'; + /** + * @var array Default messages + */ + private static $messages = array( + "REQUIRED" => 'This field is required.', + "INVALID_CHOICE" => 'Select a valid choice. "{choice}" is not one of the available choices.', + "INVALID_LIST" => 'Enter a list of values.', + "INVALID_DATE" => 'Enter a valid date.', + "INVALID_DATETIME" => 'Enter a valid date/time.', + "INVALID_NUMBER" => 'Enter a whole number.', + "INVALID_EMAIL" => 'Enter a valid email address.', + "INVALID_URL" => 'Enter a valid URL.', + "INVALID_FILE" => 'Invalid file submitted.', + "EMPTY_FILE" => 'The submitted file is empty.', + "INVALID_FILE_MAX_SIZE" => 'Ensure the file has at most {limit} bytes (it has {value} bytes).', + "INVALID_FILE_TYPE" => 'Ensure the file is one of "{valid_types}" types (it has {type}).', + "INVALID_MAX_LENGTH" => 'Ensure this value has at most {limit} character (it has {value}).', + "INVALID_MAX_VALUE" => 'Ensure this value is less than or equal to {limit}.', + "INVALID_MIN_LENGTH" => 'Ensure this value has at least {limit} character (it has {value}).', + "INVALID_MIN_VALUE" => 'Ensure this value is greater than or equal to {limit}.' + ); + + public static function setMessages(array $messages) + { + self::$messages = array_merge(self::$messages, $messages); + } /** * Format message witg context. @@ -35,7 +45,7 @@ class Messages */ public static function format(string $id, array $context = null) { - $message = defined("static::$id") ? constant("static::$id") : $id; + $message = array_key_exists($id, self::$messages) ? self::$messages[$id] : $id; if (!is_null($context)) { $message = Formatter::format($message, $context); diff --git a/tests/unit/MessagesTest.php b/tests/unit/MessagesTest.php index 2511a31..773e457 100644 --- a/tests/unit/MessagesTest.php +++ b/tests/unit/MessagesTest.php @@ -7,6 +7,13 @@ class MessagesTest extends TestCase { + public function testSetMessages() + { + Messages::setMessages(["REQUIRED2" => "Required"]); + + $this->assertEquals("Required", Messages::format("REQUIRED2")); + } + public function testFormat() { $this->assertEquals("String", Messages::format("String"));