1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bluz Framework Component |
4
|
|
|
* |
5
|
|
|
* @copyright Bluz PHP Team |
6
|
|
|
* @link https://github.com/bluzphp/framework |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Bluz\Messages; |
12
|
|
|
|
13
|
|
|
use Bluz\Common\Options; |
14
|
|
|
use Bluz\Proxy\Session; |
15
|
|
|
use Bluz\Proxy\Translator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Realization of Flash Messages |
19
|
|
|
* |
20
|
|
|
* @package Bluz\Messages |
21
|
|
|
* @author Anton Shevchuk |
22
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Messages |
23
|
|
|
*/ |
24
|
|
|
class Messages |
25
|
|
|
{ |
26
|
|
|
use Options; |
27
|
|
|
|
28
|
|
|
private const TYPE_ERROR = 'error'; |
29
|
|
|
private const TYPE_SUCCESS = 'success'; |
30
|
|
|
private const TYPE_NOTICE = 'notice'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array list of messages types |
34
|
|
|
*/ |
35
|
|
|
protected $types = [ |
36
|
|
|
self::TYPE_ERROR, |
37
|
|
|
self::TYPE_SUCCESS, |
38
|
|
|
self::TYPE_NOTICE |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add notice |
43
|
|
|
* |
44
|
|
|
* @param string $message |
45
|
|
|
* @param string[] $text |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* @since 1.0.0 added $text |
49
|
|
|
*/ |
50
|
4 |
|
public function addNotice($message, ...$text) : void |
51
|
|
|
{ |
52
|
4 |
|
$this->add(self::TYPE_NOTICE, $message, ...$text); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add success |
57
|
|
|
* |
58
|
|
|
* @param string $message |
59
|
|
|
* @param string[] $text |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @since 1.0.0 added $text |
63
|
|
|
*/ |
64
|
4 |
|
public function addSuccess($message, ...$text) : void |
65
|
|
|
{ |
66
|
4 |
|
$this->add(self::TYPE_SUCCESS, $message, ...$text); |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add error |
71
|
|
|
* |
72
|
|
|
* @param string $message |
73
|
|
|
* @param string[] $text |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @since 1.0.0 added $text |
77
|
|
|
*/ |
78
|
4 |
|
public function addError($message, ...$text) : void |
79
|
|
|
{ |
80
|
4 |
|
$this->add(self::TYPE_ERROR, $message, ...$text); |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add message to container |
85
|
|
|
* |
86
|
|
|
* @param string $type One of error, notice or success |
87
|
|
|
* @param string $message |
88
|
|
|
* @param string[] $text |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
4 |
|
protected function add($type, $message, ...$text) : void |
93
|
|
|
{ |
94
|
4 |
|
$this->getMessagesStore()[$type][] = Translator::translate($message, ...$text); |
95
|
4 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Pop a message by type |
99
|
|
|
* |
100
|
|
|
* @param string $type |
101
|
|
|
* |
102
|
|
|
* @return \stdClass|null |
103
|
|
|
*/ |
104
|
4 |
|
public function pop($type) : ?\stdClass |
105
|
|
|
{ |
106
|
4 |
|
$text = array_shift($this->getMessagesStore()[$type]); |
107
|
4 |
|
if ($text) { |
108
|
3 |
|
$message = new \stdClass(); |
109
|
3 |
|
$message->text = $text; |
110
|
3 |
|
$message->type = $type; |
111
|
3 |
|
return $message; |
112
|
|
|
} |
113
|
1 |
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Pop all messages |
118
|
|
|
* |
119
|
|
|
* @return \ArrayObject|array |
120
|
|
|
*/ |
121
|
2 |
|
public function popAll() |
122
|
|
|
{ |
123
|
2 |
|
$messages = $this->getMessagesStore()->getArrayCopy(); |
124
|
2 |
|
$this->resetMessagesStore(); |
125
|
2 |
|
return $messages; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get size of messages container |
130
|
|
|
* |
131
|
|
|
* @return integer |
132
|
|
|
*/ |
133
|
2 |
|
public function count() : int |
134
|
|
|
{ |
135
|
2 |
|
$size = 0; |
136
|
2 |
|
if (!$store = $this->getMessagesStore()) { |
137
|
|
|
return $size; |
138
|
|
|
} |
139
|
2 |
|
foreach ($store as $messages) { |
140
|
2 |
|
$size += count($messages); |
141
|
|
|
} |
142
|
2 |
|
return $size; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Reset messages |
147
|
|
|
* |
148
|
|
|
* @param \ArrayObject $store |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
6 |
|
protected function setMessagesStore(\ArrayObject $store) : void |
152
|
|
|
{ |
153
|
6 |
|
Session::set('messages:store', $store); |
154
|
6 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns current or new messages store if it not exists |
158
|
|
|
* |
159
|
|
|
* @return \ArrayObject |
160
|
|
|
*/ |
161
|
6 |
|
protected function getMessagesStore() : \ArrayObject |
162
|
|
|
{ |
163
|
6 |
|
if (!$store = Session::get('messages:store')) { |
164
|
6 |
|
$this->resetMessagesStore(); |
165
|
|
|
} |
166
|
6 |
|
return Session::get('messages:store'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Reset messages |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
6 |
|
protected function resetMessagesStore() : void |
175
|
|
|
{ |
176
|
6 |
|
$this->setMessagesStore($this->createEmptyMessagesStore()); |
177
|
6 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Creates a new empty store for messages |
181
|
|
|
* |
182
|
|
|
* @return \ArrayObject |
183
|
|
|
*/ |
184
|
6 |
|
protected function createEmptyMessagesStore() : \ArrayObject |
185
|
|
|
{ |
186
|
6 |
|
return new \ArrayObject( |
187
|
|
|
[ |
188
|
6 |
|
self::TYPE_ERROR => [], |
189
|
6 |
|
self::TYPE_SUCCESS => [], |
190
|
6 |
|
self::TYPE_NOTICE => [] |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|