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

ButtonToolbar::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
 * 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
     * @param array $value
84
     */
85 4
    public function buttonGroups(array $value): self
86
    {
87 4
        $new = clone $this;
88 4
        $new->buttonGroups = $value;
89
90 4
        return $new;
91
    }
92
93
    /**
94
     * The HTML attributes for the container tag. The following special options are recognized.
95
     *
96
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
97
     *
98
     * @param array $value
99
     */
100 2
    public function options(array $value): self
101
    {
102 2
        $new = clone $this;
103 2
        $new->options = $value;
104
105 2
        return $new;
106
    }
107
108
    /**
109
     * Generates the button groups that compound the toolbar as specified on {@see buttonGroups}.
110
     *
111
     * @throws InvalidConfigException
112
     *
113
     * @return string the rendering result.
114
     */
115 4
    private function renderButtonGroups(): string
116
    {
117 4
        $buttonGroups = [];
118
119 4
        foreach ($this->buttonGroups as $group) {
120 4
            if (is_array($group)) {
121 4
                if (!isset($group['buttons'])) {
122 1
                    continue;
123
                }
124
125 3
                $options = ArrayHelper::getValue($group, 'options', []);
126 3
                $buttonGroups[] = ButtonGroup::widget()
127 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

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