Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
44 changes: 27 additions & 17 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/MessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down