1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bulma; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
use Yiisoft\Html\Tag\Button; |
10
|
|
|
use Yiisoft\Html\Tag\Div; |
11
|
|
|
use Yiisoft\Html\Tag\P; |
12
|
|
|
use Yiisoft\Html\Tag\Span; |
13
|
|
|
use Yiisoft\Widget\Widget; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Message renders Bulma message component. |
17
|
|
|
* |
18
|
|
|
* For example, |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* <?= Message::widget()->headerColor('success')->header('System info')->body('Say hello...') ?> |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* @link https://bulma.io/documentation/components/message/ |
25
|
|
|
*/ |
26
|
|
|
final class Message extends Widget |
27
|
|
|
{ |
28
|
|
|
private const COLORS = ['is-primary', 'is-link', 'is-info', 'is-success', 'is-warning', 'is-danger', 'is-dark']; |
29
|
|
|
private const SIZES = ['is-small', 'is-medium', 'is-large']; |
30
|
|
|
private array $attributes = []; |
31
|
|
|
private string $autoIdPrefix = 'w'; |
32
|
|
|
private string $body = ''; |
33
|
|
|
private array $bodyAttributes = []; |
34
|
|
|
private array $buttonSpanAttributes = []; |
35
|
|
|
private string $buttonSpanAriaHidden = 'true'; |
36
|
|
|
private string $buttonCssClass = 'delete'; |
37
|
|
|
private array $closeButtonAttributes = []; |
38
|
|
|
private bool $encode = false; |
39
|
|
|
private array $headerAttributes = []; |
40
|
|
|
private string $headerColor = 'is-dark'; |
41
|
|
|
private string $headerMessage = ''; |
42
|
|
|
private string $messageBodyCssClass = 'message-body'; |
43
|
|
|
private string $messageCssClass = 'message'; |
44
|
|
|
private string $messageHeaderMessageCssClass = 'message-header'; |
45
|
|
|
private string $size = ''; |
46
|
|
|
private bool $unclosedButton = false; |
47
|
|
|
private bool $withoutHeader = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The HTML attributes. The following special options are recognized. |
51
|
|
|
* |
52
|
|
|
* @param array $values Attribute values indexed by attribute names. |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
* |
56
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
57
|
|
|
*/ |
58
|
2 |
|
public function attributes(array $values): self |
59
|
|
|
{ |
60
|
2 |
|
$new = clone $this; |
61
|
2 |
|
$new->attributes = $values; |
62
|
2 |
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a new instance with the specified prefix to the automatically generated widget IDs. |
67
|
|
|
* |
68
|
|
|
* @param string $value The prefix to the automatically generated widget IDs. |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
1 |
|
public function autoIdPrefix(string $value): self |
73
|
|
|
{ |
74
|
1 |
|
$new = clone $this; |
75
|
1 |
|
$new->autoIdPrefix = $value; |
76
|
1 |
|
return $new; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The body content in the message component. Message widget will also be treated as the body content, and will be |
81
|
|
|
* rendered before this. |
82
|
|
|
* |
83
|
|
|
* @param string $value The body content in the message component. |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
12 |
|
public function body(string $value): self |
88
|
|
|
{ |
89
|
12 |
|
$new = clone $this; |
90
|
12 |
|
$new->body = $value; |
91
|
12 |
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The HTML attributes for the widget body tag. |
96
|
|
|
* |
97
|
|
|
* @param array $values Attribute values indexed by attribute names. |
98
|
|
|
* |
99
|
|
|
* @return self |
100
|
|
|
* |
101
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
102
|
|
|
*/ |
103
|
2 |
|
public function bodyAttributes(array $values): self |
104
|
|
|
{ |
105
|
2 |
|
$new = clone $this; |
106
|
2 |
|
$new->bodyAttributes = $values; |
107
|
2 |
|
return $new; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The attributes for rendering the close button tag. |
112
|
|
|
* |
113
|
|
|
* The close button is displayed in the header of the modal window. Clicking on the button will hide the modal |
114
|
|
|
* window. If {@see unclosedButton} is false, no close button will be rendered. |
115
|
|
|
* |
116
|
|
|
* @param array $values Attribute values indexed by attribute names. |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
* |
120
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
121
|
|
|
*/ |
122
|
2 |
|
public function closeButtonAttributes(array $values): self |
123
|
|
|
{ |
124
|
2 |
|
$new = clone $this; |
125
|
2 |
|
$new->closeButtonAttributes = $values; |
126
|
2 |
|
return $new; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set encode to true to encode the output. |
131
|
|
|
* |
132
|
|
|
* @param bool $value whether to encode the output. |
133
|
|
|
* |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
1 |
|
public function encode(bool $value): self |
137
|
|
|
{ |
138
|
1 |
|
$new = clone $this; |
139
|
1 |
|
$new->encode = $value; |
140
|
1 |
|
return $new; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set color header message. |
145
|
|
|
* |
146
|
|
|
* @param string $value setting default 'is-dark'. Possible values: 'is-primary', 'is-info', 'is-success', |
147
|
|
|
* 'is-link', 'is-warning', 'is-danger'. |
148
|
|
|
* |
149
|
|
|
* @return self |
150
|
|
|
* |
151
|
|
|
* @link https://bulma.io/documentation/components/message/#colors |
152
|
|
|
*/ |
153
|
3 |
|
public function headerColor(string $value): self |
154
|
|
|
{ |
155
|
3 |
|
if (!in_array($value, self::COLORS)) { |
156
|
1 |
|
$values = implode(' ', self::COLORS); |
157
|
1 |
|
throw new InvalidArgumentException("Invalid color. Valid values are: $values."); |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
$new = clone $this; |
161
|
2 |
|
$new->headerColor = $value; |
162
|
2 |
|
return $new; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* The header message in the message component. Message widget will also be treated as the header content, and will |
167
|
|
|
* be rendered before body. |
168
|
|
|
* |
169
|
|
|
* @param string $value The header message. |
170
|
|
|
* |
171
|
|
|
* @return self |
172
|
|
|
*/ |
173
|
12 |
|
public function headerMessage(string $value): self |
174
|
|
|
{ |
175
|
12 |
|
$new = clone $this; |
176
|
12 |
|
$new->headerMessage = $value; |
177
|
12 |
|
return $new; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* The HTML attributes for the widget header tag. |
182
|
|
|
* |
183
|
|
|
* @param array $values Attribute values indexed by attribute names. |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
* |
187
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
188
|
|
|
*/ |
189
|
2 |
|
public function headerAttributes(array $values): self |
190
|
|
|
{ |
191
|
2 |
|
$new = clone $this; |
192
|
2 |
|
$new->headerAttributes = $values; |
193
|
2 |
|
return $new; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns a new instance with the specified ID of the widget. |
198
|
|
|
* |
199
|
|
|
* @param string $value The ID of the widget. |
200
|
|
|
* |
201
|
|
|
* @return self |
202
|
|
|
*/ |
203
|
2 |
|
public function id(string $value): self |
204
|
|
|
{ |
205
|
2 |
|
$new = clone $this; |
206
|
2 |
|
$new->attributes['id'] = $value; |
207
|
2 |
|
return $new; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set size config widgets. |
212
|
|
|
* |
213
|
|
|
* @param string $value size class. |
214
|
|
|
* |
215
|
|
|
* @return self |
216
|
|
|
* |
217
|
|
|
* @link https://bulma.io/documentation/components/message/#sizes |
218
|
|
|
*/ |
219
|
3 |
|
public function size(string $value): self |
220
|
|
|
{ |
221
|
3 |
|
if (!in_array($value, self::SIZES)) { |
222
|
1 |
|
$values = implode(' ', self::SIZES); |
223
|
1 |
|
throw new InvalidArgumentException("Invalid size. Valid values are: $values."); |
224
|
|
|
} |
225
|
|
|
|
226
|
2 |
|
$new = clone $this; |
227
|
2 |
|
$new->size = $value; |
228
|
2 |
|
return $new; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Allows you to disable close button message widget. |
233
|
|
|
* |
234
|
|
|
* @return self |
235
|
|
|
*/ |
236
|
2 |
|
public function unclosedButton(): self |
237
|
|
|
{ |
238
|
2 |
|
$new = clone $this; |
239
|
2 |
|
$new->unclosedButton = true; |
240
|
2 |
|
return $new; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Allows you to disable header widget. |
245
|
|
|
* |
246
|
|
|
* @return self |
247
|
|
|
* |
248
|
|
|
* @link https://bulma.io/documentation/components/message/#message-body-only |
249
|
|
|
*/ |
250
|
2 |
|
public function withoutHeader(): self |
251
|
|
|
{ |
252
|
2 |
|
$new = clone $this; |
253
|
2 |
|
$new->withoutHeader = false; |
254
|
2 |
|
return $new; |
255
|
|
|
} |
256
|
|
|
|
257
|
11 |
|
protected function run(): string |
258
|
|
|
{ |
259
|
11 |
|
return $this->renderMessage(); |
260
|
|
|
} |
261
|
|
|
|
262
|
11 |
|
private function renderCloseButton(): string |
263
|
|
|
{ |
264
|
11 |
|
$html = ''; |
265
|
|
|
|
266
|
11 |
|
$buttonSpanAttributes = $this->buttonSpanAttributes; |
267
|
11 |
|
$closeButtonAttributes = $this->closeButtonAttributes; |
268
|
|
|
|
269
|
11 |
|
if ($this->unclosedButton === true) { |
270
|
1 |
|
return $html; |
271
|
|
|
} |
272
|
|
|
|
273
|
10 |
|
$buttonSpanAttributes['aria-hidden'] = $this->buttonSpanAriaHidden; |
274
|
10 |
|
$closeButtonAttributes['type'] = 'button'; |
275
|
|
|
|
276
|
10 |
|
Html::addCssClass($closeButtonAttributes, $this->buttonCssClass); |
277
|
10 |
|
unset($closeButtonAttributes['label']); |
278
|
|
|
|
279
|
10 |
|
$label = Span::tag()->attributes($buttonSpanAttributes)->content('×')->encode(false)->render(); |
280
|
|
|
|
281
|
10 |
|
if ($this->size !== '') { |
282
|
1 |
|
Html::addCssClass($closeButtonAttributes, $this->size); |
283
|
|
|
} |
284
|
|
|
|
285
|
10 |
|
return Button::tag()->attributes($closeButtonAttributes)->content($label)->encode(false)->render() . PHP_EOL; |
286
|
|
|
} |
287
|
|
|
|
288
|
11 |
|
private function renderHeader(): string |
289
|
|
|
{ |
290
|
11 |
|
$html = ''; |
291
|
|
|
|
292
|
11 |
|
$headerAttributes = $this->headerAttributes; |
293
|
11 |
|
$headerMessage = $this->headerMessage; |
294
|
|
|
|
295
|
11 |
|
Html::addCssClass($headerAttributes, $this->messageHeaderMessageCssClass); |
296
|
|
|
|
297
|
11 |
|
$renderCloseButton = $this->renderCloseButton(); |
298
|
|
|
|
299
|
11 |
|
if ($this->encode) { |
300
|
1 |
|
$headerMessage = Html::encode($headerMessage); |
301
|
|
|
} |
302
|
|
|
|
303
|
11 |
|
if ($renderCloseButton !== '') { |
304
|
10 |
|
$headerMessage = PHP_EOL . P::tag()->content($headerMessage) . PHP_EOL . $renderCloseButton; |
305
|
|
|
} |
306
|
|
|
|
307
|
11 |
|
if ($this->withoutHeader) { |
308
|
10 |
|
$html = Div::tag() |
309
|
10 |
|
->attributes($headerAttributes) |
310
|
10 |
|
->content($headerMessage) |
311
|
10 |
|
->encode(false) |
312
|
10 |
|
->render() . PHP_EOL; |
313
|
|
|
} |
314
|
|
|
|
315
|
11 |
|
return $html; |
316
|
|
|
} |
317
|
|
|
|
318
|
11 |
|
private function renderMessage(): string |
319
|
|
|
{ |
320
|
11 |
|
$attributes = $this->attributes; |
321
|
|
|
|
322
|
|
|
/** @var string */ |
323
|
11 |
|
$id = $attributes['id'] ?? (Html::generateId($this->autoIdPrefix) . '-message'); |
324
|
11 |
|
unset($attributes['id']); |
325
|
|
|
|
326
|
11 |
|
Html::addCssClass($attributes, $this->messageCssClass); |
327
|
11 |
|
Html::addCssClass($attributes, $this->headerColor); |
328
|
|
|
|
329
|
11 |
|
if ($this->size !== '') { |
330
|
1 |
|
Html::addCssClass($attributes, $this->size); |
331
|
|
|
} |
332
|
|
|
|
333
|
11 |
|
return Div::tag() |
334
|
11 |
|
->attributes($attributes) |
335
|
11 |
|
->content(PHP_EOL . $this->renderHeader() . $this->renderMessageBody()) |
336
|
11 |
|
->encode(false) |
337
|
11 |
|
->id($id) |
338
|
11 |
|
->render(); |
339
|
|
|
} |
340
|
|
|
|
341
|
11 |
|
private function renderMessageBody(): string |
342
|
|
|
{ |
343
|
11 |
|
$body = $this->body; |
344
|
11 |
|
$bodyAttributes = $this->bodyAttributes; |
345
|
|
|
|
346
|
11 |
|
Html::addCssClass($bodyAttributes, $this->messageBodyCssClass); |
347
|
|
|
|
348
|
11 |
|
if ($this->encode) { |
349
|
1 |
|
$body = Html::encode($body); |
350
|
|
|
} |
351
|
|
|
|
352
|
11 |
|
if ($body !== '') { |
353
|
11 |
|
$body = PHP_EOL . $body . PHP_EOL; |
354
|
|
|
} |
355
|
|
|
|
356
|
11 |
|
return Div::tag()->attributes($bodyAttributes)->content($body)->encode(false)->render() . PHP_EOL; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: