Passed
Pull Request — master (#90)
by
unknown
13:09
created

ButtonToolbar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 89
ccs 30
cts 30
cp 1
rs 10
wmc 9

4 Methods

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

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

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