Image   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 131
ccs 49
cts 49
cp 1
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A src() 0 5 1
A tabIndex() 0 5 1
A alt() 0 5 1
A width() 0 5 1
A height() 0 5 1
A autofocus() 0 5 1
A ariaLabel() 0 5 1
A inputAttributes() 0 5 1
A ariaDescribedBy() 0 5 1
A addInputAttributes() 0 5 1
A disabled() 0 5 1
A generateInput() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use Stringable;
8
use Yiisoft\Form\Field\Base\PartsField;
9
use Yiisoft\Html\Html;
10
11
/**
12
 * Represents `<input>` element of type "image" are used to create graphical submit buttons.
13
 *
14
 * @link https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image)
15
 * @link https://developer.mozilla.org/docs/Web/HTML/Element/input/image
16
 */
17
final class Image extends PartsField
18
{
19
    private array $inputAttributes = [];
20
21
    /**
22
     * Provides the textual label for the button for users and user agents who cannot use the image.
23
     *
24
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-alt
25
     */
26 3
    public function alt(?string $value): self
27
    {
28 3
        $new = clone $this;
29 3
        $new->inputAttributes['alt'] = $value;
30 3
        return $new;
31
    }
32
33 5
    public function width(int|string|Stringable|null $width): self
34
    {
35 5
        $new = clone $this;
36 5
        $new->inputAttributes['width'] = $width;
37 5
        return $new;
38
    }
39
40 5
    public function height(int|string|Stringable|null $height): self
41
    {
42 5
        $new = clone $this;
43 5
        $new->inputAttributes['height'] = $height;
44 5
        return $new;
45
    }
46
47
    /**
48
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-src
49
     */
50 5
    public function src(?string $url): self
51
    {
52 5
        $new = clone $this;
53 5
        $new->inputAttributes['src'] = $url;
54 5
        return $new;
55
    }
56
57
    /**
58
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
59
     * at the same time.
60
     *
61
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
62
     */
63 4
    public function autofocus(bool $value = true): self
64
    {
65 4
        $new = clone $this;
66 4
        $new->inputAttributes['autofocus'] = $value;
67 4
        return $new;
68
    }
69
70
    /**
71
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
72
     */
73 4
    public function disabled(bool $disabled = true): self
74
    {
75 4
        $new = clone $this;
76 4
        $new->inputAttributes['disabled'] = $disabled;
77 4
        return $new;
78
    }
79
80
    /**
81
     * Identifies the element (or elements) that describes the object.
82
     *
83
     * @link https://w3c.github.io/aria/#aria-describedby
84
     */
85 2
    public function ariaDescribedBy(?string $value): self
86
    {
87 2
        $new = clone $this;
88 2
        $new->inputAttributes['aria-describedby'] = $value;
89 2
        return $new;
90
    }
91
92
    /**
93
     * Defines a string value that labels the current element.
94
     *
95
     * @link https://w3c.github.io/aria/#aria-label
96
     */
97 2
    public function ariaLabel(?string $value): self
98
    {
99 2
        $new = clone $this;
100 2
        $new->inputAttributes['aria-label'] = $value;
101 2
        return $new;
102
    }
103
104
    /**
105
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
106
     * keyboard navigation (usually with the Tab key, hence the name).
107
     *
108
     * It accepts an integer as a value, with different results depending on the integer's value:
109
     *
110
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
111
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
112
     *   with JavaScript.
113
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
114
     *   defined by the document's source order.
115
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
116
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
117
     *   `tabindex="3"`.
118
     *
119
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
120
     */
121 2
    public function tabIndex(?int $value): self
122
    {
123 2
        $new = clone $this;
124 2
        $new->inputAttributes['tabindex'] = $value;
125 2
        return $new;
126
    }
127
128 2
    public function inputAttributes(array $attributes): self
129
    {
130 2
        $new = clone $this;
131 2
        $new->inputAttributes = $attributes;
132 2
        return $new;
133
    }
134
135 2
    public function addInputAttributes(array $attributes): self
136
    {
137 2
        $new = clone $this;
138 2
        $new->inputAttributes = array_merge($new->inputAttributes, $attributes);
139 2
        return $new;
140
    }
141
142 28
    protected function generateInput(): string
143
    {
144 28
        return Html::input(
145 28
            type: 'image',
146 28
            attributes: $this->inputAttributes
147 28
        )->render();
148
    }
149
}
150