Passed
Pull Request — master (#90)
by
unknown
13:09
created

ButtonGroup   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 117
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 3
A buttons() 0 6 1
A withoutEncodeLabels() 0 6 1
A options() 0 6 1
B renderButtons() 0 35 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RedCatGirl\YiiBootstrap386;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Definitions\Exception\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
 *     ->buttons([
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
 *     ->buttons([
31
 *         Button::widget()->label('A'),
32
 *         ['label' => 'B'],
33
 *     ]);
34
 * ```
35
 *
36
 * Pressing on the button should be handled via JavaScript. See the following for details:
37
 */
38
final class ButtonGroup extends Widget
39
{
40
    private array $buttons = [];
41
    private bool $encodeLabels = true;
42
    private bool $encodeTags = false;
43
    private array $options = [];
44
45 7
    protected function run(): string
46
    {
47 7
        if (!isset($this->options['id'])) {
48 7
            $this->options['id'] = "{$this->getId()}-button-group";
49
        }
50
51
        /** @psalm-suppress InvalidArgument */
52 7
        Html::addCssClass($this->options, ['widget' => 'btn-group']);
53
54 7
        if (!isset($this->options['role'])) {
55 7
            $this->options['role'] = 'group';
56
        }
57
58 7
        return Html::div($this->renderButtons(), $this->options)
59 7
            ->encode($this->encodeTags)
60 7
            ->render();
61
    }
62
63
    /**
64
     * List of buttons. Each array element represents a single button which can be specified as a string or an array of
65
     * the following structure:
66
     *
67
     * - label: string, required, the button label.
68
     * - options: array, optional, the HTML attributes of the button.
69
     * - visible: bool, optional, whether this button is visible. Defaults to true.
70
     *
71
     * @param array $value
72
     *
73
     * @return self
74
     */
75 7
    public function buttons(array $value): self
76
    {
77 7
        $new = clone $this;
78 7
        $new->buttons = $value;
79
80 7
        return $new;
81
    }
82
83
    /**
84
     * When tags Labels HTML should not be encoded.
85
     *
86
     * @return self
87
     */
88 1
    public function withoutEncodeLabels(): self
89
    {
90 1
        $new = clone $this;
91 1
        $new->encodeLabels = false;
92
93 1
        return $new;
94
    }
95
96
    /**
97
     * The HTML attributes for the widget container tag. The following special options are recognized.
98
     *
99
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
100
     *
101
     * @param array $value
102
     *
103
     * @return self
104
     */
105 4
    public function options(array $value): self
106
    {
107 4
        $new = clone $this;
108 4
        $new->options = $value;
109
110 4
        return $new;
111
    }
112
113
    /**
114
     * Generates the buttons that compound the group as specified on {@see buttons}.
115
     *
116
     * @throws InvalidConfigException
117
     *
118
     * @return string the rendering result.
119
     */
120 7
    private function renderButtons(): string
121
    {
122 7
        $buttons = [];
123
124 7
        foreach ($this->buttons as $button) {
125 7
            if (is_array($button)) {
126 7
                $visible = ArrayHelper::remove($button, 'visible', true);
127
128 7
                if ($visible === false) {
129 1
                    continue;
130
                }
131
132 7
                if (!isset($button['encodeLabel'])) {
133 7
                    $button['encodeLabel'] = $this->encodeLabels;
134
                }
135
136 7
                if (!isset($button['options']['type'])) {
137 6
                    ArrayHelper::setValueByPath($button, 'options.type', 'button');
138
                }
139
140 7
                $buttonWidget = Button::widget()
141 7
                    ->label($button['label'])
0 ignored issues
show
Bug introduced by
The method label() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as RedCatGirl\YiiBootstrap386\Button or RedCatGirl\YiiBootstrap386\Progress or RedCatGirl\YiiBootstrap386\ButtonDropdown. ( Ignorable by Annotation )

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

141
                    ->/** @scrutinizer ignore-call */ label($button['label'])
Loading history...
142 7
                    ->options($button['options']);
143
144 7
                if ($button['encodeLabel'] === false) {
145 1
                    $buttonWidget = $buttonWidget->withoutEncodeLabels();
146
                }
147
148 7
                $buttons[] = $buttonWidget->render();
149
            } else {
150 1
                $buttons[] = $button;
151
            }
152
        }
153
154 7
        return implode("\n", $buttons);
155
    }
156
}
157