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