Passed
Pull Request — master (#192)
by Alexander
03:40
created

ButtonField   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 64.44%

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 132
ccs 29
cts 45
cp 0.6444
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A button() 0 5 1
A attributes() 0 5 1
A replaceAttributes() 0 5 1
A autofocus() 0 5 1
A generateInput() 0 15 3
A tabIndex() 0 5 1
A ariaLabel() 0 5 1
A ariaDescribedBy() 0 5 1
A form() 0 5 1
A disabled() 0 5 1
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
    /**
41
     * Identifies the element (or elements) that describes the object.
42
     *
43
     * @link https://w3c.github.io/aria/#aria-describedby
44
     */
45
    final public function ariaDescribedBy(string $value): static
46
    {
47
        $new = clone $this;
48
        $new->attributes['aria-describedby'] = $value;
49
        return $new;
50
    }
51
52
    /**
53
     * Defines a string value that labels the current element.
54
     *
55
     * @link https://w3c.github.io/aria/#aria-label
56
     */
57
    final public function ariaLabel(string $value): static
58
    {
59
        $new = clone $this;
60
        $new->attributes['aria-label'] = $value;
61
        return $new;
62
    }
63
64
    /**
65
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
66
     * at the same time.
67
     *
68
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
69
     */
70
    final public function autofocus(bool $value = true): static
71
    {
72
        $new = clone $this;
73
        $new->attributes['autofocus'] = $value;
74
        return $new;
75
    }
76
77
    /**
78
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
79
     * keyboard navigation (usually with the Tab key, hence the name).
80
     *
81
     * It accepts an integer as a value, with different results depending on the integer's value:
82
     *
83
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
84
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
85
     *   with JavaScript.
86
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
87
     *   defined by the document's source order.
88
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
89
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
90
     *   `tabindex="3"`.
91
     *
92
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
93
     */
94
    final public function tabIndex(?int $value): static
95
    {
96
        $new = clone $this;
97
        $new->attributes['tabindex'] = $value;
98
        return $new;
99
    }
100
101
    /**
102
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
103
     */
104 3
    final public function disabled(bool $disabled = true): static
105
    {
106 3
        $new = clone $this;
107 3
        $new->attributes['disabled'] = $disabled;
108 3
        return $new;
109
    }
110
111
    /**
112
     * Specifies the form element the button belongs to. The value of this attribute must be the ID attribute of a form
113
     * element in the same document.
114
     *
115
     * @param string|null $id ID of a form.
116
     *
117
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
118
     */
119 1
    final public function form(?string $id): static
120
    {
121 1
        $new = clone $this;
122 1
        $new->attributes['form'] = $id;
123 1
        return $new;
124
    }
125
126 10
    final protected function generateInput(): string
127
    {
128 10
        $button = ($this->button ?? Button::tag())
129 10
            ->type($this->getType());
130
131 10
        if (!empty($this->attributes)) {
132 2
            $button = $button->attributes($this->attributes);
133
        }
134
135 10
        $content = $this->renderContent();
136 10
        if ($content !== '') {
137 7
            $button = $button->content($content);
138
        }
139
140 10
        return $button->render();
141
    }
142
143
    abstract protected function getType(): string;
144
}
145