Test Failed
Pull Request — master (#160)
by Wilmer
02:22
created

ChoiceAttributes::buildList()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 25
rs 9.5555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
abstract class ChoiceAttributes extends WidgetAttributes
8
{
9
    /**
10
     * If it is required to fill in a value in order to submit the form.
11
     *
12
     * @return static
13
     *
14
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
15
     */
16
    public function required(): self
17
    {
18
        $new = clone $this;
19
        $new->attributes['required'] = true;
20
        return $new;
21
    }
22
23
    /**
24
     * Set build attributes for the ChoiceWidget.
25
     *
26
     * @param array $attributes $value
27
     *
28
     * @return array
29
     */
30
    protected function build(array $attributes): array
31
    {
32
        if (!array_key_exists('id', $attributes)) {
33
            $attributes['id'] = $this->getInputId();
34
        }
35
36
        if (!array_key_exists('name', $attributes)) {
37
            $attributes['name'] = $this->getInputName();
38
        }
39
40
        return $attributes;
41
    }
42
43
    /**
44
     * Set build container attributes for the ChoiceListWidget.
45
     *
46
     * @param array $attributes $value
47
     * @param array $containerAttributes
48
     *
49
     * @return array
50
     */
51
    protected function buildList(array $attributes, array $containerAttributes): array
52
    {
53
        if (array_key_exists('autofocus', $attributes)) {
54
            /** @var string */
55
            $containerAttributes['autofocus'] = $attributes['autofocus'];
56
            unset($attributes['autofocus']);
57
        }
58
59
        if (array_key_exists('id', $attributes)) {
60
            /** @var string */
61
            $containerAttributes['id'] = $attributes['id'];
62
            unset($attributes['id']);
63
        }
64
65
        if (!array_key_exists('id', $containerAttributes)) {
66
            $containerAttributes['id'] = $this->getInputId();
67
        }
68
69
        if (array_key_exists('tabindex', $attributes)) {
70
            /** @var string */
71
            $containerAttributes['tabindex'] = $attributes['tabindex'];
72
            unset($attributes['tabindex']);
73
        }
74
75
        return [$attributes, $containerAttributes];
76
    }
77
}
78