Passed
Push — master ( d4ad0d...35f6e8 )
by
unknown
14:03
created

FlashMessage::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
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