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