Passed
Pull Request — master (#160)
by Wilmer
02:47
created

InputAttributes   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 56.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 116
ccs 18
cts 32
cp 0.5625
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A form() 0 5 1
A readonly() 0 5 1
A ariaDescribedBy() 0 5 1
A getAriaDescribedBy() 0 4 1
A build() 0 17 3
A ariaLabel() 0 5 1
A required() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
use Yiisoft\Form\Widget\Validator\FieldValidator;
8
9
abstract class InputAttributes extends WidgetAttributes
10
{
11
    /**
12
     * Set aria-describedby attribute.
13
     *
14
     * @param string $value
15
     *
16
     * @return static
17
     *
18
     * @link https://www.w3.org/TR/WCAG20-TECHS/ARIA1.html
19
     */
20
    public function ariaDescribedBy(string $value): self
21
    {
22
        $new = clone $this;
23
        $new->attributes['aria-describedby'] = $value;
24
        return $new;
25
    }
26
27
    /**
28
     * Set aria-label attribute.
29
     *
30
     * @param string $value
31
     *
32
     * @return static
33
     */
34
    public function ariaLabel(string $value): self
35
    {
36
        $new = clone $this;
37
        $new->attributes['aria-label'] = $value;
38
        return $new;
39
    }
40
41
    /**
42
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the id
43
     * attribute of a {@see Form} element in the same document.
44
     *
45
     * @param string $value
46
     *
47
     * @return static
48
     *
49
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
50
     */
51
    public function form(string $value): self
52
    {
53
        $new = clone $this;
54
        $new->attributes['form'] = $value;
55
        return $new;
56
    }
57
58
    /**
59
     * Return aria-describedby attribute.
60
     *
61
     * @return string
62
     */
63
    public function getAriaDescribedBy(): string
64
    {
65
        /** @var string */
66
        return $this->attributes['aria-describedby'] ?? '';
67
    }
68
69
    /**
70
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
71
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
72
     * property.
73
     *
74
     * @param bool $value
75
     *
76
     * @return static
77
     *
78
     * @link https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute
79
     */
80 14
    public function readonly(bool $value = true): self
81
    {
82 14
        $new = clone $this;
83 14
        $new->attributes['readonly'] = $value;
84 14
        return $new;
85
    }
86
87
    /**
88
     * If it is required to fill in a value in order to submit the form.
89
     *
90
     * @return static
91
     *
92
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
93
     */
94 13
    public function required(): self
95
    {
96 13
        $new = clone $this;
97 13
        $new->attributes['required'] = true;
98 13
        return $new;
99
    }
100
101
    /**
102
     * Set build attributes for the widget.
103
     *
104
     * @param array $attributes $value
105
     *
106
     * @return array
107
     */
108 519
    protected function build(array $attributes): array
109
    {
110 519
        if (!array_key_exists('id', $attributes)) {
111 470
            $attributes['id'] = $this->getInputId();
112
        }
113
114 519
        if (!array_key_exists('name', $attributes)) {
115 467
            $attributes['name'] = $this->getInputName();
116
        }
117
118 519
        $fieldValidator = new FieldValidator();
119
120 519
        return $fieldValidator->getValidatorAttributes(
121 519
            $this,
122 519
            $this->getFormModel(),
123 519
            $this->getAttribute(),
124
            $attributes,
125
        );
126
    }
127
}
128