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