Skip to content

Commit faa1454

Browse files
authored
Merge pull request #11 from dipcode-software/feat/messages
Improved redefinition of messages
2 parents 68b2360 + 6a14b11 commit faa1454

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/Config.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function setTemplatePack(string $template_pack)
4141
}
4242

4343
/**
44-
* Set messages class.
44+
* Redefine default messages.
4545
*
46-
* @param string Class name of Renderer.
46+
* @param array Messages array.
4747
*/
48-
public function setMessages(string $messages_class)
48+
public function setMessages(array $messages)
4949
{
50-
$this->messages_class = $messages_class;
50+
$this->messages_class::setMessages($messages);
5151
}
5252

5353
/**

src/Messages.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@
88

99
class Messages
1010
{
11-
const REQUIRED = 'This field is required.';
12-
const INVALID_CHOICE = 'Select a valid choice. "{choice}" is not one of the available choices.';
13-
const INVALID_LIST = 'Enter a list of values.';
14-
const INVALID_DATE = 'Enter a valid date.';
15-
const INVALID_DATETIME = 'Enter a valid date/time.';
16-
const INVALID_NUMBER = 'Enter a whole number.';
17-
const INVALID_EMAIL = 'Enter a valid email address.';
18-
const INVALID_URL = 'Enter a valid URL.';
19-
const INVALID_FILE = 'Invalid file submitted.';
20-
const EMPTY_FILE = 'The submitted file is empty.';
21-
const INVALID_FILE_MAX_SIZE = 'Ensure the file has at most {limit} bytes (it has {value} bytes).';
22-
const INVALID_FILE_TYPE = 'Ensure the file is one of "{valid_types}" types (it has {type}).';
23-
const INVALID_MAX_LENGTH = 'Ensure this value has at most {limit} character (it has {value}).';
24-
const INVALID_MAX_VALUE = 'Ensure this value is less than or equal to {limit}.';
25-
const INVALID_MIN_LENGTH = 'Ensure this value has at least {limit} character (it has {value}).';
26-
const INVALID_MIN_VALUE = 'Ensure this value is greater than or equal to {limit}.';
11+
/**
12+
* @var array Default messages
13+
*/
14+
private static $messages = array(
15+
"REQUIRED" => 'This field is required.',
16+
"INVALID_CHOICE" => 'Select a valid choice. "{choice}" is not one of the available choices.',
17+
"INVALID_LIST" => 'Enter a list of values.',
18+
"INVALID_DATE" => 'Enter a valid date.',
19+
"INVALID_DATETIME" => 'Enter a valid date/time.',
20+
"INVALID_NUMBER" => 'Enter a whole number.',
21+
"INVALID_EMAIL" => 'Enter a valid email address.',
22+
"INVALID_URL" => 'Enter a valid URL.',
23+
"INVALID_FILE" => 'Invalid file submitted.',
24+
"EMPTY_FILE" => 'The submitted file is empty.',
25+
"INVALID_FILE_MAX_SIZE" => 'Ensure the file has at most {limit} bytes (it has {value} bytes).',
26+
"INVALID_FILE_TYPE" => 'Ensure the file is one of "{valid_types}" types (it has {type}).',
27+
"INVALID_MAX_LENGTH" => 'Ensure this value has at most {limit} character (it has {value}).',
28+
"INVALID_MAX_VALUE" => 'Ensure this value is less than or equal to {limit}.',
29+
"INVALID_MIN_LENGTH" => 'Ensure this value has at least {limit} character (it has {value}).',
30+
"INVALID_MIN_VALUE" => 'Ensure this value is greater than or equal to {limit}.'
31+
);
32+
33+
public static function setMessages(array $messages)
34+
{
35+
self::$messages = array_merge(self::$messages, $messages);
36+
}
2737

2838
/**
2939
* Format message witg context.
@@ -35,7 +45,7 @@ class Messages
3545
*/
3646
public static function format(string $id, array $context = null)
3747
{
38-
$message = defined("static::$id") ? constant("static::$id") : $id;
48+
$message = array_key_exists($id, self::$messages) ? self::$messages[$id] : $id;
3949

4050
if (!is_null($context)) {
4151
$message = Formatter::format($message, $context);

tests/unit/MessagesTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
class MessagesTest extends TestCase
99
{
10+
public function testSetMessages()
11+
{
12+
Messages::setMessages(["REQUIRED2" => "Required"]);
13+
14+
$this->assertEquals("Required", Messages::format("REQUIRED2"));
15+
}
16+
1017
public function testFormat()
1118
{
1219
$this->assertEquals("String", Messages::format("String"));

0 commit comments

Comments
 (0)