Passed
Push — master ( d3891b...901043 )
by Alexander
02:19
created

Message::options()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Html\Html;
9
10
/**
11
 * Message renders Bulma message component.
12
 *
13
 * For example,
14
 *
15
 * ```php
16
 * <?= Message::widget()
17
 *     ->headerColor('success')
18
 *     ->header('System info')
19
 *     ->body('Say hello...') ?>
20
 * ```
21
 *
22
 * @link https://bulma.io/documentation/components/message/
23
 */
24
final class Message extends Widget
25
{
26
    private string $body = '';
27
    private string $headerColor = 'is-dark';
28
    private string $headerMessage = '';
29
    private array $options = [];
30
    private array $bodyOptions = [];
31
    private array $closeButtonOptions = [];
32
    private array $headerOptions = [];
33
    private string $size = '';
34
    private bool $withoutCloseButton = false;
35
    private bool $withoutHeader = true;
36
37 11
    protected function run(): string
38
    {
39 11
        $this->buildOptions();
40
41
        return
42 11
            Html::beginTag('div', $this->options) . "\n" .
43 11
                $this->renderHeader() .
44 11
                Html::beginTag('div', $this->bodyOptions) . "\n" .
45 11
                    $this->renderBodyEnd() . "\n" .
46 11
                Html::endTag('div') . "\n" .
47 11
            Html::endTag('div');
48
    }
49
50
    /**
51
     * The body content in the message component. Message widget will also be treated as the body content, and will be
52
     * rendered before this.
53
     *
54
     * @param string $value
55
     *
56
     * @return self
57
     */
58 11
    public function body(string $value): self
59
    {
60 11
        $new = clone $this;
61 11
        $new->body = $value;
62 11
        return $new;
63
    }
64
65
    /**
66
     * Set color header message.
67
     *
68
     * @param string $value setting default 'is-dark', 'is-primary', 'is-link', 'is-info', 'is-success', 'is-warning',
69
     * 'is-danger'.
70
     *
71
     * @return self
72
     */
73 1
    public function headerColor(string $value): self
74
    {
75 1
        $new = clone $this;
76 1
        $new->headerColor = $value;
77 1
        return $new;
78
    }
79
80
    /**
81
     * The header message in the message component. Message widget will also be treated as the header content, and will
82
     * be rendered before body.
83
     *
84
     * @param string $value
85
     *
86
     * @return self
87
     */
88 11
    public function headerMessage(string $value): self
89
    {
90 11
        $new = clone $this;
91 11
        $new->headerMessage = $value;
92 11
        return $new;
93
    }
94
95
    /**
96
     * The HTML attributes for the widget container tag.
97
     *
98
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
99
     *
100
     * @param array $value
101
     *
102
     * @return self
103
     */
104 1
    public function options(array $value): self
105
    {
106 1
        $new = clone $this;
107 1
        $new->options = $value;
108 1
        return $new;
109
    }
110
111
    /**
112
     * The HTML attributes for the widget body tag.
113
     *
114
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
115
     *
116
     * @param array $value
117
     *
118
     * @return self
119
     */
120 1
    public function bodyOptions(array $value): self
121
    {
122 1
        $new = clone $this;
123 1
        $new->bodyOptions = $value;
124 1
        return $new;
125
    }
126
127
    /**
128
     * The options for rendering the close button tag.
129
     *
130
     * The close button is displayed in the header of the modal window. Clicking on the button will hide the modal
131
     * window. If {@see withoutCloseButton} is false, no close button will be rendered.
132
     *
133
     * @param array $value
134
     *
135
     * @return self
136
     */
137 1
    public function closeButtonOptions(array $value): self
138
    {
139 1
        $new = clone $this;
140 1
        $new->closeButtonOptions = $value;
141 1
        return $new;
142
    }
143
144
    /**
145
     * The HTML attributes for the widget header tag.
146
     *
147
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
148
     *
149
     * @param array $value
150
     *
151
     * @return self
152
     */
153 1
    public function headerOptions(array $value): self
154
    {
155 1
        $new = clone $this;
156 1
        $new->headerOptions = $value;
157 1
        return $new;
158
    }
159
160
    /**
161
     * Set size message widget.
162
     *
163
     * @param string $value default setting empty normal, 'is-small', 'is-medium', 'is-large'.
164
     *
165
     * @return self
166
     */
167 1
    public function size(string $value): self
168
    {
169 1
        $new = clone $this;
170 1
        $new->size = $value;
171 1
        return $new;
172
    }
173
174
    /**
175
     * Allows you to disable close button message widget.
176
     *
177
     * @param bool $value
178
     *
179
     * @return self
180
     */
181 1
    public function withoutCloseButton(bool $value): self
182
    {
183 1
        $new = clone $this;
184 1
        $new->withoutCloseButton = $value;
185 1
        return $new;
186
    }
187
188
    /**
189
     * Allows you to disable header widget.
190
     *
191
     * @param bool $value
192
     *
193
     * @return self
194
     */
195 1
    public function withoutHeader(bool $value): self
196
    {
197 1
        $new = clone $this;
198 1
        $new->withoutHeader = $value;
199 1
        return $new;
200
    }
201
202 11
    private function buildOptions(): void
203
    {
204 11
        if (!isset($this->options['id'])) {
205 11
            $this->options['id'] = "{$this->getId()}-message";
206
        }
207
208 11
        $this->options = $this->addOptions($this->options, 'message');
209
210 11
        Html::addCssClass($this->options, $this->headerColor);
211
212 11
        if ($this->size !== '') {
213 1
            Html::addCssClass($this->options, $this->size);
214
        }
215
216 11
        $this->bodyOptions = $this->addOptions($this->bodyOptions, 'message-body');
217 11
        $this->closeButtonOptions = $this->addOptions($this->closeButtonOptions, 'delete');
218 11
        $this->headerOptions = $this->addOptions($this->headerOptions, 'message-header');
219 11
    }
220
221 11
    private function renderHeader(): string
222
    {
223 11
        $html = '';
224
225 11
        if ($this->withoutHeader) {
226 10
            $html = Html::beginTag('div', $this->headerOptions) . "\n" . $this->renderHeaderMessage() . "\n" .
227 10
                Html::endTag('div') . "\n";
228
        }
229
230 11
        return $html;
231
    }
232
233 10
    private function renderHeaderMessage(): string
234
    {
235 10
        $result = $this->headerMessage;
236
237 10
        if ($this->renderCloseButton() !== null) {
238 9
            $result = '<p>' . $this->headerMessage . '</p>' . "\n" . $this->renderCloseButton();
239
        }
240
241 10
        return $result;
242
    }
243
244 11
    private function renderBodyEnd(): string
245
    {
246 11
        return $this->body;
247
    }
248
249 10
    private function renderCloseButton(): ?string
250
    {
251 10
        if ($this->withoutCloseButton === true) {
252 1
            return null;
253
        }
254
255 9
        $tag = ArrayHelper::remove($this->closeButtonOptions, 'tag', 'button');
256 9
        $label = ArrayHelper::remove($this->closeButtonOptions, 'label', Html::tag('span', '&times;', [
257 9
            'aria-hidden' => 'true'
258
        ]));
259
260 9
        if ($tag === 'button') {
261 9
            $this->closeButtonOptions['type'] = 'button';
262
        }
263
264 9
        if ($this->size !== '') {
265 1
            Html::addCssClass($this->closeButtonOptions, [$this->size]);
266
        }
267
268 9
        return Html::tag($tag, $label, $this->closeButtonOptions);
269
    }
270
}
271