Skip to content

Commit eee2166

Browse files
committed
Add raw generation and correct style
1 parent 67d242b commit eee2166

4 files changed

Lines changed: 151 additions & 155 deletions

File tree

README.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
1-
# Guide
1+
# Flash
22

3-
## Requirements
3+
## Flash message property
44

5-
- Require php ^7.4
5+
- General style:
6+
- margin & padding: 10px
7+
- border-radius: 75px
8+
- font-weight: 1000
9+
- font-size: 18px
10+
- display: grid
11+
12+
- Valid
13+
- text color: `#3C763D`
14+
- background color: `#DFFFD8`
15+
- icon: `&#10003`
16+
17+
- Info
18+
- text color: `#31708F`
19+
- background color: `#D9EDF7`
20+
- icon: `&#128712`
21+
22+
- Alert
23+
- text color: `#FF5D00`
24+
- background color: `#FFB149`
25+
- icon: `&#9888`
26+
27+
- Error
28+
- text color: `#FF3331`
29+
- background color: `#FFABAB`
30+
- icon: `&#10754`
31+
32+
## Create flash message
33+
34+
- ### Create flash message with default property
35+
36+
`Flash::type($message);`
37+
38+
With:
39+
- type -> in ['valid', 'info', 'alert', 'error']
40+
- message -> (string)
41+
42+
- ### Create flash message without default property but with general style
43+
44+
`Flash::other($message, $colorText, $backgroundColor, $iconCode)`
45+
46+
With:
47+
- message -> (string) message to show
48+
- colorText -> (string) color of text (all color type supported by css suported)
49+
- colorBackgroud -> (string) same of colorText but for background
50+
- iconCode -> Code for icon (prefer hex code)
51+
52+
## Generate flash message
53+
54+
- Generate message with css style: `Flash::generate();`
55+
- Generate message without default style: `Flash::rawGenerate()`
56+
57+
> Note: for the 2 methods, a class `flash` is added, but, for `rawGenerate`, a second class named by `flash-type` (with type, the type of message (valid, info, alert, error, other)) is added.

src/Flash.php

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<?php
2-
32
namespace Bubu\Flash;
43

54
use Bubu\Http\Session\Session;
65

6+
use function PHPSTORM_META\type;
7+
78
class 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 = '&#10003';
7589
break;
90+
7691
case 'info':
7792
$textColor = self::$infoText;
7893
$bg = self::$infoBg;
7994
$iconCode = '&#128712';
8095
break;
96+
8197
case 'alert':
8298
$textColor = self::$alertText;
8399
$bg = self::$alertBg;
84100
$iconCode = '&#9888';
85101
break;
102+
86103
case 'error':
87104
$textColor = self::$errorText;
88105
$bg = self::$errorBg;
89106
$iconCode = '&#10754';
90107
break;
108+
91109
case 'other':
92110
$textColor = $value['colorText'];
93111
$bg = $value['colorBackground'];
94-
$value = $value['message'];
95-
$iconCode = '&#128712';
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()">&cross;</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">&cross;</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 = '&#10003';
148+
break;
149+
150+
case 'info':
151+
$class = "info";
152+
$iconCode = '&#128712';
153+
break;
154+
155+
case 'alert':
156+
$class = "alert";
157+
$iconCode = '&#9888';
158+
break;
159+
160+
case 'error':
161+
$class = "error";
162+
$iconCode = '&#10754';
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()">&cross;</p>
174+
<p>{$iconCode}</p>
175+
<p>{$message}</p>
176+
</div>
177+
HTML;
117178
}
118179
Session::delete('flash');
119180
return $flash;

tests/flash.php

Lines changed: 0 additions & 127 deletions
This file was deleted.

tests/index.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<?php
22
require '../vendor/autoload.php';
33

4-
use Bubu\Tests\Flash\Flash;
4+
use Bubu\Flash\Flash;
55

66
Flash::info('Test info');
7+
/*Flash::info('Test info');
78
8-
echo Flash::generate();
9+
Flash::alert('Test alert');
10+
11+
Flash::valid('Test info');
12+
13+
Flash::error('Test info');
14+
*/
15+
Flash::other('Autre', '#00AAFF', '#FFFFFF', '&#x20AC');
16+
17+
18+
echo Flash::rawGenerate();

0 commit comments

Comments
 (0)