|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Message.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:FlashMessages! |
|
9
|
|
|
* @subpackage Entities |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 06.02.15 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\FlashMessages\Entities; |
|
18
|
|
|
|
|
19
|
|
|
use Nette; |
|
20
|
|
|
use Nette\Localization; |
|
21
|
|
|
|
|
22
|
|
|
use IPub\FlashMessages\Adapters; |
|
23
|
|
|
use IPub\FlashMessages\Exceptions; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Flash message entity |
|
27
|
|
|
* |
|
28
|
|
|
* @package iPublikuj:FlashMessages! |
|
29
|
|
|
* @subpackage Entities |
|
30
|
|
|
* |
|
31
|
|
|
* @author Adam Kadlec <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
1 |
|
class Message extends \stdClass implements IMessage |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Implement nette smart magic |
|
37
|
|
|
*/ |
|
38
|
1 |
|
use Nette\SmartObject; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $message; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $level; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $title; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
private $overlay = FALSE; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool |
|
62
|
|
|
*/ |
|
63
|
|
|
private $displayed = FALSE; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var Localization\ITranslator |
|
67
|
|
|
*/ |
|
68
|
|
|
private $translator; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var Adapters\IPhraseAdapter |
|
72
|
|
|
*/ |
|
73
|
|
|
private $messagePhraseAdapter; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var Adapters\IPhraseAdapter |
|
77
|
|
|
*/ |
|
78
|
|
|
private $titlePhraseAdapter; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Localization\ITranslator $translator |
|
82
|
|
|
* @param Adapters\IPhraseAdapter $messagePhraseAdapter |
|
83
|
|
|
* @param Adapters\IPhraseAdapter $titlePhraseAdapter |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct( |
|
86
|
|
|
Localization\ITranslator $translator = NULL, |
|
87
|
|
|
Adapters\IPhraseAdapter $messagePhraseAdapter, |
|
88
|
|
|
Adapters\IPhraseAdapter $titlePhraseAdapter = NULL |
|
89
|
|
|
) { |
|
90
|
|
|
$this->translator = $translator; |
|
91
|
|
|
$this->messagePhraseAdapter = $messagePhraseAdapter; |
|
92
|
|
|
$this->titlePhraseAdapter = $titlePhraseAdapter; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setMessage(string $message) : void |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->isUnserialized()) { |
|
101
|
|
|
$this->message = $message; |
|
102
|
|
|
|
|
103
|
|
|
} else { |
|
104
|
|
|
$this->messagePhraseAdapter->setMessage($message); |
|
105
|
|
|
$this->message = NULL; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getMessage() : string |
|
113
|
|
|
{ |
|
114
|
|
|
if ($this->message === NULL && $this->translator) { |
|
115
|
|
|
$this->message = $this->messagePhraseAdapter->translate($this->translator); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->message; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setLevel(string $level) : void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->level = $level; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getLevel() : string |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->level; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function info() : void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->setLevel(self::LEVEL_INFO); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function success() : void |
|
149
|
|
|
{ |
|
150
|
|
|
$this->setLevel(self::LEVEL_SUCCESS); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function warning() : void |
|
157
|
|
|
{ |
|
158
|
|
|
$this->setLevel(self::LEVEL_WARNING); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
*/ |
|
164
|
|
|
public function error() : void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->setLevel(self::LEVEL_ERROR); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setTitle(string $title = NULL) : void |
|
173
|
|
|
{ |
|
174
|
|
|
if ($this->isUnserialized()) { |
|
175
|
|
|
$this->title = $title; |
|
176
|
|
|
|
|
177
|
|
|
} else { |
|
178
|
|
|
if ($this->titlePhraseAdapter instanceof Adapters\IPhraseAdapter) { |
|
179
|
|
|
$this->titlePhraseAdapter->setMessage($title); |
|
180
|
|
|
} |
|
181
|
|
|
$this->title = NULL; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getTitle() : ?string |
|
189
|
|
|
{ |
|
190
|
|
|
if ($this->title === NULL && $this->translator && $this->titlePhraseAdapter instanceof Adapters\IPhraseAdapter) { |
|
191
|
|
|
$this->title = $this->titlePhraseAdapter->translate($this->translator); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $this->title; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
*/ |
|
200
|
|
|
public function setOverlay(bool $overlay) : void |
|
201
|
|
|
{ |
|
202
|
|
|
$this->overlay = $overlay; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* {@inheritdoc} |
|
207
|
|
|
*/ |
|
208
|
|
|
public function hasOverlay() : bool |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->overlay; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* {@inheritdoc} |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setParameters(array $parameter) : void |
|
217
|
|
|
{ |
|
218
|
|
|
$this->validateState(__FUNCTION__); |
|
219
|
|
|
$this->messagePhraseAdapter->setParameters($parameter); |
|
220
|
|
|
$this->message = NULL; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setCount(int $count) : void |
|
227
|
|
|
{ |
|
228
|
|
|
$this->validateState(__FUNCTION__); |
|
229
|
|
|
$this->messagePhraseAdapter->setCount($count); |
|
230
|
|
|
$this->message = NULL; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* {@inheritdoc} |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setDisplayed(bool $displayed = TRUE) : void |
|
237
|
|
|
{ |
|
238
|
|
|
$this->displayed = $displayed; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* {@inheritdoc} |
|
243
|
|
|
*/ |
|
244
|
|
|
public function isDisplayed() : bool |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->displayed; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param string $method |
|
251
|
|
|
* |
|
252
|
|
|
* @return void |
|
253
|
|
|
* |
|
254
|
|
|
* @throws Exceptions\InvalidStateException |
|
255
|
|
|
*/ |
|
256
|
|
|
private function validateState(string $method) : void |
|
257
|
|
|
{ |
|
258
|
|
|
if ($this->isUnserialized()) { |
|
259
|
|
|
throw new Exceptions\InvalidStateException(sprintf('You cannot call method %s on unserialized Entities\Message object', $method)); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @return bool |
|
265
|
|
|
*/ |
|
266
|
|
|
private function isUnserialized() : bool |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->translator === NULL; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
public function __toString() |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->level . ' ' . ($this->title ? $this->title . ' ' : '') . $this->getMessage(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
|
|
public function __sleep() |
|
283
|
|
|
{ |
|
284
|
|
|
$this->message = $this->getMessage(); |
|
285
|
|
|
|
|
286
|
|
|
return ['message', 'level', 'title', 'overlay', 'displayed']; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|