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

ChoiceAttributes::buildList()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

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
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
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