|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tamtamchik\SimpleFlash; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Engine. |
|
7
|
|
|
*/ |
|
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
|
9 |
|
public function __construct(TemplateInterface $template) |
|
30
|
|
|
{ |
|
31
|
9 |
|
$this->template = $template; |
|
32
|
|
|
|
|
33
|
9 |
|
if (!array_key_exists($this->key, $_SESSION)) { |
|
34
|
3 |
|
$_SESSION[$this->key] = []; |
|
35
|
1 |
|
} |
|
36
|
9 |
|
} |
|
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
|
72 |
|
public function message($message = '', $type = 'info') |
|
47
|
|
|
{ |
|
48
|
72 |
|
if (is_array($message)) { |
|
49
|
3 |
|
foreach ($message as $issue) { |
|
50
|
3 |
|
$this->addMessage($issue, $type); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
69 |
|
return $this->addMessage($message, $type); |
|
57
|
|
|
} |
|
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
|
72 |
|
protected function addMessage($message = '', $type = 'info') |
|
68
|
|
|
{ |
|
69
|
72 |
|
$type = strip_tags($type); |
|
70
|
|
|
|
|
71
|
72 |
|
if (empty($message) || !in_array($type, $this->types)) { |
|
72
|
3 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
69 |
|
if (!array_key_exists($type, $_SESSION[$this->key])) { |
|
76
|
69 |
|
$_SESSION[$this->key][$type] = []; |
|
77
|
23 |
|
} |
|
78
|
|
|
|
|
79
|
69 |
|
$_SESSION[$this->key][$type][] = $message; |
|
80
|
|
|
|
|
81
|
69 |
|
return $this; |
|
82
|
|
|
} |
|
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
|
66 |
|
public function display($type = null) |
|
92
|
|
|
{ |
|
93
|
66 |
|
$result = ''; |
|
94
|
|
|
|
|
95
|
66 |
|
if (!is_null($type) && !in_array($type, $this->types)) { |
|
96
|
3 |
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
63 |
|
if (in_array($type, $this->types)) { |
|
100
|
3 |
|
$result .= $this->buildMessages($_SESSION[$this->key][$type], $type); |
|
101
|
61 |
|
} elseif (is_null($type)) { |
|
102
|
60 |
|
foreach ($_SESSION[$this->key] as $messageType => $messages) { |
|
103
|
60 |
|
$result .= $this->buildMessages($messages, $messageType); |
|
104
|
19 |
|
} |
|
105
|
19 |
|
} |
|
106
|
|
|
|
|
107
|
60 |
|
$this->clear($type); |
|
108
|
|
|
|
|
109
|
60 |
|
return $result; |
|
110
|
|
|
} |
|
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
|
27 |
|
public function hasMessages($type = null) |
|
120
|
|
|
{ |
|
121
|
27 |
|
if (!is_null($type)) { |
|
122
|
6 |
|
return !empty($_SESSION[$this->key][$type]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
21 |
|
foreach ($this->types as $type) { |
|
126
|
21 |
|
if (!empty($_SESSION[$this->key][$type])) { |
|
127
|
15 |
|
return true; |
|
128
|
|
|
} |
|
129
|
7 |
|
} |
|
130
|
|
|
|
|
131
|
21 |
|
return false; |
|
132
|
|
|
} |
|
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
|
63 |
|
public function clear($type = null) |
|
142
|
|
|
{ |
|
143
|
63 |
|
if (is_null($type)) { |
|
144
|
60 |
|
$_SESSION[$this->key] = []; |
|
145
|
20 |
|
} else { |
|
146
|
3 |
|
unset($_SESSION[$this->key][$type]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
63 |
|
return $this; |
|
150
|
|
|
} |
|
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
|
63 |
|
protected function buildMessages(array $flashes, $type) |
|
161
|
|
|
{ |
|
162
|
63 |
|
$messages = ''; |
|
163
|
63 |
|
foreach ($flashes as $msg) { |
|
164
|
63 |
|
$messages .= $this->template->wrapMessage($msg); |
|
165
|
20 |
|
} |
|
166
|
|
|
|
|
167
|
60 |
|
return $this->template->wrapMessages($messages, $type); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* If requested as string will HTML will be returned. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string - HTML with flash messages |
|
174
|
|
|
*/ |
|
175
|
6 |
|
public function __toString() |
|
176
|
|
|
{ |
|
177
|
6 |
|
return $this->display(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Shortcut for error message. |
|
182
|
|
|
* |
|
183
|
|
|
* @param $message - message text |
|
184
|
|
|
* |
|
185
|
|
|
* @return Engine $this |
|
186
|
|
|
*/ |
|
187
|
3 |
|
public function error($message) |
|
188
|
|
|
{ |
|
189
|
3 |
|
return $this->message($message, 'error'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Shortcut for warning message. |
|
194
|
|
|
* |
|
195
|
|
|
* @param $message - message text |
|
196
|
|
|
* |
|
197
|
|
|
* @return Engine $this |
|
198
|
|
|
*/ |
|
199
|
3 |
|
public function warning($message) |
|
200
|
|
|
{ |
|
201
|
3 |
|
return $this->message($message, 'warning'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Shortcut for info message. |
|
206
|
|
|
* |
|
207
|
|
|
* @param $message - message text |
|
208
|
|
|
* |
|
209
|
|
|
* @return Engine $this |
|
210
|
|
|
*/ |
|
211
|
24 |
|
public function info($message) |
|
212
|
|
|
{ |
|
213
|
24 |
|
return $this->message($message, 'info'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Shortcut for success message. |
|
218
|
|
|
* |
|
219
|
|
|
* @param $message - message text |
|
220
|
|
|
* |
|
221
|
|
|
* @return Engine $this |
|
222
|
|
|
*/ |
|
223
|
3 |
|
public function success($message) |
|
224
|
|
|
{ |
|
225
|
3 |
|
return $this->message($message, 'success'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Setter for $template. |
|
230
|
|
|
* |
|
231
|
|
|
* @param TemplateInterface $template |
|
232
|
|
|
* |
|
233
|
|
|
* @return Engine $this |
|
234
|
|
|
*/ |
|
235
|
15 |
|
public function setTemplate(TemplateInterface $template) |
|
236
|
|
|
{ |
|
237
|
15 |
|
$this->template = $template; |
|
238
|
|
|
|
|
239
|
15 |
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Getter for $template. |
|
244
|
|
|
* |
|
245
|
|
|
* @return TemplateInterface |
|
246
|
|
|
*/ |
|
247
|
3 |
|
public function getTemplate() |
|
248
|
|
|
{ |
|
249
|
3 |
|
return $this->template; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|