Passed
Push — master ( cae493...39eeb7 )
by Alexander
02:49
created

ButtonToolbar::buttonGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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\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
class ButtonToolbar extends Widget
52
{
53
    private array $buttonGroups = [];
54
    private array $options = [];
55
56 3
    protected function run(): string
57
    {
58 3
        if (!isset($this->options['id'])) {
59 3
            $this->options['id'] = "{$this->getId()}-button-toolbar";
60
        }
61
62 3
        Html::addCssClass($this->options, ['widget' => 'btn-toolbar']);
63
64 3
        if (!isset($this->options['role'])) {
65 3
            $this->options['role'] = 'toolbar';
66
        }
67
68 3
        return Html::tag('div', $this->renderButtonGroups(), $this->options);
69
    }
70
71
    /**
72
     * Generates the button groups that compound the toolbar as specified on {@see buttonGroups}.
73
     *
74
     * @throws InvalidConfigException
75
     *
76
     * @return string the rendering result.
77
     */
78 3
    protected function renderButtonGroups(): string
79
    {
80 3
        $buttonGroups = [];
81
82 3
        foreach ($this->buttonGroups as $group) {
83 3
            if (is_array($group)) {
84 3
                if (!isset($group['buttons'])) {
85
                    continue;
86
                }
87
88 3
                $options = ArrayHelper::getValue($group, 'options', []);
89 3
                $buttonGroups[] = ButtonGroup::widget()
90 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

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