Test Failed
Pull Request — master (#35)
by Wilmer
02:18
created

ButtonGroup::buttons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Factory\Exceptions\InvalidConfigException;
9
use Yiisoft\Html\Html;
10
11
use function implode;
12
use function is_array;
13
14
/**
15
 * ButtonGroup renders a button group bootstrap component.
16
 *
17
 * For example,
18
 *
19
 * ```php
20
 * // a button group with items configuration
21
 * echo ButtonGroup::widget()
22
 *     ->withButtons([
23
 *         ['label' => 'A'],
24
 *         ['label' => 'B'],
25
 *         ['label' => 'C', 'visible' => false],
26
 *     ]);
27
 *
28
 * // button group with an item as a string
29
 * echo ButtonGroup::widget()
30
 *     ->withButtons([
31
 *         Button::widget()
32
 *             ->label('A'),
33
 *         ['label' => 'B'],
34
 *     ]);
35
 * ```
36
 *
37
 * Pressing on the button should be handled via JavaScript. See the following for details:
38
 */
39
final class ButtonGroup extends Widget
40
{
41
    private array $buttons = [];
42
    private bool $encodeLabels = true;
43
    private bool $encodeTags = false;
44
    private array $options = [];
45 5
46
    public function run(): string
47 5
    {
48 5
        if (!isset($this->options['id'])) {
49
            $this->options['id'] = "{$this->getId()}-button-group";
50
        }
51
52 5
        /** @psalm-suppress InvalidArgument */
53
        Html::addCssClass($this->options, ['widget' => 'btn-group']);
54 5
55 5
        if (!isset($this->options['role'])) {
56
            $this->options['role'] = 'group';
57
        }
58 5
59
        if ($this->encodeTags === false) {
60
            $this->options = array_merge($this->options, ['encode' => false]);
61
        }
62
63
        return Html::div($this->renderButtons(), $this->options);
64
    }
65
66
    /**
67
     * List of buttons. Each array element represents a single button which can be specified as a string or an array of
68 5
     * the following structure:
69
     *
70 5
     * - label: string, required, the button label.
71
     * - options: array, optional, the HTML attributes of the button.
72 5
     * - visible: bool, optional, whether this button is visible. Defaults to true.
73 5
     *
74 5
     * @param array $value
75
     *
76 5
     * @return $this
77 1
     */
78
    public function withButtons(array $value): self
79
    {
80 5
        $new = clone $this;
81 5
        $new->buttons = $value;
82
83
        return $new;
84 5
    }
85 4
86
    /**
87
     * Whether to HTML-encode the button labels.
88 5
     *
89 5
     * @param bool $value
90 5
     *
91 5
     * @return $this
92 5
     */
93
    public function withEncodeLabels(bool $value): self
94 1
    {
95
        $new = clone $this;
96
        $new->encodeLabels = $value;
97
98 5
        return $new;
99
    }
100
101
    /**
102
     * The HTML attributes for the widget container tag. The following special options are recognized.
103
     *
104
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
105
     *
106
     * @param array $value
107
     *
108
     * @return $this
109
     */
110
    public function withOptions(array $value): self
111
    {
112
        $new = clone $this;
113 5
        $new->options = $value;
114
115 5
        return $new;
116
    }
117 5
118
    /**
119
     * Allows you to enable or disable the encoding tags html.
120
     *
121
     * @param bool $value
122
     *
123
     * @return self
124
     */
125
    public function withEncodeTags(bool $value): self
126
    {
127
        $new = clone $this;
128
        $new->encodeTags = $value;
129
130
        return $new;
131
    }
132
133
    /**
134
     * Generates the buttons that compound the group as specified on {@see buttons}.
135
     *
136
     * @throws InvalidConfigException
137
     *
138
     * @return string the rendering result.
139
     */
140
    private function renderButtons(): string
141
    {
142
        $buttons = [];
143 3
144
        foreach ($this->buttons as $button) {
145 3
            if (is_array($button)) {
146
                $visible = ArrayHelper::remove($button, 'visible', true);
147 3
148
                if ($visible === false) {
149
                    continue;
150
                }
151
152
                if (!isset($button['encodeLabel'])) {
153
                    $button['encodeLabel'] = $this->encodeLabels;
154
                }
155
156
                if (!isset($button['options']['type'])) {
157
                    ArrayHelper::setValueByPath($button, 'options.type', 'button');
158
                }
159
160
                $buttons[] = Button::widget()
161
                    ->withEncodeLabels($button['encodeLabel'])
0 ignored issues
show
Bug introduced by
The method withEncodeLabels() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Yii\Bootstrap5\Dropdown or Yiisoft\Yii\Bootstrap5\ButtonGroup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
                    ->/** @scrutinizer ignore-call */ withEncodeLabels($button['encodeLabel'])
Loading history...
162
                    ->withLabel($button['label'])
163
                    ->withOptions($button['options'])
164
                    ->render();
165
            } else {
166
                $buttons[] = $button;
167
            }
168
        }
169
170
        return implode("\n", $buttons);
171
    }
172
}
173