1<?php
2
3declare(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
18namespace TYPO3\CMS\Extbase\Error;
19
20/**
21 * An object representation of a generic message. Usually, you will use Error, Warning or Notice instead of this one.
22 */
23class Message
24{
25    /**
26     * The default (english) error message
27     *
28     * @var string
29     */
30    protected $message = 'Unknown message';
31
32    /**
33     * The error code
34     *
35     * @var int
36     */
37    protected $code;
38
39    /**
40     * The message arguments. Will be replaced in the message body.
41     *
42     * @var array
43     */
44    protected $arguments = [];
45
46    /**
47     * An optional title for the message (used eg. in flashMessages).
48     *
49     * @var string
50     */
51    protected $title = '';
52
53    /**
54     * Constructs this error
55     *
56     * @param string $message An english error message which is used if no other error message can be resolved
57     * @param int $code A unique error code
58     * @param array $arguments Array of arguments to be replaced in message
59     * @param string $title optional title for the message
60     */
61    public function __construct(string $message, int $code, array $arguments = [], string $title = '')
62    {
63        $this->message = $message;
64        $this->code = $code;
65        $this->arguments = $arguments;
66        $this->title = $title;
67    }
68
69    /**
70     * Returns the error message
71     *
72     * @return string The error message
73     */
74    public function getMessage(): string
75    {
76        return $this->message;
77    }
78
79    /**
80     * Returns the error code
81     *
82     * @return int The error code
83     */
84    public function getCode(): int
85    {
86        return $this->code;
87    }
88
89    /**
90     * Get arguments
91     *
92     * @return array
93     */
94    public function getArguments(): array
95    {
96        return $this->arguments;
97    }
98
99    /**
100     * Get title
101     *
102     * @return string
103     */
104    public function getTitle(): string
105    {
106        return $this->title;
107    }
108
109    /**
110     * Return the rendered message
111     *
112     * @return string
113     */
114    public function render(): string
115    {
116        if (count($this->arguments) > 0) {
117            return vsprintf($this->message, $this->arguments);
118        }
119        return $this->message;
120    }
121
122    /**
123     * Converts this error into a string
124     *
125     * @return string
126     */
127    public function __toString()
128    {
129        return $this->render();
130    }
131}
132