1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Core\Messaging; |
19
|
|
|
|
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A class representing flash messages. |
24
|
|
|
*/ |
25
|
|
|
class FlashMessage extends AbstractMessage |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Defines whether the message should be stored in the session (to survive redirects) or only for one request (default) |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $storeInSession = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor for a flash message |
36
|
|
|
* |
37
|
|
|
* @param string $message The message. |
38
|
|
|
* @param string $title Optional message title. |
39
|
|
|
* @param int $severity Optional severity, must be either of one of \TYPO3\CMS\Core\Messaging\FlashMessage constants |
40
|
|
|
* @param bool $storeInSession Optional, defines whether the message should be stored in the session or only for one request (default) |
41
|
|
|
*/ |
42
|
|
|
public function __construct($message, $title = '', $severity = self::OK, $storeInSession = false) |
43
|
|
|
{ |
44
|
|
|
$this->setMessage($message); |
45
|
|
|
$this->setTitle($title); |
46
|
|
|
$this->setSeverity($severity); |
47
|
|
|
$this->setStoreInSession($storeInSession); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Factory method. Useful when creating flash messages from a jsonSerialize json_decode() call. |
52
|
|
|
* |
53
|
|
|
* @param array<string, string|int|bool> $data |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public static function createFromArray(array $data): self |
57
|
|
|
{ |
58
|
|
|
return GeneralUtility::makeInstance( |
59
|
|
|
static::class, |
60
|
|
|
(string)$data['message'], |
61
|
|
|
(string)($data['title'] ?? ''), |
62
|
|
|
(int)($data['severity'] ?? AbstractMessage::OK), |
63
|
|
|
(bool)($data['storeInSession'] ?? false) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the message's storeInSession flag. |
69
|
|
|
* |
70
|
|
|
* @return bool TRUE if message should be stored in the session, otherwise FALSE. |
71
|
|
|
*/ |
72
|
|
|
public function isSessionMessage() |
73
|
|
|
{ |
74
|
|
|
return $this->storeInSession; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the message's storeInSession flag |
79
|
|
|
* |
80
|
|
|
* @param bool $storeInSession The persistence flag |
81
|
|
|
*/ |
82
|
|
|
public function setStoreInSession($storeInSession) |
83
|
|
|
{ |
84
|
|
|
$this->storeInSession = (bool)$storeInSession; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array Data which can be serialized by json_encode() |
89
|
|
|
*/ |
90
|
|
|
public function jsonSerialize(): array |
91
|
|
|
{ |
92
|
|
|
$data = parent::jsonSerialize(); |
93
|
|
|
$data['storeInSession'] = $this->storeInSession; |
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|