Passed
Push — master ( aedd55...0c8c47 )
by Alexander
15:54 queued 14:03
created

Message::bodyOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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