ButtonField::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Html\Html;
8
use Yiisoft\Html\Tag\Button;
9
10
/**
11
 * @link https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element
12
 */
13
abstract class ButtonField extends PartsField
14
{
15
    use FieldContentTrait;
16
17
    private ?Button $button = null;
18
    private array $buttonAttributes = [];
19
20 4
    final public function button(?Button $button): static
21
    {
22 4
        $new = clone $this;
23 4
        $new->button = $button;
24 4
        return $new;
25
    }
26
27 3
    final public function buttonAttributes(array $attributes): static
28
    {
29 3
        $new = clone $this;
30 3
        $new->buttonAttributes = $attributes;
31 3
        return $new;
32
    }
33
34 3
    final public function addButtonAttributes(array $attributes): static
35
    {
36 3
        $new = clone $this;
37 3
        $new->buttonAttributes = array_merge($this->buttonAttributes, $attributes);
38 3
        return $new;
39
    }
40
41
    /**
42
     * Set button tag ID.
43
     *
44
     * @param string|null $id Button tag ID.
45
     */
46 3
    final public function buttonId(?string $id): static
47
    {
48 3
        $new = clone $this;
49 3
        $new->buttonAttributes['id'] = $id;
50 3
        return $new;
51
    }
52
53
    /**
54
     * Replace button tag CSS classes with a new set of classes.
55
     *
56
     * @param string|null ...$class One or many CSS classes.
57
     */
58 7
    final public function buttonClass(?string ...$class): static
59
    {
60 7
        $new = clone $this;
61 7
        $new->buttonAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
62 7
        return $new;
63
    }
64
65
    /**
66
     * Add one or more CSS classes to the button tag.
67
     *
68
     * @param string|null ...$class One or many CSS classes.
69
     */
70 9
    final public function addButtonClass(?string ...$class): static
71
    {
72 9
        $new = clone $this;
73 9
        Html::addCssClass($new->buttonAttributes, $class);
74 9
        return $new;
75
    }
76
77
    /**
78
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
79
     */
80 3
    final public function name(?string $name): self
81
    {
82 3
        $new = clone $this;
83 3
        $new->buttonAttributes['name'] = $name;
84 3
        return $new;
85
    }
86
87
    /**
88
     * Identifies the element (or elements) that describes the object.
89
     *
90
     * @link https://w3c.github.io/aria/#aria-describedby
91
     */
92 2
    final public function ariaDescribedBy(?string $value): static
93
    {
94 2
        $new = clone $this;
95 2
        $new->buttonAttributes['aria-describedby'] = $value;
96 2
        return $new;
97
    }
98
99
    /**
100
     * Defines a string value that labels the current element.
101
     *
102
     * @link https://w3c.github.io/aria/#aria-label
103
     */
104 2
    final public function ariaLabel(?string $value): static
105
    {
106 2
        $new = clone $this;
107 2
        $new->buttonAttributes['aria-label'] = $value;
108 2
        return $new;
109
    }
110
111
    /**
112
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
113
     * at the same time.
114
     *
115
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
116
     */
117 2
    final public function autofocus(bool $value = true): static
118
    {
119 2
        $new = clone $this;
120 2
        $new->buttonAttributes['autofocus'] = $value;
121 2
        return $new;
122
    }
123
124
    /**
125
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
126
     * keyboard navigation (usually with the Tab key, hence the name).
127
     *
128
     * It accepts an integer as a value, with different results depending on the integer's value:
129
     *
130
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
131
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
132
     *   with JavaScript.
133
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
134
     *   defined by the document's source order.
135
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
136
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
137
     *   `tabindex="3"`.
138
     *
139
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
140
     */
141 2
    final public function tabIndex(?int $value): static
142
    {
143 2
        $new = clone $this;
144 2
        $new->buttonAttributes['tabindex'] = $value;
145 2
        return $new;
146
    }
147
148
    /**
149
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
150
     */
151 5
    final public function disabled(bool $disabled = true): static
152
    {
153 5
        $new = clone $this;
154 5
        $new->buttonAttributes['disabled'] = $disabled;
155 5
        return $new;
156
    }
157
158
    /**
159
     * Specifies the form element the button belongs to. The value of this attribute must be the ID attribute of a form
160
     * element in the same document.
161
     *
162
     * @param string|null $id ID of a form.
163
     *
164
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
165
     */
166 3
    final public function form(?string $id): static
167
    {
168 3
        $new = clone $this;
169 3
        $new->buttonAttributes['form'] = $id;
170 3
        return $new;
171
    }
172
173 55
    final protected function generateInput(): string
174
    {
175 55
        $button = ($this->button ?? Button::tag())
176 55
            ->type($this->getType());
177
178 55
        if (!empty($this->buttonAttributes)) {
179 27
            $button = $button->addAttributes($this->buttonAttributes);
180
        }
181
182 55
        $content = $this->renderContent();
183 55
        if ($content !== '') {
184 13
            $button = $button->content($content);
185
        }
186
187 55
        return $button->render();
188
    }
189
190
    abstract protected function getType(): string;
191
}
192