Passed
Pull Request — master (#192)
by Alexander
06:02 queued 02:54
created

ButtonGroup   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 31.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 78
ccs 11
cts 35
cp 0.3143
rs 10
c 1
b 0
f 0
wmc 10

9 Methods

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