Passed
Push — master ( 189602...938383 )
by Alexander
03:57 queued 01:44
created

Message::bodyAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Yii\Bulma\Widget. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
    public const SIZE_SMALL = 'is-small';
29
    public const SIZE_MEDIUM = 'is-medium';
30
    public const SIZE_LARGE = 'is-large';
31
    private const SIZE_ALL = [
32
        self::SIZE_SMALL,
33
        self::SIZE_MEDIUM,
34
        self::SIZE_LARGE,
35
    ];
36
    public const COLOR_PRIMARY = 'is-primary';
37
    public const COLOR_LINK = 'is-link';
38
    public const COLOR_INFO = 'is-info';
39
    public const COLOR_SUCCESS = 'is-success';
40
    public const COLOR_WARNING = 'is-warning';
41
    public const COLOR_DANGER = 'is-danger';
42
    public const COLOR_DARK = 'is-dark';
43
    private const COLOR_ALL = [
44
        self::COLOR_PRIMARY,
45
        self::COLOR_LINK,
46
        self::COLOR_INFO,
47
        self::COLOR_SUCCESS,
48
        self::COLOR_WARNING,
49
        self::COLOR_DANGER,
50
        self::COLOR_DARK,
51
    ];
52
    private array $attributes = [];
53
    private string $autoIdPrefix = 'w';
54
    private string $body = '';
55
    private array $bodyAttributes = [];
56
    private array $buttonSpanAttributes = [];
57
    private string $buttonSpanAriaHidden = 'true';
58
    private string $buttonCssClass = 'delete';
59
    private bool $closedButton = false;
60
    private array $closeButtonAttributes = [];
61
    private bool $encode = false;
62
    private array $headerAttributes = [];
63
    private string $headerColor = self::COLOR_DARK;
64
    private string $headerMessage = '';
65
    private string $messageBodyCssClass = 'message-body';
66
    private string $messageCssClass = 'message';
67
    private string $messageHeaderMessageCssClass = 'message-header';
68
    private string $size = '';
69
    private bool $withoutHeader = false;
70
71
    /**
72
     * The HTML attributes.
73
     *
74
     * @param array $values Attribute values indexed by attribute names.
75
     *
76
     * @return self
77
     *
78
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
79
     */
80 2
    public function attributes(array $values): self
81
    {
82 2
        $new = clone $this;
83 2
        $new->attributes = $values;
84 2
        return $new;
85
    }
86
87
    /**
88
     * Returns a new instance with the specified prefix to the automatically generated widget IDs.
89
     *
90
     * @param string $value The prefix to the automatically generated widget IDs.
91
     *
92
     * @return self
93
     */
94 1
    public function autoIdPrefix(string $value): self
95
    {
96 1
        $new = clone $this;
97 1
        $new->autoIdPrefix = $value;
98 1
        return $new;
99
    }
100
101
    /**
102
     * The body content.
103
     *
104
     * @param string $value The body content.
105
     *
106
     * @return self
107
     */
108 12
    public function body(string $value): self
109
    {
110 12
        $new = clone $this;
111 12
        $new->body = $value;
112 12
        return $new;
113
    }
114
115
    /**
116
     * The HTML attributes for the widget body tag.
117
     *
118
     * @param array $values Attribute values indexed by attribute names.
119
     *
120
     * @return self
121
     *
122
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
123
     */
124 2
    public function bodyAttributes(array $values): self
125
    {
126 2
        $new = clone $this;
127 2
        $new->bodyAttributes = $values;
128 2
        return $new;
129
    }
130
131
    /**
132
     * The attributes for rendering the close button tag.
133
     *
134
     * The close button is displayed in the header of the Message. Clicking on the button will hide the message.
135
     *
136
     * @param array $values Attribute values indexed by attribute names.
137
     *
138
     * @return self
139
     *
140
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
141
     */
142 2
    public function closeButtonAttributes(array $values): self
143
    {
144 2
        $new = clone $this;
145 2
        $new->closeButtonAttributes = $values;
146 2
        return $new;
147
    }
148
149
    /**
150
     * Set encode to true to encode the output.
151
     *
152
     * @param bool $value whether to encode the output.
153
     *
154
     * @return self
155
     */
156 1
    public function encode(bool $value): self
157
    {
158 1
        $new = clone $this;
159 1
        $new->encode = $value;
160 1
        return $new;
161
    }
162
163
    /**
164
     * Set message header color.
165
     *
166
     * @param string $value The header color. Default is Message::COLOR_DARK.
167
     * Possible values: Message::COLOR_PRIMARY, Message::COLOR_LINK, Message::COLOR_INFO, Message::COLOR_SUCCESS,
168
     * Message::COLOR_WARNING, Message::COLOR_DANGER, Message::COLOR_DARK.
169
     *
170
     * @return self
171
     *
172
     * @link https://bulma.io/documentation/components/message/#colors
173
     */
174 3
    public function headerColor(string $value): self
175
    {
176 3
        if (!in_array($value, self::COLOR_ALL, true)) {
177 1
            $values = implode('", "', self::COLOR_ALL);
178 1
            throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
179
        }
180
181 2
        $new = clone $this;
182 2
        $new->headerColor = $value;
183 2
        return $new;
184
    }
185
186
    /**
187
     * The header message.
188
     *
189
     * @param string $value The header message.
190
     *
191
     * @return self
192
     */
193 12
    public function headerMessage(string $value): self
194
    {
195 12
        $new = clone $this;
196 12
        $new->headerMessage = $value;
197 12
        return $new;
198
    }
199
200
    /**
201
     * The HTML attributes for the widget header tag.
202
     *
203
     * @param array $values Attribute values indexed by attribute names.
204
     *
205
     * @return self
206
     *
207
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
208
     */
209 2
    public function headerAttributes(array $values): self
210
    {
211 2
        $new = clone $this;
212 2
        $new->headerAttributes = $values;
213 2
        return $new;
214
    }
215
216
    /**
217
     * Returns a new instance with the specified ID of the widget.
218
     *
219
     * @param string $value The ID of the widget.
220
     *
221
     * @return self
222
     */
223 2
    public function id(string $value): self
224
    {
225 2
        $new = clone $this;
226 2
        $new->attributes['id'] = $value;
227 2
        return $new;
228
    }
229
230
    /**
231
     * Set size.
232
     *
233
     * @param string $value size class. By default, not class is added and the size is considered "normal".
234
     * Possible values: Message::SIZE_SMALL, Message::SIZE_MEDIUM, Message::SIZE_LARGE.
235
     *
236
     * @return self
237
     *
238
     * @link https://bulma.io/documentation/components/message/#sizes
239
     */
240 3
    public function size(string $value): self
241
    {
242 3
        if (!in_array($value, self::SIZE_ALL, true)) {
243 1
            $values = implode('", "', self::SIZE_ALL);
244 1
            throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\".");
245
        }
246
247 2
        $new = clone $this;
248 2
        $new->size = $value;
249 2
        return $new;
250
    }
251
252
    /**
253
     * Allows you to remove the close button.
254
     *
255
     * @param bool $value Whether to remove the close button.
256
     *
257
     * @return self
258
     */
259 2
    public function withoutCloseButton(bool $value): self
260
    {
261 2
        $new = clone $this;
262 2
        $new->closedButton = $value;
263 2
        return $new;
264
    }
265
266
    /**
267
     * Allows you to disable header.
268
     *
269
     * @param bool $value Whether to disable header.
270
     *
271
     * @return self
272
     *
273
     * @link https://bulma.io/documentation/components/message/#message-body-only
274
     */
275 2
    public function withoutHeader(bool $value): self
276
    {
277 2
        $new = clone $this;
278 2
        $new->withoutHeader = $value;
279 2
        return $new;
280
    }
281
282 11
    protected function run(): string
283
    {
284 11
        return $this->renderMessage();
285
    }
286
287 11
    private function renderCloseButton(): string
288
    {
289 11
        $html = '';
290
291 11
        $buttonSpanAttributes = $this->buttonSpanAttributes;
292 11
        $closeButtonAttributes = $this->closeButtonAttributes;
293
294 11
        if ($this->closedButton === true) {
295 1
            return $html;
296
        }
297
298 10
        $buttonSpanAttributes['aria-hidden'] = $this->buttonSpanAriaHidden;
299 10
        $closeButtonAttributes['type'] = 'button';
300
301 10
        Html::addCssClass($closeButtonAttributes, $this->buttonCssClass);
302 10
        unset($closeButtonAttributes['label']);
303
304 10
        $label = Span::tag()->attributes($buttonSpanAttributes)->content('&times;')->encode(false)->render();
305
306 10
        if ($this->size !== '') {
307 1
            Html::addCssClass($closeButtonAttributes, $this->size);
308
        }
309
310 10
        return Button::tag()->attributes($closeButtonAttributes)->content($label)->encode(false)->render() . PHP_EOL;
311
    }
312
313 11
    private function renderHeader(): string
314
    {
315 11
        $html = '';
316
317 11
        $headerAttributes = $this->headerAttributes;
318 11
        $headerMessage = $this->headerMessage;
319
320 11
        Html::addCssClass($headerAttributes, $this->messageHeaderMessageCssClass);
321
322 11
        $renderCloseButton = $this->renderCloseButton();
323
324 11
        if ($this->encode) {
325 1
            $headerMessage = Html::encode($headerMessage);
326
        }
327
328 11
        if ($renderCloseButton !== '') {
329 10
            $headerMessage = PHP_EOL . P::tag()->content($headerMessage) . PHP_EOL . $renderCloseButton;
330
        }
331
332 11
        if ($this->withoutHeader === false) {
333 10
            $html = Div::tag()
334 10
                ->attributes($headerAttributes)
335 10
                ->content($headerMessage)
336 10
                ->encode(false)
337 10
                ->render() . PHP_EOL;
338
        }
339
340 11
        return $html;
341
    }
342
343 11
    private function renderMessage(): string
344
    {
345 11
        $attributes = $this->attributes;
346
347
        /** @var string */
348 11
        $id = $attributes['id'] ?? (Html::generateId($this->autoIdPrefix) . '-message');
349 11
        unset($attributes['id']);
350
351 11
        Html::addCssClass($attributes, $this->messageCssClass);
352 11
        Html::addCssClass($attributes, $this->headerColor);
353
354 11
        if ($this->size !== '') {
355 1
            Html::addCssClass($attributes, $this->size);
356
        }
357
358 11
        return Div::tag()
359 11
            ->attributes($attributes)
360 11
            ->content(PHP_EOL . $this->renderHeader() . $this->renderMessageBody())
361 11
            ->encode(false)
362 11
            ->id($id)
363 11
            ->render();
364
    }
365
366 11
    private function renderMessageBody(): string
367
    {
368 11
        $body = $this->body;
369 11
        $bodyAttributes = $this->bodyAttributes;
370
371 11
        Html::addCssClass($bodyAttributes, $this->messageBodyCssClass);
372
373 11
        if ($this->encode) {
374 1
            $body = Html::encode($body);
375
        }
376
377 11
        if ($body !== '') {
378 11
            $body = PHP_EOL . $body . PHP_EOL;
379
        }
380
381 11
        return Div::tag()->attributes($bodyAttributes)->content($body)->encode(false)->render() . PHP_EOL;
382
    }
383
}
384