Passed
Pull Request — master (#192)
by Sergei
13:59
created

ButtonField::replaceButtonClass()   A

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