Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
Message | |
0.00% |
0 / 16 |
|
0.00% |
0 / 9 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isInline | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHeading | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIconClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHtml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Message.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system for Wikimedia projects. |
6 | * It contains the definition and implementation of the `Message` class, responsible for managing |
7 | * the behavior and properties of the corresponding component. |
8 | * |
9 | * @category Component |
10 | * @package Codex\Component |
11 | * @since 0.1.0 |
12 | * @author Doğu Abaris <abaris@null.net> |
13 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
14 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
15 | */ |
16 | |
17 | namespace Wikimedia\Codex\Component; |
18 | |
19 | use Wikimedia\Codex\Renderer\MessageRenderer; |
20 | |
21 | /** |
22 | * Message |
23 | * |
24 | * This class is part of the Codex PHP library and is responsible for |
25 | * representing an immutable object. It is primarily intended for use |
26 | * with a builder class to construct its instances. |
27 | * |
28 | * @category Component |
29 | * @package Codex\Component |
30 | * @since 0.1.0 |
31 | * @author Doğu Abaris <abaris@null.net> |
32 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
33 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
34 | */ |
35 | class Message { |
36 | |
37 | /** |
38 | * The ID for the Message. |
39 | */ |
40 | private string $id; |
41 | |
42 | /** |
43 | * The content displayed inside the message box. |
44 | */ |
45 | private string $content; |
46 | |
47 | /** |
48 | * The type of the message (e.g., 'notice', 'warning', 'error', 'success'). |
49 | */ |
50 | private string $type; |
51 | |
52 | /** |
53 | * Whether the message box should be displayed inline. |
54 | */ |
55 | private bool $inline; |
56 | |
57 | /** |
58 | * The heading displayed at the top of the message content. |
59 | */ |
60 | private string $heading; |
61 | |
62 | /** |
63 | * The CSS class name for the icon. |
64 | */ |
65 | private string $iconClass; |
66 | |
67 | /** |
68 | * Additional HTML attributes for the message box. |
69 | */ |
70 | private array $attributes; |
71 | |
72 | /** |
73 | * The renderer instance used to render the message. |
74 | */ |
75 | private MessageRenderer $renderer; |
76 | |
77 | /** |
78 | * Constructor for the Message component. |
79 | * |
80 | * Initializes a Message instance with the specified properties. |
81 | * |
82 | * @param string $id The ID for the Message. |
83 | * @param string $content The content displayed inside the message box. |
84 | * @param string $type The type of the message. |
85 | * @param bool $inline Whether the message box should be displayed inline. |
86 | * @param string $heading The heading displayed at the top of the message content. |
87 | * @param string $iconClass The CSS class name for the icon. |
88 | * @param array $attributes Additional HTML attributes for the message box. |
89 | * @param MessageRenderer $renderer The renderer to use for rendering the message. |
90 | */ |
91 | public function __construct( |
92 | string $id, |
93 | string $content, |
94 | string $type, |
95 | bool $inline, |
96 | string $heading, |
97 | string $iconClass, |
98 | array $attributes, |
99 | MessageRenderer $renderer |
100 | ) { |
101 | $this->id = $id; |
102 | $this->content = $content; |
103 | $this->type = $type; |
104 | $this->inline = $inline; |
105 | $this->heading = $heading; |
106 | $this->iconClass = $iconClass; |
107 | $this->attributes = $attributes; |
108 | $this->renderer = $renderer; |
109 | } |
110 | |
111 | /** |
112 | * Get the Message's HTML ID attribute. |
113 | * |
114 | * This method returns the ID that is assigned to the Message element. |
115 | * The ID can be used for targeting the message with JavaScript, CSS, or for accessibility purposes. |
116 | * |
117 | * @since 0.1.0 |
118 | * @return string The ID of the Message element. |
119 | */ |
120 | public function getId(): string { |
121 | return $this->id; |
122 | } |
123 | |
124 | /** |
125 | * Get the content of the message box. |
126 | * |
127 | * This method returns the text or HTML content that is displayed inside the message box. |
128 | * The content provides the primary feedback or information that the message conveys to the user. |
129 | * |
130 | * @since 0.1.0 |
131 | * @return string The content of the message box. |
132 | */ |
133 | public function getContent(): string { |
134 | return $this->content; |
135 | } |
136 | |
137 | /** |
138 | * Get the type of the message box. |
139 | * |
140 | * This method returns the type of the message, which determines its visual style. |
141 | * The type can be one of the following: 'notice', 'warning', 'error', 'success'. |
142 | * |
143 | * @since 0.1.0 |
144 | * @return string The type of the message box. |
145 | */ |
146 | public function getType(): string { |
147 | return $this->type; |
148 | } |
149 | |
150 | /** |
151 | * Check if the message box is displayed inline. |
152 | * |
153 | * This method returns a boolean indicating whether the message box is displayed inline, |
154 | * without additional padding, background color, or border. |
155 | * |
156 | * @since 0.1.0 |
157 | * @return bool True if the message box is displayed inline, false otherwise. |
158 | */ |
159 | public function isInline(): bool { |
160 | return $this->inline; |
161 | } |
162 | |
163 | /** |
164 | * Get the heading of the message box. |
165 | * |
166 | * This method returns the heading text that is prominently displayed at the top of the message content. |
167 | * The heading helps to quickly convey the primary purpose or topic of the message. |
168 | * |
169 | * @since 0.1.0 |
170 | * @return string The heading text of the message box. |
171 | */ |
172 | public function getHeading(): string { |
173 | return $this->heading; |
174 | } |
175 | |
176 | /** |
177 | * Get the CSS class name for the icon. |
178 | * |
179 | * This method returns the CSS class name for the icon displayed in the message box, |
180 | * enhancing the visual representation of the message. |
181 | * |
182 | * @since 0.1.0 |
183 | * @return string The CSS class name for the icon. |
184 | */ |
185 | public function getIconClass(): string { |
186 | return $this->iconClass; |
187 | } |
188 | |
189 | /** |
190 | * Get the additional HTML attributes for the message box. |
191 | * |
192 | * This method returns an associative array of additional HTML attributes that are applied |
193 | * to the outer `<div>` element of the message box. These attributes can be used to enhance |
194 | * accessibility or integrate with JavaScript. |
195 | * |
196 | * @since 0.1.0 |
197 | * @return array The additional attributes as an array. |
198 | */ |
199 | public function getAttributes(): array { |
200 | return $this->attributes; |
201 | } |
202 | |
203 | /** |
204 | * Get the component's HTML representation. |
205 | * |
206 | * This method generates the HTML markup for the component, incorporating relevant properties |
207 | * and any additional attributes. The component is structured using appropriate HTML elements |
208 | * as defined by the implementation. |
209 | * |
210 | * @since 0.1.0 |
211 | * @return string The generated HTML string for the component. |
212 | */ |
213 | public function getHtml(): string { |
214 | return $this->renderer->render( $this ); |
215 | } |
216 | } |