Passed
Pull Request — master (#192)
by Sergei
02:45
created

Image   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 53
ccs 12
cts 24
cp 0.5
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A src() 0 5 1
A alt() 0 5 1
A width() 0 5 1
A generateInput() 0 6 1
A inputTagAttributes() 0 5 1
A height() 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\AbstractSimpleField;
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 AbstractSimpleField
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 1
    public function alt(?string $value): self
24
    {
25 1
        $new = clone $this;
26 1
        $new->inputTagAttributes['alt'] = $value;
27 1
        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 1
    public function src(?string $url): self
48
    {
49 1
        $new = clone $this;
50 1
        $new->inputTagAttributes['src'] = $url;
51 1
        return $new;
52
    }
53
54
    public function inputTagAttributes(array $attributes): self
55
    {
56
        $new = clone $this;
57
        $new->inputTagAttributes = $attributes;
58
        return $new;
59
    }
60
61 1
    protected function generateInput(): string
62
    {
63 1
        return Html::input(
64
            type: 'image',
65 1
            attributes: $this->inputTagAttributes
66 1
        )->render();
67
    }
68
}
69