Passed
Pull Request — master (#192)
by Sergei
19:23 queued 16:27
created

ButtonField::generateInput()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Html\Tag\Button;
8
9
/**
10
 * @link https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element
11
 */
12
abstract class ButtonField extends PartsField
13
{
14
    use FieldContentTrait;
15
16
    private ?Button $button = null;
17
    private array $attributes = [];
18
19 2
    final public function button(?Button $button): static
20
    {
21 2
        $new = clone $this;
22 2
        $new->button = $button;
23 2
        return $new;
24
    }
25
26 1
    final public function attributes(array $attributes): static
27
    {
28 1
        $new = clone $this;
29 1
        $new->attributes = array_merge($this->attributes, $attributes);
30 1
        return $new;
31
    }
32
33 1
    final public function replaceAttributes(array $attributes): static
34
    {
35 1
        $new = clone $this;
36 1
        $new->attributes = $attributes;
37 1
        return $new;
38
    }
39
40 4
    final public function disabled(?bool $disabled = true): static
41
    {
42 4
        $new = clone $this;
43 4
        $new->attributes['disabled'] = $disabled;
44 4
        return $new;
45
    }
46
47
    /**
48
     * Specifies the form element the button belongs to. The value of this attribute must be the ID attribute of a form
49
     * element in the same document.
50
     *
51
     * @param string|null $id ID of a form.
52
     *
53
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
54
     */
55 1
    final public function form(?string $id): static
56
    {
57 1
        $new = clone $this;
58 1
        $new->attributes['form'] = $id;
59 1
        return $new;
60
    }
61
62
    /**
63
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
64
     * keyboard navigation (usually with the Tab key, hence the name).
65
     *
66
     * It accepts an integer as a value, with different results depending on the integer's value:
67
     *
68
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
69
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
70
     *   with JavaScript.
71
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
72
     *   defined by the document's source order.
73
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
74
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
75
     *   `tabindex="3"`.
76
     *
77
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
78
     */
79
    final public function tabIndex(?int $value): static
80
    {
81
        $new = clone $this;
82
        $new->attributes['tabindex'] = $value;
83
        return $new;
84
    }
85
86 11
    final protected function generateInput(): string
87
    {
88 11
        $button = ($this->button ?? Button::tag())
89 11
            ->type($this->getType());
90
91 11
        if (!empty($this->attributes)) {
92 3
            $button = $button->attributes($this->attributes);
93
        }
94
95 11
        $content = $this->renderContent();
96 11
        if ($content !== '') {
97 7
            $button = $button->content($content);
98
        }
99
100 11
        return $button->render();
101
    }
102
103
    abstract protected function getType(): string;
104
}
105