d49e2f9015fce6ee20998e3b497c637579f25db9
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / AbstractMessage.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * A class used for any kind of messages.
19 */
20 abstract class AbstractMessage
21 {
22 const NOTICE = -2;
23 const INFO = -1;
24 const OK = 0;
25 const WARNING = 1;
26 const ERROR = 2;
27
28 /**
29 * The message's title
30 *
31 * @var string
32 */
33 protected $title = '';
34
35 /**
36 * The message
37 *
38 * @var string
39 */
40 protected $message = '';
41
42 /**
43 * The message's severity
44 *
45 * @var int
46 */
47 protected $severity = self::OK;
48
49 /**
50 * Gets the message's title.
51 *
52 * @return string The message's title.
53 */
54 public function getTitle()
55 {
56 return $this->title;
57 }
58
59 /**
60 * Sets the message's title
61 *
62 * @param string $title The message's title
63 * @return void
64 */
65 public function setTitle($title)
66 {
67 $this->title = (string)$title;
68 }
69
70 /**
71 * Gets the message.
72 *
73 * @return string The message.
74 */
75 public function getMessage()
76 {
77 return $this->message;
78 }
79
80 /**
81 * Sets the message
82 *
83 * @param string $message The message
84 * @return void
85 */
86 public function setMessage($message)
87 {
88 $this->message = (string)$message;
89 }
90
91 /**
92 * Gets the message' severity.
93 *
94 * @return int The message' severity, must be one of AbstractMessage::INFO or similar contstants
95 */
96 public function getSeverity()
97 {
98 return $this->severity;
99 }
100
101 /**
102 * Sets the message' severity
103 *
104 * @param int $severity The severity, must be one of AbstractMessage::INFO or similar constants
105 * @return void
106 */
107 public function setSeverity($severity = self::OK)
108 {
109 $this->severity = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($severity, self::NOTICE, self::ERROR, self::OK);
110 }
111
112 /**
113 * Creates a string representation of the message. Useful for command
114 * line use.
115 *
116 * @return string A string representation of the message.
117 */
118 public function __toString()
119 {
120 $severities = [
121 self::INFO => 'INFO',
122 self::OK => 'OK',
123 self::WARNING => 'WARNING',
124 self::ERROR => 'ERROR'
125 ];
126 $title = '';
127 if (!empty($this->title)) {
128 $title = ' - ' . $this->title;
129 }
130 return $severities[$this->severity] . $title . ': ' . $this->message;
131 }
132 }