Passed
Push — master ( 73cd93...7a270d )
by Alexander
30:15 queued 27:55
created

Message::renderCloseButton()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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