1 | <?php |
||
15 | class Messages { |
||
16 | |||
17 | const MSG_EMPTY = 0; |
||
18 | const MSG_INFO = 1; |
||
19 | const MSG_SUCCESS = 2; |
||
20 | const MSG_WARNING = 3; |
||
21 | const MSG_ERROR = 4; |
||
22 | |||
23 | /** |
||
24 | * Messages list. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $messages = []; |
||
29 | |||
30 | /** |
||
31 | * Push new message to the global messages list. |
||
32 | * |
||
33 | * @param int $type Priority type of the message. |
||
34 | * @param string $text Text of the message. |
||
35 | * @param string $code Code of the message in the message list (optional). |
||
36 | * |
||
37 | * @throws \Exception If wrong priority message type provided. |
||
38 | */ |
||
39 | public static function pushMessage($type, $text, $code = null) { |
||
53 | |||
54 | /** |
||
55 | * Push new message to the global messages list. |
||
56 | * |
||
57 | * @param int $type Priority type of the message. |
||
58 | * @param string $text Text of the message. |
||
59 | * @param string $code Code of the message in the message list (optional). |
||
60 | * |
||
61 | * @throws \Exception If wrong priority message type provided. |
||
62 | */ |
||
63 | public static function addMessage($type, $text, $code = null) { |
||
66 | |||
67 | /** |
||
68 | * Returns message by it's code from global messages list. |
||
69 | * |
||
70 | * @param string $code Code of the message in the message list. |
||
71 | * |
||
72 | * @return __MSG Message object. |
||
73 | * |
||
74 | * @throws \Exception If message with such code is not exists. |
||
75 | */ |
||
76 | public static function getMessage($code) { |
||
83 | |||
84 | /** |
||
85 | * Returns message text (content) silently. |
||
86 | * |
||
87 | * @param mixed $code Key of the message. |
||
88 | * |
||
89 | * @return string Message text or empty string if message doesn't exist. |
||
90 | */ |
||
91 | public static function get($code) { |
||
103 | |||
104 | /** |
||
105 | * Removes message with some code from global messages list. |
||
106 | * |
||
107 | * @param string $code Code of the message in the message list. |
||
108 | */ |
||
109 | public static function popMessages($code) { |
||
114 | |||
115 | /** |
||
116 | * Removes message with some code from global messages list. |
||
117 | * |
||
118 | * @param string $code Code of the message in the message list. |
||
119 | */ |
||
120 | public static function removeMessages($code) { |
||
123 | |||
124 | /** |
||
125 | * Sort messages list with most important messages priority but save order if |
||
126 | * priority the same. |
||
127 | */ |
||
128 | public static function reorderMessages() { |
||
133 | |||
134 | /** |
||
135 | * Sort messages list with most important messages priority but save order if |
||
136 | * priority the same. |
||
137 | */ |
||
138 | public static function sortMessages() { |
||
141 | |||
142 | /** |
||
143 | * Returns reordered by priority messages list. |
||
144 | * |
||
145 | * @return array<_MSG> List with reordered by prioroty messages. |
||
146 | */ |
||
147 | public static function getMessages() { |
||
152 | |||
153 | } |
||
154 | |||
169 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.