Passed
Pull Request — master (#192)
by Alexander
05:47 queued 02:55
created

ButtonGroup::buttonsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
 * `ButtonGroup` renders a button group widget.
13
 */
14
final class ButtonGroup extends PartsField
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
    /**
65
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
66
     */
67
    public function disabled(bool $disabled = true): self
68
    {
69
        $new = clone $this;
70
        $new->widget = $this->widget->disabled($disabled);
71
        return $new;
72
    }
73
74
    public function form(?string $formId): self
75
    {
76
        $new = clone $this;
77
        $new->widget = $this->widget->form($formId);
78
        return $new;
79
    }
80
81
    public function separator(string $separator): self
82
    {
83
        $new = clone $this;
84
        $new->widget = $this->widget->separator($separator);
85
        return $new;
86
    }
87
88 1
    protected function generateInput(): string
89
    {
90 1
        $html = $this->widget->render();
91
92 1
        return $html !== ''
93 1
            ? "\n" . $html . "\n"
94 1
            : '';
95
    }
96
}
97