1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tamtamchik\SimpleFlash\Core; |
4
|
|
|
|
5
|
|
|
use Tamtamchik\SimpleFlash\Exceptions\FlashTemplateNotFoundException; |
6
|
|
|
use Tamtamchik\SimpleFlash\TemplateFactory; |
7
|
|
|
use Tamtamchik\SimpleFlash\TemplateInterface; |
8
|
|
|
use Tamtamchik\SimpleFlash\Templates; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Engine. |
12
|
|
|
*/ |
13
|
|
|
class Engine extends MessageManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Creates flash container from session. |
17
|
|
|
* |
18
|
|
|
* @param TemplateInterface $template |
19
|
|
|
*/ |
20
|
|
|
public function __construct(TemplateInterface $template) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($template); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* If requested as string will HTML will be returned. |
27
|
|
|
* |
28
|
|
|
* @return string - HTML with flash messages |
29
|
|
|
* |
30
|
|
|
* @throws FlashTemplateNotFoundException |
31
|
|
|
*/ |
32
|
|
|
public function __toString() |
33
|
|
|
{ |
34
|
|
|
return $this->display(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns Bootstrap ready HTML for Engine messages. |
39
|
|
|
* |
40
|
|
|
* @param string|null $type - message type: success, info, warning, error |
41
|
|
|
* @param string|null $template - template name from Templates class |
42
|
|
|
* |
43
|
|
|
* @return string - HTML with flash messages |
44
|
|
|
* |
45
|
|
|
* @throws FlashTemplateNotFoundException |
46
|
|
|
*/ |
47
|
|
|
public function display(string $type = null, string $template = null): string |
48
|
|
|
{ |
49
|
|
|
$result = ''; |
50
|
|
|
$session = $this->getSession(); |
51
|
|
|
|
52
|
|
|
$isEmptySession = empty($session); |
53
|
|
|
$isTypeNotInSession = ! is_null($type) && ! array_key_exists($type, $session); |
54
|
|
|
$isTypeNotInTypes = ! is_null($type) && ! $this->_hasMessageType($type); |
55
|
|
|
|
56
|
|
|
if ($isEmptySession || $isTypeNotInSession || $isTypeNotInTypes) { |
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( ! is_null($template)) { |
61
|
|
|
$this->setTemplate(TemplateFactory::create($template)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_null($type)) { |
65
|
|
|
foreach ($session as $messageType => $messages) { |
66
|
|
|
$result .= $this->_compileMessage($messages, $messageType); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
$result .= $this->_compileMessage($session[$type], $type); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->clear($type); |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns if there are any messages in container. |
79
|
|
|
* |
80
|
|
|
* @param string|null $type - message type: success, info, warning, error |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function some(string $type = null): bool |
85
|
|
|
{ |
86
|
|
|
return $this->_hasMessage($type); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Clears messages from session store. |
91
|
|
|
* |
92
|
|
|
* @param string|null $type - message type: success, info, warning, error |
93
|
|
|
* |
94
|
|
|
* @return Engine $this |
95
|
|
|
*/ |
96
|
|
|
public function clear(string $type = null): Engine |
97
|
|
|
{ |
98
|
|
|
$this->_clearMessage($type); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Base method for adding messages to flash. |
105
|
|
|
* |
106
|
|
|
* @param string|string[] $message - message text |
107
|
|
|
* @param string $type - message type: success, info, warning, error |
108
|
|
|
* |
109
|
|
|
* @return Engine $this |
110
|
|
|
*/ |
111
|
|
|
public function message($message = '', string $type = 'info'): Engine |
112
|
|
|
{ |
113
|
|
|
if (is_array($message)) { |
114
|
|
|
foreach ($message as $issue) { |
115
|
|
|
$this->_addMessage($issue, $type); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
$this->_addMessage($message, $type); |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Getter for $template. |
126
|
|
|
* |
127
|
|
|
* @return TemplateInterface |
128
|
|
|
*/ |
129
|
|
|
public |
130
|
|
|
function getTemplate(): ?TemplateInterface |
131
|
|
|
{ |
132
|
|
|
return $this->_getTemplate(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Setter for $template. |
137
|
|
|
* |
138
|
|
|
* @param TemplateInterface $template |
139
|
|
|
* |
140
|
|
|
* @return Engine $this |
141
|
|
|
*/ |
142
|
|
|
public |
143
|
|
|
function setTemplate(TemplateInterface $template): Engine |
144
|
|
|
{ |
145
|
|
|
$this->_setTemplate($template); |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Shortcut for error message. |
152
|
|
|
* |
153
|
|
|
* @param string|string[] $message - message text |
154
|
|
|
* |
155
|
|
|
* @return Engine $this |
156
|
|
|
*/ |
157
|
|
|
public function error($message): Engine |
158
|
|
|
{ |
159
|
|
|
return $this->message($message, 'error'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Shortcut for warning message. |
164
|
|
|
* |
165
|
|
|
* @param string|string[] $message - message text |
166
|
|
|
* |
167
|
|
|
* @return Engine $this |
168
|
|
|
*/ |
169
|
|
|
public function warning($message): Engine |
170
|
|
|
{ |
171
|
|
|
return $this->message($message, 'warning'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Shortcut for info message. |
176
|
|
|
* |
177
|
|
|
* @param string|string[] $message - message text |
178
|
|
|
* |
179
|
|
|
* @return Engine $this |
180
|
|
|
*/ |
181
|
|
|
public function info($message): Engine |
182
|
|
|
{ |
183
|
|
|
return $this->message($message); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Shortcut for success message. |
188
|
|
|
* |
189
|
|
|
* @param string|string[] $message - message text |
190
|
|
|
* |
191
|
|
|
* @return Engine $this |
192
|
|
|
*/ |
193
|
|
|
public function success($message): Engine |
194
|
|
|
{ |
195
|
|
|
return $this->message($message, 'success'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns Bootstrap ready HTML for messages. |
200
|
|
|
* |
201
|
|
|
* @param string|null $type - message type: success, info, warning, error |
202
|
|
|
* |
203
|
|
|
* @throws FlashTemplateNotFoundException |
204
|
|
|
*/ |
205
|
|
|
public function displayBootstrap(string $type = null): string |
206
|
|
|
{ |
207
|
|
|
return $this->display($type, Templates::BOOTSTRAP); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns Foundation ready HTML for messages. |
212
|
|
|
* |
213
|
|
|
* @param string|null $type - message type: success, info, warning, error |
214
|
|
|
* |
215
|
|
|
* @throws FlashTemplateNotFoundException |
216
|
|
|
*/ |
217
|
|
|
public function displayFoundation(string $type = null): string |
218
|
|
|
{ |
219
|
|
|
return $this->display($type, Templates::FOUNDATION); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns Bulma ready HTML for messages. |
224
|
|
|
* |
225
|
|
|
* @param string|null $type - message type: success, info, warning, error |
226
|
|
|
* |
227
|
|
|
* @throws FlashTemplateNotFoundException |
228
|
|
|
*/ |
229
|
|
|
public function displayBulma(string $type = null): string |
230
|
|
|
{ |
231
|
|
|
return $this->display($type, Templates::BULMA); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns Materialize ready HTML for messages. |
236
|
|
|
* |
237
|
|
|
* @param string|null $type - message type: success, info, warning, error |
238
|
|
|
* |
239
|
|
|
* @throws FlashTemplateNotFoundException |
240
|
|
|
*/ |
241
|
|
|
public function displayMaterialize(string $type = null): string |
242
|
|
|
{ |
243
|
|
|
return $this->display($type, Templates::MATERIALIZE); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns Tailwind ready HTML for messages. |
248
|
|
|
* |
249
|
|
|
* @param string|null $type - message type: success, info, warning, error |
250
|
|
|
* |
251
|
|
|
* @throws FlashTemplateNotFoundException |
252
|
|
|
*/ |
253
|
|
|
public function displayTailwind(string $type = null): string |
254
|
|
|
{ |
255
|
|
|
return $this->display($type, Templates::TAILWIND); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns Primer ready HTML for messages. |
260
|
|
|
* |
261
|
|
|
* @param string|null $type - message type: success, info, warning, error |
262
|
|
|
* |
263
|
|
|
* @throws FlashTemplateNotFoundException |
264
|
|
|
*/ |
265
|
|
|
public function displayPrimer(string $type = null): string |
266
|
|
|
{ |
267
|
|
|
return $this->display($type, Templates::PRIMER); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns UiKit ready HTML for messages. |
272
|
|
|
* |
273
|
|
|
* @param string|null $type - message type: success, info, warning, error |
274
|
|
|
* |
275
|
|
|
* @throws FlashTemplateNotFoundException |
276
|
|
|
*/ |
277
|
|
|
public function displayUiKit(string $type = null): string |
278
|
|
|
{ |
279
|
|
|
return $this->display($type, Templates::UIKIT); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns Semantic ready HTML for messages. |
284
|
|
|
* |
285
|
|
|
* @param string|null $type - message type: success, info, warning, error |
286
|
|
|
* |
287
|
|
|
* @throws FlashTemplateNotFoundException |
288
|
|
|
*/ |
289
|
|
|
public function displaySemantic(string $type = null): string |
290
|
|
|
{ |
291
|
|
|
return $this->display($type, Templates::SEMANTIC); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns Spectre ready HTML for messages. |
296
|
|
|
* |
297
|
|
|
* @param string|null $type - message type: success, info, warning, error |
298
|
|
|
* |
299
|
|
|
* @throws FlashTemplateNotFoundException |
300
|
|
|
*/ |
301
|
|
|
public function displaySpectre(string $type = null): string |
302
|
|
|
{ |
303
|
|
|
return $this->display($type, Templates::SPECTRE); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns Spectre ready HTML for messages. |
308
|
|
|
* |
309
|
|
|
* @param string|null $type - message type: success, info, warning, error |
310
|
|
|
* |
311
|
|
|
* @throws FlashTemplateNotFoundException |
312
|
|
|
*/ |
313
|
|
|
public function displayHalfmoon(string $type = null): string |
314
|
|
|
{ |
315
|
|
|
return $this->display($type, Templates::HALFMOON); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|