-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.php
101 lines (91 loc) · 2.56 KB
/
Notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <[email protected]>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Notification;
class Notification
{
/**
* Notifications session key
*
* @var string
*/
const SESSION_KEY = 'notifications';
/**
* Notifications array
*
* @var array
*/
private static $notifications = [];
/**
* Returns a specific variable from the Notifications array.
*
* echo Notification::get('success');
* echo Notification::get('errors');
*
* @param string $key Variable name
* @return mixed
*/
public static function get(string $key)
{
return isset(Notification::$notifications[$key]) ? Notification::$notifications[$key] : null;
}
/**
* Adds specific variable to the Notifications array.
*
* Notification::set('success', 'Data has been saved with success!');
* Notification::set('errors', 'Data not saved!');
*
* @param string $key Variable name
* @param mixed $value Variable value
*/
public static function set(string $key, $value) : void
{
$_SESSION[Notification::SESSION_KEY][$key] = $value;
}
/**
* Adds specific variable to the Notifications array for current page.
*
* Notification::setNow('success', 'Success!');
*
* @param string $var Variable name
* @param mixed $value Variable value
*/
public static function setNow(string $key, $value) : void
{
Notification::$notifications[$key] = $value;
}
/**
* Clears the Notifications array.
*
* Notification::clean();
*
* Data that previous pages stored will not be deleted, just the data that
* this page stored itself.
*/
public static function clean() : void
{
$_SESSION[Notification::SESSION_KEY] = [];
}
/**
* Initializes the Notification service.
*
* Notification::init();
*
* This will read notification/flash data from the $_SESSION variable and load it into
* the $this->previous array.
*/
public static function init() : void
{
if ( ! empty($_SESSION[Notification::SESSION_KEY]) && is_array($_SESSION[Notification::SESSION_KEY])) {
Notification::$notifications = $_SESSION[Notification::SESSION_KEY];
}
$_SESSION[Notification::SESSION_KEY] = [];
}
}