Test Failed
Pull Request — master (#35)
by Wilmer
02:28
created

ButtonToolbar::withOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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