Passed
Push — master ( 566566...d0f53d )
by Alexander
02:18
created

ButtonToolbar::renderButtonGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 4
rs 9.8333
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
 * 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 5
    protected function run(): string
58
    {
59 5
        if (!isset($this->options['id'])) {
60 5
            $this->options['id'] = "{$this->getId()}-button-toolbar";
61
        }
62
63
        /** @psalm-suppress InvalidArgument */
64 5
        Html::addCssClass($this->options, ['widget' => 'btn-toolbar']);
65
66 5
        if (!isset($this->options['role'])) {
67 5
            $this->options['role'] = 'toolbar';
68
        }
69
70 5
        if ($this->encodeTags === false) {
71 4
            $this->options['encode'] = false;
72
        }
73
74 5
        return Html::div($this->renderButtonGroups(), $this->options);
75
    }
76
77
    /**
78
     * List of buttons groups. Each array element represents a single group which can be specified as a string or an
79
     * array of the following structure:
80
     *
81
     * - buttons: array list of buttons. Either as array or string representation
82
     * - options: array optional, the HTML attributes of the button group.
83
     * - encodeLabels: bool whether to HTML-encode the button labels.
84
     *
85
     * @param array $value
86
     *
87
     * @return $this
88
     */
89 5
    public function buttonGroups(array $value): self
90
    {
91 5
        $new = clone $this;
92 5
        $new->buttonGroups = $value;
93
94 5
        return $new;
95
    }
96
97
    /**
98
     * The HTML attributes for the container tag. The following special options are recognized.
99
     *
100
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
101
     *
102
     * @param array $value
103
     *
104
     * @return $this
105
     */
106 2
    public function options(array $value): self
107
    {
108 2
        $new = clone $this;
109 2
        $new->options = $value;
110
111 2
        return $new;
112
    }
113
114
    /**
115
     * Allows you to enable the encoding tags html.
116
     *
117
     * @return self
118
     */
119 1
    public function encodeTags(): self
120
    {
121 1
        $new = clone $this;
122 1
        $new->encodeTags = true;
123
124 1
        return $new;
125
    }
126
127
    /**
128
     * Generates the button groups that compound the toolbar as specified on {@see buttonGroups}.
129
     *
130
     * @throws InvalidConfigException
131
     *
132
     * @return string the rendering result.
133
     */
134 5
    private function renderButtonGroups(): string
135
    {
136 5
        $buttonGroups = [];
137
138 5
        foreach ($this->buttonGroups as $group) {
139 5
            if (is_array($group)) {
140 5
                if (!isset($group['buttons'])) {
141 1
                    continue;
142
                }
143
144 4
                $options = ArrayHelper::getValue($group, 'options', []);
145 4
                $buttonGroups[] = ButtonGroup::widget()
146 4
                    ->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

146
                    ->/** @scrutinizer ignore-call */ buttons($group['buttons'])
Loading history...
147 4
                    ->options($options)
148 4
                    ->render();
149
            } else {
150 2
                $buttonGroups[] = $group;
151
            }
152
        }
153
154 5
        return implode("\n", $buttonGroups);
155
    }
156
}
157