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
|
6 |
|
public function __construct(TemplateInterface $template) |
30
|
|
|
{ |
31
|
6 |
|
$this->template = $template; |
32
|
|
|
|
33
|
6 |
|
if (!array_key_exists($this->key, $_SESSION)) { |
34
|
2 |
|
$_SESSION[$this->key] = []; |
35
|
2 |
|
} |
36
|
6 |
|
} |
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
|
48 |
|
public function message($message = '', $type = 'info') |
47
|
|
|
{ |
48
|
48 |
|
if (is_array($message)) { |
49
|
2 |
|
foreach ($message as $issue) { |
50
|
2 |
|
$this->addMessage($issue, $type); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
2 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
46 |
|
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
|
48 |
|
protected function addMessage($message = '', $type = 'info') |
68
|
|
|
{ |
69
|
48 |
|
$type = strip_tags($type); |
70
|
|
|
|
71
|
48 |
|
if (empty($message) || !in_array($type, $this->types)) { |
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
46 |
|
if (!array_key_exists($type, $_SESSION[$this->key])) { |
76
|
46 |
|
$_SESSION[$this->key][$type] = []; |
77
|
46 |
|
} |
78
|
|
|
|
79
|
46 |
|
$_SESSION[$this->key][$type][] = $message; |
80
|
|
|
|
81
|
46 |
|
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
|
44 |
|
public function display($type = null) |
92
|
|
|
{ |
93
|
44 |
|
$result = ''; |
94
|
|
|
|
95
|
44 |
|
if (!is_null($type) && !in_array($type, $this->types)) { |
96
|
2 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
42 |
|
if (in_array($type, $this->types)) { |
100
|
2 |
|
$result .= $this->buildMessages($_SESSION[$this->key][$type], $type); |
101
|
42 |
|
} elseif (is_null($type)) { |
102
|
40 |
|
foreach ($_SESSION[$this->key] as $messageType => $messages) { |
103
|
40 |
|
$result .= $this->buildMessages($messages, $messageType); |
104
|
38 |
|
} |
105
|
38 |
|
} |
106
|
|
|
|
107
|
40 |
|
$this->clear($type); |
108
|
|
|
|
109
|
40 |
|
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
|
18 |
|
public function hasMessages($type = null) |
120
|
|
|
{ |
121
|
18 |
|
if (!is_null($type)) { |
122
|
4 |
|
return !empty($_SESSION[$this->key][$type]); |
123
|
|
|
} |
124
|
|
|
|
125
|
14 |
|
foreach ($this->types as $type) { |
126
|
14 |
|
if (!empty($_SESSION[$this->key][$type])) { |
127
|
2 |
|
return true; |
128
|
|
|
} |
129
|
14 |
|
} |
130
|
|
|
|
131
|
14 |
|
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
|
42 |
|
public function clear($type = null) |
142
|
|
|
{ |
143
|
42 |
|
if (is_null($type)) { |
144
|
40 |
|
$_SESSION[$this->key] = []; |
145
|
40 |
|
} else { |
146
|
2 |
|
unset($_SESSION[$this->key][$type]); |
147
|
|
|
} |
148
|
|
|
|
149
|
42 |
|
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
|
42 |
|
protected function buildMessages(array $flashes, $type) |
161
|
|
|
{ |
162
|
42 |
|
$messages = ''; |
163
|
42 |
|
foreach ($flashes as $msg) { |
164
|
42 |
|
$messages .= $this->template->wrapMessage($msg); |
165
|
40 |
|
} |
166
|
|
|
|
167
|
40 |
|
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
|
4 |
|
public function __toString() |
176
|
|
|
{ |
177
|
4 |
|
return $this->display(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Shortcut for error message. |
182
|
|
|
* |
183
|
|
|
* @param $message - message text |
184
|
|
|
* |
185
|
|
|
* @return Engine $this |
186
|
|
|
*/ |
187
|
2 |
|
public function error($message) |
188
|
|
|
{ |
189
|
2 |
|
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
|
2 |
|
public function warning($message) |
200
|
|
|
{ |
201
|
2 |
|
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
|
16 |
|
public function info($message) |
212
|
|
|
{ |
213
|
16 |
|
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
|
2 |
|
public function success($message) |
224
|
|
|
{ |
225
|
2 |
|
return $this->message($message, 'success'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Setter for $template. |
230
|
|
|
* |
231
|
|
|
* @param TemplateInterface $template |
232
|
|
|
* |
233
|
|
|
* @return Engine $this |
234
|
|
|
*/ |
235
|
10 |
|
public function setTemplate(TemplateInterface $template) |
236
|
|
|
{ |
237
|
10 |
|
$this->template = $template; |
238
|
|
|
|
239
|
10 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Getter for $template. |
244
|
|
|
* |
245
|
|
|
* @return TemplateInterface |
246
|
|
|
*/ |
247
|
2 |
|
public function getTemplate() |
248
|
|
|
{ |
249
|
2 |
|
return $this->template; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|