ButtonToolbar::render()   A
last analyzed

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
 * ButtonToolbar Combines sets of button groups into button toolbars for more complex components.
16
 * Use utility classes as needed to space out groups, buttons, and more.
17
 *
18
 * For example,
19
 *
20
 * ```php
21
 * // a button toolbar with items configuration
22
 * echo ButtonToolbar::widget()
23
 *     ->buttonGroups([
24
 *         [
25
 *             'buttons' => [
26
 *                 ['label' => '1', 'class' => ['btn-secondary']],
27
 *                 ['label' => '2', 'class' => ['btn-secondary']],
28
 *                 ['label' => '3', 'class' => ['btn-secondary']],
29
 *                 ['label' => '4', 'class' => ['btn-secondary']]
30
 *             ],
31
 *              'class' => ['mr-2']
32
 *         ],
33
 *         [
34
 *             'buttons' => [
35
 *                 ['label' => '5', 'class' => ['btn-secondary']],
36
 *                 ['label' => '6', 'class' => ['btn-secondary']],
37
 *                 ['label' => '7', 'class' => ['btn-secondary']]
38
 *             ],
39
 *             'class' => ['mr-2']
40
 *         ],
41
 *         [
42
 *             'buttons' => [
43
 *                 ['label' => '8', 'class' => ['btn-secondary']]
44
 *             ]
45
 *         ]
46
 *     ]);
47
 * ```
48
 *
49
 * Pressing on the button should be handled via JavaScript. See the following for details:
50
 */
51
final class ButtonToolbar extends Widget
52
{
53
    private bool $encodeTags = false;
54
    private array $buttonGroups = [];
55
    private array $options = [];
56
57 4
    public function render(): string
58
    {
59 4
        if (!isset($this->options['id'])) {
60 4
            $this->options['id'] = "{$this->getId()}-button-toolbar";
61
        }
62
63
        /** @psalm-suppress InvalidArgument */
64 4
        Html::addCssClass($this->options, ['widget' => 'btn-toolbar']);
65
66 4
        if (!isset($this->options['role'])) {
67 4
            $this->options['role'] = 'toolbar';
68
        }
69
70 4
        return Html::div($this->renderButtonGroups(), $this->options)
71 4
            ->encode($this->encodeTags)
72 4
            ->render();
73
    }
74
75
    /**
76
     * List of buttons groups. Each array element represents a single group which can be specified as a string or an
77
     * array of the following structure:
78
     *
79
     * - buttons: array list of buttons. Either as array or string representation
80
     * - options: array optional, the HTML attributes of the button group.
81
     * - encodeLabels: bool whether to HTML-encode the button labels.
82
     */
83 4
    public function buttonGroups(array $value): self
84
    {
85 4
        $new = clone $this;
86 4
        $new->buttonGroups = $value;
87
88 4
        return $new;
89
    }
90
91
    /**
92
     * The HTML attributes for the container tag. The following special options are recognized.
93
     *
94
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
95
     */
96 2
    public function options(array $value): self
97
    {
98 2
        $new = clone $this;
99 2
        $new->options = $value;
100
101 2
        return $new;
102
    }
103
104
    /**
105
     * Generates the button groups that compound the toolbar as specified on {@see buttonGroups}.
106
     *
107
     * @throws InvalidConfigException
108
     *
109
     * @return string the rendering result.
110
     */
111 4
    private function renderButtonGroups(): string
112
    {
113 4
        $buttonGroups = [];
114
115 4
        foreach ($this->buttonGroups as $group) {
116 4
            if (is_array($group)) {
117 4
                if (!isset($group['buttons'])) {
118 1
                    continue;
119
                }
120
121 3
                $options = ArrayHelper::getValue($group, 'options', []);
122 3
                $buttonGroups[] = ButtonGroup::widget()
123 3
                    ->buttons($group['buttons'])
0 ignored issues
show
Bug introduced by
The method buttons() 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\ButtonGroup. ( Ignorable by Annotation )

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

123
                    ->/** @scrutinizer ignore-call */ buttons($group['buttons'])
Loading history...
124 3
                    ->options($options)
125 3
                    ->render();
126
            } else {
127 2
                $buttonGroups[] = $group;
128
            }
129
        }
130
131 4
        return implode("\n", $buttonGroups);
132
    }
133
}
134