ButtonGroup   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 91.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 108
ccs 31
cts 34
cp 0.9118
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 5 1
A buttons() 0 5 1
A renderButtons() 0 31 6
A encodeLabels() 0 5 1
A run() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4;
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
 *     ->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()
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
class ButtonGroup extends Widget
40
{
41
    private array $buttons = [];
42
    private bool $encodeLabels = true;
43
    private array $options = [];
44
45 3
    protected function run(): string
46
    {
47 3
        if (!isset($this->options['id'])) {
48 3
            $this->options['id'] = "{$this->getId()}-button-group";
49
        }
50
51 3
        Html::addCssClass($this->options, ['widget' => 'btn-group']);
52
53 3
        if (!isset($this->options['role'])) {
54 3
            $this->options['role'] = 'group';
55
        }
56
57 3
        return Html::tag('div', $this->renderButtons(), $this->options);
58
    }
59
60
    /**
61
     * Generates the buttons that compound the group as specified on {@see buttons}.
62
     *
63
     * @throws InvalidConfigException
64
     *
65
     * @return string the rendering result.
66
     */
67 3
    protected function renderButtons(): string
68
    {
69 3
        $buttons = [];
70
71 3
        foreach ($this->buttons as $button) {
72 3
            if (is_array($button)) {
73 3
                $visible = ArrayHelper::remove($button, 'visible', true);
74
75 3
                if ($visible === false) {
76 1
                    continue;
77
                }
78
79 3
                if (!isset($button['encodeLabel'])) {
80 3
                    $button['encodeLabel'] = $this->encodeLabels;
81
                }
82
83 3
                if (!isset($button['options']['type'])) {
84 3
                    ArrayHelper::setValueByPath($button, 'options.type', 'button');
85
                }
86
87 3
                $buttons[] = Button::widget()
88 3
                                 ->encodeLabels($button['encodeLabel'])
0 ignored issues
show
Bug introduced by
The method encodeLabels() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Yii\Bootstrap4\Tabs or Yiisoft\Yii\Bootstrap4\Accordion or Yiisoft\Yii\Bootstrap4\ButtonGroup or Yiisoft\Yii\Bootstrap4\Button or Yiisoft\Yii\Bootstrap4\Dropdown or Yiisoft\Yii\Bootstrap4\Nav or Yiisoft\Yii\Bootstrap4\ButtonDropdown or Yiisoft\Yii\Bootstrap4\Breadcrumbs. ( Ignorable by Annotation )

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

88
                                 ->/** @scrutinizer ignore-call */ encodeLabels($button['encodeLabel'])
Loading history...
89 3
                                 ->label($button['label'])
90 3
                                 ->options($button['options'])
91 3
                                 ->render();
92
            } else {
93 1
                $buttons[] = $button;
94
            }
95
        }
96
97 3
        return implode("\n", $buttons);
98
    }
99
100
    /**
101
     * List of buttons. Each array element represents a single button which can be specified as a string or an array of
102
     * the following structure:
103
     *
104
     * - label: string, required, the button label.
105
     * - options: array, optional, the HTML attributes of the button.
106
     * - visible: bool, optional, whether this button is visible. Defaults to true.
107
     *
108
     * @param array $value
109
     *
110
     * @return $this
111
     */
112 3
    public function buttons(array $value): self
113
    {
114 3
        $this->buttons = $value;
115
116 3
        return $this;
117
    }
118
119
    /**
120
     * Whether to HTML-encode the button labels.
121
     *
122
     * @param bool $value
123
     *
124
     * @return $this
125
     */
126
    public function encodeLabels(bool $value): self
127
    {
128
        $this->encodeLabels = $value;
129
130
        return $this;
131
    }
132
133
    /**
134
     * The HTML attributes for the widget container tag. The following special options are recognized.
135
     *
136
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
137
     *
138
     * @param array $value
139
     *
140
     * @return $this
141
     */
142 2
    public function options(array $value): self
143
    {
144 2
        $this->options = $value;
145
146 2
        return $this;
147
    }
148
}
149