Passed
Pull Request — master (#192)
by Alexander
05:47 queued 02:55
created

Image   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 36.36%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 36
c 3
b 0
f 0
dl 0
loc 124
ccs 16
cts 44
cp 0.3636
rs 10
wmc 11

11 Methods

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