@@ -35,18 +35,20 @@ use Phalcon\Di\InjectionAwareInterface;
35
35
abstract class Flash implements FlashInterface, InjectionAwareInterface
36
36
{
37
37
38
- protected _cssClasses;
39
-
40
- protected _implicitFlush = true ;
38
+ protected _autoescape = true ;
41
39
42
40
protected _automaticHtml = true ;
43
41
44
- protected _escaperService = null ;
42
+ protected _cssClasses ;
45
43
46
- protected _autoescape = true ;
44
+ protected _customTemplate = " " ;
47
45
48
46
protected _dependencyInjector = null ;
49
47
48
+ protected _escaperService = null ;
49
+
50
+ protected _implicitFlush = true ;
51
+
50
52
protected _messages;
51
53
52
54
/**
@@ -65,21 +67,55 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
65
67
let this -> _cssClasses = cssClasses;
66
68
}
67
69
70
+ /**
71
+ * Clears accumulated messages when implicit flush is disabled
72
+ */
73
+ public function clear () -> void
74
+ {
75
+ let this -> _messages = [];
76
+ }
77
+
78
+ /**
79
+ * Shows a HTML error message
80
+ *
81
+ *<code>
82
+ * $flash->error("This is an error");
83
+ *</code>
84
+ */
85
+ public function error (var message ) -> string
86
+ {
87
+ return this -> {" message" }(" error" , message);
88
+ }
89
+
68
90
/**
69
91
* Returns the autoescape mode in generated html
70
92
*/
71
93
public function getAutoescape () -> bool
72
94
{
73
- return this -> _autoescape;
95
+ return this -> _autoescape;
74
96
}
75
97
76
98
/**
77
- * Set the autoescape mode in generated html
99
+ * Returns the custom template set
78
100
*/
79
- public function setAutoescape ( boolean autoescape ) -> <Flash>
101
+ public function getCustomTemplate ( ) -> string
80
102
{
81
- let this -> _autoescape = autoescape;
82
- return this ;
103
+ return this -> _customTemplate;
104
+ }
105
+
106
+ /**
107
+ * Returns the internal dependency injector
108
+ */
109
+ public function getDI () -> <DiInterface>
110
+ {
111
+ var di;
112
+ let di = this -> _dependencyInjector;
113
+
114
+ if typeof di != " object" {
115
+ let di = Di:: getDefault();
116
+ }
117
+
118
+ return di;
83
119
}
84
120
85
121
/**
@@ -99,89 +135,79 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
99
135
100
136
return escaper;
101
137
}
102
-
103
138
/**
104
- * Sets the Escaper Service
139
+ * Shows a HTML notice/information message
140
+ *
141
+ *<code>
142
+ * $flash->notice("This is an information");
143
+ *</code>
105
144
*/
106
- public function setEscaperService ( <EscaperInterface> escaperService ) -> <Flash>
145
+ public function notice ( var message ) -> string
107
146
{
108
- let this -> _escaperService = escaperService;
109
- return this ;
147
+ return this -> {" message" }(" notice" , message);
110
148
}
111
149
112
150
/**
113
- * Sets the dependency injector
151
+ * Set the autoescape mode in generated html
114
152
*/
115
- public function setDI ( <DiInterface> dependencyInjector ) -> <Flash>
153
+ public function setAutoescape ( boolean autoescape ) -> <Flash>
116
154
{
117
- let this -> _dependencyInjector = dependencyInjector ;
155
+ let this -> _autoescape = autoescape ;
118
156
return this ;
119
157
}
120
158
121
159
/**
122
- * Returns the internal dependency injector
160
+ * Set if the output must be implicitly formatted with HTML
123
161
*/
124
- public function getDI ( ) -> <DiInterface >
162
+ public function setAutomaticHtml ( boolean automaticHtml ) -> <FlashInterface >
125
163
{
126
- var di;
127
- let di = this -> _dependencyInjector;
128
-
129
- if typeof di != " object" {
130
- let di = Di:: getDefault();
131
- }
132
-
133
- return di;
164
+ let this -> _automaticHtml = automaticHtml;
165
+ return this ;
134
166
}
135
167
136
168
/**
137
- * Set whether the output must be implicitly flushed to the output or returned as string
169
+ * Set an array with CSS classes to format the messages
138
170
*/
139
- public function setImplicitFlush ( boolean implicitFlush ) -> <FlashInterface>
171
+ public function setCssClasses ( array! cssClasses ) -> <FlashInterface>
140
172
{
141
- let this -> _implicitFlush = implicitFlush ;
173
+ let this -> _cssClasses = cssClasses ;
142
174
return this ;
143
175
}
144
176
145
177
/**
146
- * Set if the output must be implicitly formatted with HTML
178
+ * Set an custom template for showing the messages
147
179
*/
148
- public function setAutomaticHtml ( boolean automaticHtml ) -> <FlashInterface>
180
+ public function setCustomTemplate ( string! customTemplate ) -> <FlashInterface>
149
181
{
150
- let this -> _automaticHtml = automaticHtml ;
182
+ let this -> _customTemplate = customTemplate ;
151
183
return this ;
152
184
}
153
185
154
186
/**
155
- * Set an array with CSS classes to format the messages
187
+ * Sets the dependency injector
156
188
*/
157
- public function setCssClasses ( array! cssClasses ) -> <FlashInterface >
189
+ public function setDI ( <DiInterface> dependencyInjector ) -> <Flash >
158
190
{
159
- let this -> _cssClasses = cssClasses ;
191
+ let this -> _dependencyInjector = dependencyInjector ;
160
192
return this ;
161
193
}
162
194
163
195
/**
164
- * Shows a HTML error message
165
- *
166
- *<code>
167
- * $flash->error("This is an error");
168
- *</code>
196
+ * Sets the Escaper Service
169
197
*/
170
- public function error ( var message ) -> string
198
+ public function setEscaperService ( <EscaperInterface> escaperService ) -> <Flash>
171
199
{
172
- return this -> {" message" }(" error" , message);
200
+ let this -> _escaperService = escaperService;
201
+ return this ;
173
202
}
174
203
175
204
/**
176
- * Shows a HTML notice/information message
177
- *
178
- *<code>
179
- * $flash->notice("This is an information");
180
- *</code>
205
+ * Set whether the output must be implicitly flushed to the output or returned as string
181
206
*/
182
- public function notice ( var message ) -> string
207
+ public function setImplicitFlush ( boolean implicitFlush ) -> <FlashInterface>
183
208
{
184
- return this -> {" message" }(" notice" , message);
209
+ let this -> _implicitFlush = implicitFlush;
210
+ return this ;
185
211
}
186
212
187
213
/**
@@ -196,18 +222,6 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
196
222
return this -> {" message" }(" success" , message);
197
223
}
198
224
199
- /**
200
- * Shows a HTML warning message
201
- *
202
- *<code>
203
- * $flash->warning("Hey, this is important");
204
- *</code>
205
- */
206
- public function warning (var message ) -> string
207
- {
208
- return this -> {" message" }(" warning" , message);
209
- }
210
-
211
225
/**
212
226
* Outputs a message formatting it with HTML
213
227
*
@@ -220,30 +234,9 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
220
234
*/
221
235
public function outputMessage (string type, var message )
222
236
{
223
- boolean automaticHtml, implicitFlush;
224
- var content, cssClasses, classes, typeClasses, eol, msg,
225
- htmlMessage, autoEscape, escaper, preparedMsg;
226
-
227
- let automaticHtml = (bool) this -> _automaticHtml,
228
- autoEscape = (bool) this -> _autoescape;
229
-
230
- if automaticHtml === true {
231
- let classes = this -> _cssClasses;
232
- if fetch typeClasses, classes[type] {
233
- if typeof typeClasses == " array" {
234
- let cssClasses = " class=\" " . join(" " , typeClasses) . " \" " ;
235
- } else {
236
- let cssClasses = " class=\" " . typeClasses . " \" " ;
237
- }
238
- } else {
239
- let cssClasses = " " ;
240
- }
241
- let eol = PHP_EOL ;
242
- }
243
-
244
- if autoEscape === true {
245
- let escaper = this -> getEscaperService();
246
- }
237
+ boolean implicitFlush;
238
+ var content, msg,
239
+ htmlMessage, preparedMsg;
247
240
248
241
let implicitFlush = (bool) this -> _implicitFlush;
249
242
if typeof message == " array" {
@@ -259,20 +252,15 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
259
252
* We create the message with implicit flush or other
260
253
*/
261
254
for msg in message {
262
- if autoEscape === true {
263
- let preparedMsg = escaper-> escapeHtml(msg);
264
- } else {
265
- let preparedMsg = msg;
266
- }
255
+ /**
256
+ * Check if the message needs to be escaped
257
+ */
258
+ let preparedMsg = this -> prepareEscapedMessage(msg);
267
259
268
260
/**
269
261
* We create the applying formatting or not
270
262
*/
271
- if automaticHtml === true {
272
- let htmlMessage = " <div" . cssClasses . " >" . preparedMsg . " </div>" . eol;
273
- } else {
274
- let htmlMessage = preparedMsg;
275
- }
263
+ let htmlMessage = this -> prepareHtmlMessage(type, preparedMsg);
276
264
277
265
if implicitFlush === true {
278
266
echo htmlMessage;
@@ -290,20 +278,15 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
290
278
}
291
279
292
280
} else {
293
- if autoEscape === true {
294
- let preparedMsg = escaper-> escapeHtml(message);
295
- } else {
296
- let preparedMsg = message;
297
- }
281
+ /**
282
+ * Check if the message needs to be escaped
283
+ */
284
+ let preparedMsg = this -> prepareEscapedMessage(message);
298
285
299
286
/**
300
287
* We create the applying formatting or not
301
288
*/
302
- if automaticHtml === true {
303
- let htmlMessage = " <div" . cssClasses . " >" . preparedMsg . " </div>" . eol;
304
- } else {
305
- let htmlMessage = preparedMsg;
306
- }
289
+ let htmlMessage = this -> prepareHtmlMessage(type, preparedMsg);
307
290
308
291
/**
309
292
* We return the message as string if the implicit_flush is turned off
@@ -318,10 +301,74 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
318
301
}
319
302
320
303
/**
321
- * Clears accumulated messages when implicit flush is disabled
304
+ * Shows a HTML warning message
305
+ *
306
+ *<code>
307
+ * $flash->warning("Hey, this is important");
308
+ *</code>
322
309
*/
323
- public function clear ( ) -> void
310
+ public function warning ( var message ) -> string
324
311
{
325
- let this -> _messages = [];
312
+ return this -> {" message" }(" warning" , message);
313
+ }
314
+
315
+
316
+ private function getTemplate (string cssClassses ) -> string
317
+ {
318
+ if (" " === this -> _customTemplate) {
319
+ if (" " === cssClassses) {
320
+ return " <div>%message%</div>" . PHP_EOL ;
321
+ } else {
322
+ return " <div class=\" %cssClass%\" >%message%</div>" . PHP_EOL ;
323
+ }
324
+ }
325
+
326
+ return this -> _customTemplate;
327
+ }
328
+
329
+ /**
330
+ * Returns the message escaped if the autoEscape is true, otherwise the
331
+ * original message is returned
332
+ */
333
+ private function prepareEscapedMessage (string message ) -> string
334
+ {
335
+ var autoEscape, escaper;
336
+
337
+ let autoEscape = (bool) this -> _autoescape;
338
+
339
+ if autoEscape === true {
340
+ let escaper = this -> getEscaperService();
341
+ return escaper-> escapeHtml(message);
342
+ } else {
343
+ return message;
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Prepares the HTML output for the message. If automaticHtml is not set then
349
+ * the original message is returned
350
+ */
351
+ private function prepareHtmlMessage (string type, string message ) -> string
352
+ {
353
+ var classes, cssClasses, typeClasses, automaticHtml;
354
+
355
+ let automaticHtml = (bool) this -> _automaticHtml;
356
+
357
+ if automaticHtml === true {
358
+ let classes = this -> _cssClasses;
359
+ if fetch typeClasses, classes[type] {
360
+ if typeof typeClasses == " array" {
361
+ let cssClasses = join(" " , typeClasses);
362
+ } else {
363
+ let cssClasses = typeClasses;
364
+ }
365
+ } else {
366
+ let cssClasses = " " ;
367
+ }
368
+
369
+ return str_replace([" %cssClass%" , " %message%" ], [cssClasses, message], this -> getTemplate(cssClasses));
370
+ } else {
371
+ return message;
372
+ }
326
373
}
327
374
}
0 commit comments