|
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
|
3 |
|
public static function read() |
|
22
|
|
|
{ |
|
23
|
3 |
|
if (!self::$consumed) { |
|
24
|
|
|
// Get notification |
|
25
|
3 |
|
self::$items = (array) Suricate::Session()->read('flash'); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// Erase (consume) |
|
28
|
2 |
|
Suricate::Session()->destroy('flash'); |
|
29
|
2 |
|
self::$consumed = true; |
|
30
|
|
|
} |
|
31
|
2 |
|
} |
|
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
|
|
|
public static function getData(string $key): array |
|
61
|
|
|
{ |
|
62
|
|
|
self::read(); |
|
63
|
|
|
|
|
64
|
|
|
if (isset(self::$items[self::TYPE_DATA]) && array_key_exists($key, self::$items[self::TYPE_DATA])) { |
|
65
|
|
|
return self::$items[self::TYPE_DATA][$key]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return []; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get flash message for a type |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $type |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public static function getMessages(string $type): array |
|
78
|
|
|
{ |
|
79
|
2 |
|
self::read(); |
|
80
|
|
|
|
|
81
|
2 |
|
if (isset(self::$items[$type])) { |
|
82
|
2 |
|
$result = self::$items[$type]; |
|
83
|
2 |
|
unset(self::$items[$type]); |
|
84
|
2 |
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Write flash message or data to session |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* @param mixed $message |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public static function write(string $type, $message) |
|
98
|
|
|
{ |
|
99
|
2 |
|
if (in_array($type, static::$types)) { |
|
100
|
2 |
|
$currentSessionData = Suricate::Session()->read('flash'); |
|
101
|
|
|
|
|
102
|
2 |
|
if (isset($currentSessionData[$type]) && is_array($currentSessionData[$type])) { |
|
103
|
2 |
|
$newData = array_merge($currentSessionData[$type], (array) $message); |
|
104
|
|
|
} else { |
|
105
|
2 |
|
$newData = (array) $message; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
$currentSessionData[$type] = $newData; |
|
109
|
2 |
|
Suricate::Session()->write('flash', $currentSessionData); |
|
110
|
2 |
|
self::$consumed = false; |
|
111
|
|
|
} |
|
112
|
2 |
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|