1
|
|
|
<?php namespace Anomaly\Streams\Platform\Message; |
2
|
|
|
|
3
|
|
|
use Illuminate\Session\Store; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class MessageBag |
7
|
|
|
* |
8
|
|
|
* @link http://anomaly.is/streams-platform |
9
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
10
|
|
|
* @author Ryan Thompson <[email protected]> |
11
|
|
|
* @package Anomaly\Streams\Platform\MessageBag |
12
|
|
|
*/ |
13
|
|
|
class MessageBag |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The session store. |
18
|
|
|
* |
19
|
|
|
* @var Store |
20
|
|
|
*/ |
21
|
|
|
protected $session; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new MessageBag instance. |
25
|
|
|
* |
26
|
|
|
* @param Store $session |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Store $session) |
29
|
|
|
{ |
30
|
|
|
$this->session = $session; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Merge a message onto the session. |
35
|
|
|
* |
36
|
|
|
* @param $type |
37
|
|
|
* @param $message |
38
|
|
|
*/ |
39
|
|
|
protected function merge($type, $message) |
40
|
|
|
{ |
41
|
|
|
$messages = $this->session->get($type, []); |
42
|
|
|
|
43
|
|
|
if (is_array($message)) { |
44
|
|
|
$messages = array_merge($messages, $message); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (is_string($message)) { |
48
|
|
|
array_push($messages, $message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$messages = array_unique($messages); |
52
|
|
|
|
53
|
|
|
$this->session->set($type, $messages); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return whether messages exist. |
58
|
|
|
* |
59
|
|
|
* @param $type |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function has($type) |
63
|
|
|
{ |
64
|
|
|
return $this->session->has($type); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get messages. |
69
|
|
|
* |
70
|
|
|
* @param $type |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function get($type) |
74
|
|
|
{ |
75
|
|
|
return $this->session->get($type); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Pull the messages. |
80
|
|
|
* |
81
|
|
|
* @param $type |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function pull($type) |
85
|
|
|
{ |
86
|
|
|
return $this->session->pull($type); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add an error message. |
91
|
|
|
* |
92
|
|
|
* @param $message |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function error($message) |
96
|
|
|
{ |
97
|
|
|
$this->merge(__FUNCTION__, $message); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add an info message. |
104
|
|
|
* |
105
|
|
|
* @param $message |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function info($message) |
109
|
|
|
{ |
110
|
|
|
$this->merge(__FUNCTION__, $message); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add a success message. |
117
|
|
|
* |
118
|
|
|
* @param $message |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function success($message) |
122
|
|
|
{ |
123
|
|
|
$this->merge(__FUNCTION__, $message); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add a warning message. |
130
|
|
|
* |
131
|
|
|
* @param $message |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function warning($message) |
135
|
|
|
{ |
136
|
|
|
$this->merge(__FUNCTION__, $message); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Flush the messages. |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function flush() |
147
|
|
|
{ |
148
|
|
|
$this->session->forget('info'); |
149
|
|
|
$this->session->forget('error'); |
150
|
|
|
$this->session->forget('success'); |
151
|
|
|
$this->session->forget('warning'); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|