ButtonGroup   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A separator() 0 5 1
A buttonsData() 0 5 1
A disabled() 0 5 1
A buttonAttributes() 0 5 1
A buttons() 0 5 1
A form() 0 5 1
A addButtonAttributes() 0 5 1
A generateInput() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use Yiisoft\Form\Field\Base\PartsField;
8
use Yiisoft\Html\Tag\Button as ButtonTag;
9
use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget;
10
11
/**
12
 * Represents a button group widget.
13
 *
14
 * @see ButtonGroupWidget
15
 */
16
final class ButtonGroup extends PartsField
17
{
18
    private ButtonGroupWidget $widget;
19
20 12
    public function __construct()
21
    {
22 12
        $this->widget = ButtonGroupWidget::create()->withoutContainer();
23
    }
24
25 6
    public function buttons(ButtonTag ...$buttons): self
26
    {
27 6
        $new = clone $this;
28 6
        $new->widget = $this->widget->buttons(...$buttons);
29 6
        return $new;
30
    }
31
32
    /**
33
     * @param array $data Array of buttons. Each button is an array with label as first element and additional
34
     * name-value pairs as attributes of button.
35
     *
36
     * Example:
37
     * ```php
38
     * [
39
     *     ['Reset', 'type' => 'reset', 'class' => 'default'],
40
     *     ['Send', 'type' => 'submit', 'class' => 'primary'],
41
     * ]
42
     * ```
43
     * @param bool $encode Whether button content should be HTML-encoded.
44
     */
45 7
    public function buttonsData(array $data, bool $encode = true): self
46
    {
47 7
        $new = clone $this;
48 7
        $new->widget = $this->widget->buttonsData($data, $encode);
49 7
        return $new;
50
    }
51
52 2
    public function buttonAttributes(array $attributes): self
53
    {
54 2
        $new = clone $this;
55 2
        $new->widget = $this->widget->buttonAttributes($attributes);
56 2
        return $new;
57
    }
58
59 2
    public function addButtonAttributes(array $attributes): self
60
    {
61 2
        $new = clone $this;
62 2
        $new->widget = $this->widget->addButtonAttributes($attributes);
63 2
        return $new;
64
    }
65
66
    /**
67
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
68
     */
69 2
    public function disabled(bool $disabled = true): self
70
    {
71 2
        $new = clone $this;
72 2
        $new->widget = $this->widget->disabled($disabled);
73 2
        return $new;
74
    }
75
76 2
    public function form(?string $formId): self
77
    {
78 2
        $new = clone $this;
79 2
        $new->widget = $this->widget->form($formId);
80 2
        return $new;
81
    }
82
83 2
    public function separator(string $separator): self
84
    {
85 2
        $new = clone $this;
86 2
        $new->widget = $this->widget->separator($separator);
87 2
        return $new;
88
    }
89
90 11
    protected function generateInput(): string
91
    {
92 11
        return $this->widget->render();
93
    }
94
}
95