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

ChoiceAttributes   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 76
ccs 27
cts 27
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 17 3
A required() 0 5 1
A buildList() 0 25 5
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 ChoiceAttributes extends WidgetAttributes
10
{
11
    /**
12
     * If it is required to fill in a value in order to submit the form.
13
     *
14
     * @return static
15
     *
16
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
17
     */
18 3
    public function required(): self
19
    {
20 3
        $new = clone $this;
21 3
        $new->attributes['required'] = true;
22 3
        return $new;
23
    }
24
25
    /**
26
     * Set build attributes for the ChoiceWidget.
27
     *
28
     * @param array $attributes $value
29
     *
30
     * @return array
31
     */
32 80
    protected function build(array $attributes): array
33
    {
34 80
        if (!array_key_exists('id', $attributes)) {
35 72
            $attributes['id'] = $this->getInputId();
36
        }
37
38 80
        if (!array_key_exists('name', $attributes)) {
39 72
            $attributes['name'] = $this->getInputName();
40
        }
41
42 80
        $fieldValidator = new FieldValidator();
43
44 80
        return $fieldValidator->getValidatorAttributes(
45 80
            $this,
46 80
            $this->getFormModel(),
47 80
            $this->getAttribute(),
48
            $attributes,
49
        );
50
    }
51
52
    /**
53
     * Set build container attributes for the ChoiceListWidget.
54
     *
55
     * @param array $attributes $value
56
     * @param array $containerAttributes
57
     *
58
     * @return array
59
     */
60 77
    protected function buildList(array $attributes, array $containerAttributes): array
61
    {
62 77
        if (array_key_exists('autofocus', $attributes)) {
63
            /** @var string */
64 2
            $containerAttributes['autofocus'] = $attributes['autofocus'];
65 2
            unset($attributes['autofocus']);
66
        }
67
68 77
        if (array_key_exists('id', $attributes)) {
69
            /** @var string */
70 4
            $containerAttributes['id'] = $attributes['id'];
71 4
            unset($attributes['id']);
72
        }
73
74 77
        if (!array_key_exists('id', $containerAttributes)) {
75 69
            $containerAttributes['id'] = $this->getInputId();
76
        }
77
78 77
        if (array_key_exists('tabindex', $attributes)) {
79
            /** @var string */
80 2
            $containerAttributes['tabindex'] = $attributes['tabindex'];
81 2
            unset($attributes['tabindex']);
82
        }
83
84 77
        return [$attributes, $containerAttributes];
85
    }
86
}
87