[!!!][FEATURE] Refactoring of FlashMessage rendering
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / AbstractMessage.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Messaging;
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 use TYPO3\CMS\Core\Utility\MathUtility;
19
20 /**
21 * A class used for any kind of messages.
22 */
23 abstract class AbstractMessage
24 {
25 const NOTICE = -2;
26 const INFO = -1;
27 const OK = 0;
28 const WARNING = 1;
29 const ERROR = 2;
30
31 /**
32 * The message's title
33 *
34 * @var string
35 */
36 protected $title = '';
37
38 /**
39 * The message
40 *
41 * @var string
42 */
43 protected $message = '';
44
45 /**
46 * The message's severity
47 *
48 * @var int
49 */
50 protected $severity = self::OK;
51
52 /**
53 * Gets the message's title.
54 *
55 * @return string The message's title.
56 */
57 public function getTitle() : string
58 {
59 return $this->title;
60 }
61
62 /**
63 * Sets the message's title
64 *
65 * @param string $title The message's title
66 * @return void
67 */
68 public function setTitle(string $title)
69 {
70 $this->title = $title;
71 }
72
73 /**
74 * Gets the message.
75 *
76 * @return string The message.
77 */
78 public function getMessage() : string
79 {
80 return $this->message;
81 }
82
83 /**
84 * Sets the message
85 *
86 * @param string $message The message
87 * @return void
88 */
89 public function setMessage(string $message)
90 {
91 $this->message = $message;
92 }
93
94 /**
95 * Gets the message' severity.
96 *
97 * @return int The message' severity, must be one of AbstractMessage::INFO or similar contstants
98 */
99 public function getSeverity() : int
100 {
101 return $this->severity;
102 }
103
104 /**
105 * Sets the message' severity
106 *
107 * @param int $severity The severity, must be one of AbstractMessage::INFO or similar constants
108 * @return void
109 */
110 public function setSeverity(int $severity = self::OK)
111 {
112 $this->severity = MathUtility::forceIntegerInRange($severity, self::NOTICE, self::ERROR, self::OK);
113 }
114
115 /**
116 * Creates a string representation of the message. Useful for command
117 * line use.
118 *
119 * @return string A string representation of the message.
120 */
121 public function __toString()
122 {
123 $severities = [
124 self::NOTICE => 'NOTICE',
125 self::INFO => 'INFO',
126 self::OK => 'OK',
127 self::WARNING => 'WARNING',
128 self::ERROR => 'ERROR'
129 ];
130 $title = '';
131 if ($this->title !== '') {
132 $title = ' - ' . $this->title;
133 }
134 return $severities[$this->severity] . $title . ': ' . $this->message;
135 }
136 }