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

ButtonAttributes   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 60
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 13 3
A disabled() 0 5 1
A form() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
use Yiisoft\Html\Html;
8
use Yiisoft\Widget\Widget;
9
10
abstract class ButtonAttributes extends GlobalAttributes
11
{
12
    /**
13
     * Set whether the element is disabled or not.
14
     *
15
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
16
     * text.
17
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
18
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
19
     * this will suggest you can set it to `false` to enable the element again, which is not the case.
20
     *
21
     * @return static
22
     *
23
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled
24
     */
25 2
    public function disabled(): self
26
    {
27 2
        $new = clone $this;
28 2
        $new->attributes['disabled'] = true;
29 2
        return $new;
30
    }
31
32
    /**
33
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the id
34
     * attribute of a {@see Form} element in the same document.
35
     *
36
     * @param string $value
37
     *
38
     * @return static
39
     *
40
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
41
     */
42 4
    public function form(string $value): self
43
    {
44 4
        $new = clone $this;
45 4
        $new->attributes['form'] = $value;
46 4
        return $new;
47
    }
48
49
    /**
50
     * Set build attributes for the widget.
51
     *
52
     * @param array $attributes $value
53
     * @param string $suffix The suffix of the attribute name.
54
     *
55
     * @return array
56
     */
57 41
    protected function build(array $attributes, string $suffix): array
58
    {
59 41
        $id = Html::generateId('w') . $suffix;
60
61 41
        if (!array_key_exists('id', $attributes)) {
62 33
            $attributes['id'] = $id;
63
        }
64
65 41
        if (!array_key_exists('name', $attributes)) {
66 33
            $attributes['name'] = $id;
67
        }
68
69 41
        return $attributes;
70
    }
71
}
72