Passed
Push — master ( 2301b1...386ec7 )
by Alexander
14:22 queued 11:36
created

ButtonGroup::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
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
    public function render(): 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 7
    public function buttons(array $value): self
74
    {
75 7
        $new = clone $this;
76 7
        $new->buttons = $value;
77
78 7
        return $new;
79
    }
80
81
    /**
82
     * When tags Labels HTML should not be encoded.
83
     */
84 1
    public function withoutEncodeLabels(): self
85
    {
86 1
        $new = clone $this;
87 1
        $new->encodeLabels = false;
88
89 1
        return $new;
90
    }
91
92
    /**
93
     * The HTML attributes for the widget container tag. The following special options are recognized.
94
     *
95
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
96
     *
97
     * @param array $value
98
     */
99 4
    public function options(array $value): self
100
    {
101 4
        $new = clone $this;
102 4
        $new->options = $value;
103
104 4
        return $new;
105
    }
106
107
    /**
108
     * Generates the buttons that compound the group as specified on {@see buttons}.
109
     *
110
     * @throws InvalidConfigException
111
     *
112
     * @return string the rendering result.
113
     */
114 7
    private function renderButtons(): string
115
    {
116 7
        $buttons = [];
117
118 7
        foreach ($this->buttons as $button) {
119 7
            if (is_array($button)) {
120 7
                $visible = ArrayHelper::remove($button, 'visible', true);
121
122 7
                if ($visible === false) {
123 1
                    continue;
124
                }
125
126 7
                if (!isset($button['encodeLabel'])) {
127 7
                    $button['encodeLabel'] = $this->encodeLabels;
128
                }
129
130 7
                if (!isset($button['options']['type'])) {
131 6
                    ArrayHelper::setValueByPath($button, 'options.type', 'button');
132
                }
133
134 7
                $buttonWidget = Button::widget()
135 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 Yiisoft\Yii\Bootstrap5\Button or Yiisoft\Yii\Bootstrap5\Progress or Yiisoft\Yii\Bootstrap5\ButtonDropdown. ( Ignorable by Annotation )

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

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