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

InputAttributes   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 105
ccs 22
cts 30
cp 0.7332
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A form() 0 5 1
A ariaLabel() 0 5 1
A readonly() 0 5 1
A ariaDescribedBy() 0 5 1
A build() 0 17 3
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 1
    public function ariaDescribedBy(string $value): self
21
    {
22 1
        $new = clone $this;
23 1
        $new->attributes['aria-describedby'] = $value;
24 1
        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
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
60
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
61
     * property.
62
     *
63
     * @param bool $value
64
     *
65
     * @return static
66
     *
67
     * @link https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute
68
     */
69 14
    public function readonly(bool $value = true): self
70
    {
71 14
        $new = clone $this;
72 14
        $new->attributes['readonly'] = $value;
73 14
        return $new;
74
    }
75
76
    /**
77
     * If it is required to fill in a value in order to submit the form.
78
     *
79
     * @return static
80
     *
81
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
82
     */
83 13
    public function required(): self
84
    {
85 13
        $new = clone $this;
86 13
        $new->attributes['required'] = true;
87 13
        return $new;
88
    }
89
90
    /**
91
     * Set build attributes for the widget.
92
     *
93
     * @param array $attributes $value
94
     *
95
     * @return array
96
     */
97 535
    protected function build(array $attributes): array
98
    {
99 535
        if (!array_key_exists('id', $attributes)) {
100 486
            $attributes['id'] = $this->getInputId();
101
        }
102
103 535
        if (!array_key_exists('name', $attributes)) {
104 483
            $attributes['name'] = $this->getInputName();
105
        }
106
107 535
        $fieldValidator = new FieldValidator();
108
109 535
        return $fieldValidator->getValidatorAttributes(
110 535
            $this,
111 535
            $this->getFormModel(),
112 535
            $this->getAttribute(),
113
            $attributes,
114
        );
115
    }
116
}
117