Passed
Pull Request — master (#160)
by Wilmer
04:38 queued 02:07
created

AbstractForm::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use InvalidArgumentException;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Form\FormModelInterface;
10
use Yiisoft\Form\Helper\HtmlForm;
11
use Yiisoft\Widget\Widget;
12
13
abstract class AbstractForm extends Widget
14
{
15
    protected array $attributes = [];
16
    private string $attribute = '';
17
    private bool $encode = true;
18
    private ?FormModelInterface $formModel = null;
19
20
    /**
21
     * Add HTML attribute.
22
     *
23
     * @param string $key Attribute name.
24
     * @param mixed $value Attribute value.
25
     *
26
     * @return static
27
     */
28 1
    public function addAttribute(string $key, $value): self
29
    {
30 1
        $new = clone $this;
31 1
        $new->attributes[$key] = $value;
32 1
        return $new;
33
    }
34
35
    /**
36
     * The HTML attributes. The following special options are recognized.
37
     *
38
     * @param array $values Attribute values indexed by attribute names.
39
     *
40
     * @return static
41
     *
42
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
43
     */
44 181
    public function attributes(array $values): self
45
    {
46 181
        $new = clone $this;
47 181
        $new->attributes = $values;
48 181
        return $new;
49
    }
50
51
    /**
52
     * Whether content should be HTML-encoded.
53
     *
54
     * @param bool $value
55
     *
56
     * @return static
57
     */
58 14
    public function encode(bool $value): self
59
    {
60 14
        $new = clone $this;
61 14
        $new->encode = $value;
62 14
        return $new;
63
    }
64
65
    /**
66
     * @return static
67
     */
68 503
    public function for(FormModelInterface $formModel, string $attribute): self
69
    {
70 503
        $new = clone $this;
71 503
        $new->formModel = $formModel;
72 503
        $new->attribute = $attribute;
73 503
        return $new;
74
    }
75
76 478
    protected function getAttribute(): string
77
    {
78 478
        if ($this->attribute === '') {
79 1
            throw new InvalidArgumentException('Attribute is not set.');
80
        }
81
82 477
        return $this->attribute;
83
    }
84
85 234
    protected function getEncode(): bool
86
    {
87 234
        return $this->encode;
88
    }
89
90
    /**
91
     * Return FormModelInterface object.
92
     *
93
     * @return FormModelInterface
94
     */
95 490
    protected function getFormModel(): FormModelInterface
96
    {
97 490
        if ($this->formModel === null) {
98 1
            throw new InvalidArgumentException('Form model is not set.');
99
        }
100
101 489
        return $this->formModel;
102
    }
103
104
    /**
105
     * Generates a unique ID for the attribute, if it does not have one yet.
106
     *
107
     * @return string|null
108
     */
109 411
    protected function getId(): ?string
110
    {
111 411
        $id = ArrayHelper::remove($this->attributes, 'id', '');
112
113 411
        if (!is_string($id) && null !== $id) {
114 1
            throw new InvalidArgumentException('Attribute "id" must be a string or null.');
115
        }
116
117 410
        if ($id === '') {
118 410
            $id = HtmlForm::getInputId($this->getFormModel(), $this->attribute);
119
        }
120
121 410
        return $id ?? null;
122
    }
123
124
    /**
125
     * Generate name for the widget.
126
     *
127
     * @return string
128
     */
129 414
    protected function getName(): string
130
    {
131 414
        $name = $this->attributes['name'] ?? HtmlForm::getInputName($this->getFormModel(), $this->attribute);
132
133 414
        if (!is_string($name)) {
134 1
            throw new InvalidArgumentException('Attribute "name" must be a string.');
135
        }
136
137 413
        return $name;
138
    }
139
}
140