11<?php
2-
32namespace Bubu \Flash ;
43
54use Bubu \Http \Session \Session ;
65
6+ use function PHPSTORM_META \type ;
7+
78class Flash
89{
910
1011 private static $ validBg = '#DFFFD8 ' ;
1112 private static $ validText = '#3C763D ' ;
13+
1214 private static $ infoBg = '#D9EDF7 ' ;
1315 private static $ infoText = '#31708F ' ;
16+
1417 private static $ alertBg = '#FFB149 ' ;
1518 private static $ alertText = '#FF5D00 ' ;
19+
1620 private static $ errorBg = '#FFABAB ' ;
1721 private static $ errorText = '#FF3331 ' ;
1822
@@ -36,14 +40,19 @@ public static function error(string $message): void
3640 Session::push ('flash ' , ['error ' => $ message ]);
3741 }
3842
39- public static function other (string $ message , string $ colorText , string $ backgroundColor ): void
40- {
43+ public static function other (
44+ string $ message ,
45+ string $ colorText ,
46+ string $ backgroundColor ,
47+ string $ iconCode
48+ ): void {
4149 Session::push ('flash ' ,
4250 [
4351 'other ' => [
4452 'message ' => $ message ,
4553 'colorText ' => $ colorText ,
46- 'colorBackground ' => $ backgroundColor
54+ 'colorBackground ' => $ backgroundColor ,
55+ 'iconCode ' => $ iconCode
4756 ]
4857 ]
4958 );
@@ -63,57 +72,109 @@ public static function generate()
6372 border-radius: 75px;
6473 font-weight: 1000;
6574 font-size: 18px;
75+ display: grid;
76+ grid-template-columns: min-content min-content auto;
77+ align-items: center;
6678 }
6779 </style>
6880 HTML ;
81+
6982 foreach (Session::get ('flash ' ) as $ key => $ value ) {
83+ $ message = (isset ($ value ['message ' ]) ? $ value ['message ' ] : $ value );
7084 switch ($ key ) {
7185 case 'valid ' :
7286 $ textColor = self ::$ validText ;
7387 $ bg = self ::$ validBg ;
7488 $ iconCode = '✓ ' ;
7589 break ;
90+
7691 case 'info ' :
7792 $ textColor = self ::$ infoText ;
7893 $ bg = self ::$ infoBg ;
7994 $ iconCode = '🛈 ' ;
8095 break ;
96+
8197 case 'alert ' :
8298 $ textColor = self ::$ alertText ;
8399 $ bg = self ::$ alertBg ;
84100 $ iconCode = '⚠ ' ;
85101 break ;
102+
86103 case 'error ' :
87104 $ textColor = self ::$ errorText ;
88105 $ bg = self ::$ errorBg ;
89106 $ iconCode = '⨂ ' ;
90107 break ;
108+
91109 case 'other ' :
92110 $ textColor = $ value ['colorText ' ];
93111 $ bg = $ value ['colorBackground ' ];
94- $ value = $ value ['message ' ];
95- $ iconCode = '🛈 ' ;
112+ $ iconCode = $ value ['iconCode ' ];
96113 break ;
97114 }
98115
99- foreach ((array ) $ value as $ messages ) {
100- $ flash .= <<<HTML
101- <div
102- class="flash"
103- style="
104- color: {$ textColor };
105- border-color: {$ textColor };
106- background: {$ bg };
107- "
108- >
109- <p onclick="this.remove()">✗</p>
110- <p>
111- <span style="margin: 0 10px; font-size: 1.5em; vertical-align: sub"> {$ iconCode }</span>
112- {$ messages }
113- </p>
114- </div>
115- HTML ;
116+ $ flash .= <<<HTML
117+ <div
118+ class="flash"
119+ style="
120+ color: {$ textColor };
121+ border-color: {$ textColor };
122+ background: {$ bg };
123+ "
124+ >
125+ <p onclick="this.parentElement.remove()" style="cursor: pointer">✗</p>
126+ <p style="margin: 0 10px; font-size: 1.5em; vertical-align: sub"> {$ iconCode }</p>
127+ <p> {$ message }</p>
128+ </div>
129+ HTML ;
130+ }
131+ Session::delete ('flash ' );
132+ return $ flash ;
133+ }
134+
135+ public static function rawGenerate ()
136+ {
137+ if (is_null (Session::get ('flash ' ))) {
138+ return '' ;
139+ }
140+
141+ $ flash = "" ;
142+ foreach (Session::get ('flash ' ) as $ key => $ value ) {
143+ $ message = (isset ($ value ['message ' ]) ? $ value ['message ' ] : $ value );
144+ switch ($ key ) {
145+ case 'valid ' :
146+ $ class = "valid " ;
147+ $ iconCode = '✓ ' ;
148+ break ;
149+
150+ case 'info ' :
151+ $ class = "info " ;
152+ $ iconCode = '🛈 ' ;
153+ break ;
154+
155+ case 'alert ' :
156+ $ class = "alert " ;
157+ $ iconCode = '⚠ ' ;
158+ break ;
159+
160+ case 'error ' :
161+ $ class = "error " ;
162+ $ iconCode = '⨂ ' ;
163+ break ;
164+
165+ case 'other ' :
166+ $ class = "other " ;
167+ $ iconCode = $ value ['iconCode ' ];
168+ break ;
116169 }
170+
171+ $ flash .= <<<HTML
172+ <div class="flash flash- {$ class }">
173+ <p onclick="this.parentElement.remove()">✗</p>
174+ <p> {$ iconCode }</p>
175+ <p> {$ message }</p>
176+ </div>
177+ HTML ;
117178 }
118179 Session::delete ('flash ' );
119180 return $ flash ;
0 commit comments