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