8
8
9
9
class Messages
10
10
{
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
+ }
27
37
28
38
/**
29
39
* Format message witg context.
@@ -35,7 +45,7 @@ class Messages
35
45
*/
36
46
public static function format (string $ id , array $ context = null )
37
47
{
38
- $ message = defined ( " static ::$ id " ) ? constant ( " static ::$ id " ) : $ id ;
48
+ $ message = array_key_exists ( $ id , self ::$ messages ) ? self ::$ messages [ $ id ] : $ id ;
39
49
40
50
if (!is_null ($ context )) {
41
51
$ message = Formatter::format ($ message , $ context );
0 commit comments