1 | <?php |
||
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 | 1 | $this->translator = $translator; |
|
91 | 1 | $this->messagePhraseAdapter = $messagePhraseAdapter; |
|
92 | 1 | $this->titlePhraseAdapter = $titlePhraseAdapter; |
|
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function setMessage(string $message) : void |
||
99 | { |
||
100 | 1 | if ($this->isUnserialized()) { |
|
101 | 1 | $this->message = $message; |
|
102 | |||
103 | } else { |
||
104 | $this->messagePhraseAdapter->setMessage($message); |
||
105 | $this->message = NULL; |
||
106 | } |
||
107 | 1 | } |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getMessage() : string |
||
113 | { |
||
114 | 1 | if ($this->message === NULL && $this->translator) { |
|
115 | 1 | $this->message = $this->messagePhraseAdapter->translate($this->translator); |
|
116 | } |
||
117 | |||
118 | 1 | return $this->message; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function setLevel(string $level) : void |
||
125 | { |
||
126 | 1 | $this->level = $level; |
|
127 | 1 | } |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function getLevel() : string |
||
133 | { |
||
134 | 1 | return $this->level; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function info() : void |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function success() : void |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function warning() : void |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function error() : void |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function setTitle(string $title = NULL) : void |
||
173 | { |
||
174 | 1 | if ($this->isUnserialized()) { |
|
175 | 1 | $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 | 1 | } |
|
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function getTitle() : ?string |
||
189 | { |
||
190 | 1 | if ($this->title === NULL && $this->translator && $this->titlePhraseAdapter instanceof Adapters\IPhraseAdapter) { |
|
191 | 1 | $this->title = $this->titlePhraseAdapter->translate($this->translator); |
|
192 | } |
||
193 | |||
194 | 1 | return $this->title; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function setOverlay(bool $overlay) : void |
||
201 | { |
||
202 | 1 | $this->overlay = $overlay; |
|
203 | 1 | } |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function hasOverlay() : bool |
||
209 | { |
||
210 | 1 | return $this->overlay; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function setParameters(array $parameter) : void |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function setCount(int $count) : void |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function setDisplayed(bool $displayed = TRUE) : void |
||
237 | { |
||
238 | 1 | $this->displayed = $displayed; |
|
239 | 1 | } |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function isDisplayed() : bool |
||
245 | { |
||
246 | 1 | 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 |
||
262 | |||
263 | /** |
||
264 | * @return bool |
||
265 | */ |
||
266 | private function isUnserialized() : bool |
||
267 | { |
||
268 | 1 | return $this->translator === NULL; |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * @return string |
||
273 | */ |
||
274 | public function __toString() |
||
275 | { |
||
276 | 1 | return $this->level . ' ' . ($this->title ? $this->title . ' ' : '') . $this->getMessage(); |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return array |
||
281 | */ |
||
282 | public function __sleep() |
||
288 | } |
||
289 |