1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate; |
6
|
|
|
|
7
|
|
|
class Flash |
8
|
|
|
{ |
9
|
|
|
const TYPE_SUCCESS = 'success'; |
10
|
|
|
const TYPE_INFO = 'info'; |
11
|
|
|
const TYPE_ERROR = 'error'; |
12
|
|
|
const TYPE_DATA = 'data'; |
13
|
|
|
|
14
|
|
|
public static $types = [ |
15
|
|
|
self::TYPE_SUCCESS, |
16
|
|
|
self::TYPE_INFO, |
17
|
|
|
self::TYPE_ERROR, |
18
|
|
|
self::TYPE_DATA |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private static $items = []; |
22
|
|
|
private static $consumed = false; |
23
|
|
|
|
24
|
4 |
|
public static function read() |
25
|
|
|
{ |
26
|
4 |
|
if (!self::$consumed) { |
27
|
|
|
// Get notification |
28
|
4 |
|
self::$items = (array) Suricate::Session()->read('flash'); |
29
|
|
|
|
30
|
|
|
// Erase (consume) |
31
|
3 |
|
Suricate::Session()->destroy('flash'); |
32
|
3 |
|
self::$consumed = true; |
33
|
|
|
} |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Render success / info / error messages in HTML |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
1 |
|
public static function renderMessages(): string |
42
|
|
|
{ |
43
|
1 |
|
self::read(); |
44
|
|
|
|
45
|
|
|
$availableTypes = [ |
46
|
1 |
|
self::TYPE_SUCCESS => 'success', |
47
|
1 |
|
self::TYPE_INFO => 'info', |
48
|
1 |
|
self::TYPE_ERROR => 'danger' |
49
|
|
|
]; |
50
|
|
|
|
51
|
1 |
|
$output = ''; |
52
|
1 |
|
foreach ($availableTypes as $type => $displayAlias) { |
53
|
1 |
|
$currentMessage = self::getMessages($type); |
54
|
|
|
|
55
|
1 |
|
if (count($currentMessage)) { |
56
|
|
|
$output .= |
57
|
|
|
'<div class="alert alert-' . |
58
|
1 |
|
$displayAlias . |
59
|
1 |
|
'">' . |
60
|
1 |
|
implode('<br/>', (array) $currentMessage) . |
61
|
1 |
|
'</div>'; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $output; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get flash data for a key |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
1 |
|
public static function getData(string $key) |
75
|
|
|
{ |
76
|
1 |
|
self::read(); |
77
|
|
|
|
78
|
|
|
if ( |
79
|
1 |
|
isset(self::$items[self::TYPE_DATA]) && |
80
|
1 |
|
array_key_exists($key, self::$items[self::TYPE_DATA]) |
81
|
|
|
) { |
82
|
1 |
|
return self::$items[self::TYPE_DATA][$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get flash message for a type |
90
|
|
|
* |
91
|
|
|
* @param string $type |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
2 |
|
public static function getMessages(string $type): array |
95
|
|
|
{ |
96
|
2 |
|
self::read(); |
97
|
|
|
|
98
|
2 |
|
if (isset(self::$items[$type])) { |
99
|
2 |
|
$result = self::$items[$type]; |
100
|
2 |
|
unset(self::$items[$type]); |
101
|
2 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Write flash message or data to session |
109
|
|
|
* |
110
|
|
|
* @param string $type |
111
|
|
|
* @param mixed $message |
112
|
|
|
* @throws \InvalidArgumentException |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
3 |
|
private static function write(string $type, $message) |
116
|
|
|
{ |
117
|
3 |
|
if (in_array($type, static::$types)) { |
118
|
3 |
|
$currentSessionData = Suricate::Session()->read('flash'); |
119
|
|
|
|
120
|
|
|
if ( |
121
|
3 |
|
isset($currentSessionData[$type]) && |
122
|
3 |
|
is_array($currentSessionData[$type]) |
123
|
|
|
) { |
124
|
2 |
|
$newData = array_merge( |
125
|
2 |
|
$currentSessionData[$type], |
126
|
2 |
|
(array) $message |
127
|
|
|
); |
128
|
|
|
} else { |
129
|
3 |
|
$newData = (array) $message; |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
$currentSessionData[$type] = $newData; |
133
|
3 |
|
Suricate::Session()->write('flash', $currentSessionData); |
134
|
3 |
|
self::$consumed = false; |
135
|
|
|
|
136
|
3 |
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
throw new \InvalidArgumentException("Unknown message type '$type'"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Write message to flash storage |
144
|
|
|
* |
145
|
|
|
* @param string $type |
146
|
|
|
* @param string|array $message |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
2 |
|
public static function writeMessage(string $type, $message) |
150
|
|
|
{ |
151
|
2 |
|
self::write($type, $message); |
152
|
2 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Write data to flash storage |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* @param mixed $data |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
1 |
|
public static function writeData(string $key, $data) |
162
|
|
|
{ |
163
|
1 |
|
self::write(self::TYPE_DATA, [$key => $data]); |
164
|
1 |
|
} |
165
|
|
|
} |
166
|
|
|
|