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