-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificator.php
97 lines (78 loc) · 2.29 KB
/
Notificator.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
<?php
namespace Ling\Notificator;
use Ling\Notificator\Exception\NotificatorException;
/**
* The basics of a notification is just a simple message (string).
*
* However, I can see the case where someday we'll need a title, or an icon or both...
* I give you the options for that.
* Use them as you like.
*
*
*/
class Notificator
{
/**
* @var array of type => notificationItems.
* - with: notificationItems, array of notificationItem, each of which:
* - 0: string message
* - 1: array options, whatever you put in it...
*
*/
private static $notifs = [
"success" => [],
"info" => [],
"warning" => [],
"error" => [],
];
public static function addSuccess(string $msg, array $options = [])
{
self::addNotif($msg, "success", $options);
}
public static function addInfo(string $msg, array $options = [])
{
self::addNotif($msg, "info", $options);
}
public static function addWarning(string $msg, array $options = [])
{
self::addNotif($msg, "warning", $options);
}
public static function addError(string $msg, array $options = [])
{
self::addNotif($msg, "error", $options);
}
public static function getNotifications(string $type = null)
{
$notifs = static::getAllNotifications();
if (null === $type) {
return $notifs;
}
if (array_key_exists($type, [
"success",
"info",
"warning",
"error",
])) {
return $notifs[$type];
}
throw new NotificatorException("Unknown notification type: $type");
}
//--------------------------------------------
//
//--------------------------------------------
protected static function onNotificationAddedAfter(array $notifications)
{
}
protected static function getAllNotifications()
{
return self::$notifs;
}
//--------------------------------------------
//
//--------------------------------------------
private static function addNotif(string $msg, string $type, array $options = [])
{
self::$notifs[$type][] = [$msg, $options];
static::onNotificationAddedAfter(self::$notifs);
}
}