1 | <?php |
||
8 | class Engine |
||
9 | { |
||
10 | /** |
||
11 | * @var string - main session key for Flash messages. |
||
12 | */ |
||
13 | private $key = 'flash_messages'; |
||
14 | |||
15 | private $types = [ |
||
16 | 'error', |
||
17 | 'warning', |
||
18 | 'info', |
||
19 | 'success', |
||
20 | ]; |
||
21 | |||
22 | private $template; |
||
23 | |||
24 | /** |
||
25 | * Creates flash container from session. |
||
26 | * |
||
27 | * @param TemplateInterface|null $template |
||
28 | */ |
||
29 | 6 | public function __construct(TemplateInterface $template) |
|
37 | |||
38 | /** |
||
39 | * Base method for adding messages to flash. |
||
40 | * |
||
41 | * @param string $message - message text |
||
42 | * @param string $type - message type: success, info, warning, error |
||
43 | * |
||
44 | * @return Engine $this |
||
45 | */ |
||
46 | 48 | public function message($message = '', $type = 'info') |
|
58 | |||
59 | /** |
||
60 | * Add message to $_SESSION. |
||
61 | * |
||
62 | * @param string $message - message text |
||
63 | * @param string $type - message type: success, info, warning, error |
||
64 | * |
||
65 | * @return Engine $this |
||
66 | */ |
||
67 | 48 | protected function addMessage($message = '', $type = 'info') |
|
83 | |||
84 | /** |
||
85 | * Returns Bootstrap ready HTML for Engine messages. |
||
86 | * |
||
87 | * @param string $type - message type: success, info, warning, error |
||
88 | * |
||
89 | * @return string - HTML with flash messages |
||
90 | */ |
||
91 | 44 | public function display($type = null) |
|
111 | |||
112 | /** |
||
113 | * Returns if there are any messages in container. |
||
114 | * |
||
115 | * @param string $type - message type: success, info, warning, error |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | 18 | public function hasMessages($type = null) |
|
133 | |||
134 | /** |
||
135 | * Clears messages from session store. |
||
136 | * |
||
137 | * @param string $type - message type: success, info, warning, error |
||
138 | * |
||
139 | * @return Engine $this |
||
140 | */ |
||
141 | 42 | public function clear($type = null) |
|
151 | |||
152 | /** |
||
153 | * Builds messages for a single type. |
||
154 | * |
||
155 | * @param array $flashes - array of messages to show |
||
156 | * @param string $type - message type: success, info, warning, error |
||
157 | * |
||
158 | * @return string - HTML with flash messages |
||
159 | */ |
||
160 | 42 | protected function buildMessages(array $flashes, $type) |
|
169 | |||
170 | /** |
||
171 | * If requested as string will HTML will be returned. |
||
172 | * |
||
173 | * @return string - HTML with flash messages |
||
174 | */ |
||
175 | 4 | public function __toString() |
|
179 | |||
180 | /** |
||
181 | * Shortcut for error message. |
||
182 | * |
||
183 | * @param $message - message text |
||
184 | * |
||
185 | * @return Engine $this |
||
186 | */ |
||
187 | 2 | public function error($message) |
|
191 | |||
192 | /** |
||
193 | * Shortcut for warning message. |
||
194 | * |
||
195 | * @param $message - message text |
||
196 | * |
||
197 | * @return Engine $this |
||
198 | */ |
||
199 | 2 | public function warning($message) |
|
203 | |||
204 | /** |
||
205 | * Shortcut for info message. |
||
206 | * |
||
207 | * @param $message - message text |
||
208 | * |
||
209 | * @return Engine $this |
||
210 | */ |
||
211 | 16 | public function info($message) |
|
215 | |||
216 | /** |
||
217 | * Shortcut for success message. |
||
218 | * |
||
219 | * @param $message - message text |
||
220 | * |
||
221 | * @return Engine $this |
||
222 | */ |
||
223 | 2 | public function success($message) |
|
227 | |||
228 | /** |
||
229 | * Setter for $template. |
||
230 | * |
||
231 | * @param TemplateInterface $template |
||
232 | * |
||
233 | * @return Engine $this |
||
234 | */ |
||
235 | 10 | public function setTemplate(TemplateInterface $template) |
|
241 | |||
242 | /** |
||
243 | * Getter for $template. |
||
244 | * |
||
245 | * @return TemplateInterface |
||
246 | */ |
||
247 | 2 | public function getTemplate() |
|
251 | } |
||
252 |