Test Failed
Pull Request — master (#192)
by Sergei
02:42
created

Label::setFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Part;
6
7
use Stringable;
8
use Yiisoft\Form\Field\Base\FormAttributeTrait;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Widget\Widget;
11
12
/**
13
 * Represents label for a form field.
14
 */
15
final class Label extends Widget
16
{
17
    use FormAttributeTrait;
18
19
    private array $attributes = [];
20
21
    private bool $setFor = true;
22
23
    private ?string $forId = null;
24
    private bool $useInputId = true;
25
26
    private string|Stringable|null $content = null;
27
28
    private bool $encode = true;
29
30
    public function attributes(array $attributes): self
31
    {
32
        $new = clone $this;
33
        $new->attributes = array_merge($this->attributes, $attributes);
34
        return $new;
35
    }
36
37
    public function replaceAttributes(array $attributes): self
38
    {
39
        $new = clone $this;
40
        $new->attributes = $attributes;
41
        return $new;
42
    }
43
44
    /**
45
     * Set tag ID.
46
     *
47
     * @param string|null $id Tag ID.
48
     */
49
    public function id(?string $id): self
50
    {
51
        $new = clone $this;
52
        $new->attributes['id'] = $id;
53
        return $new;
54
    }
55
56
    /**
57
     * Add one or more CSS classes to the tag.
58
     *
59
     * @param string|null ...$class One or many CSS classes.
60
     */
61
    public function class(?string ...$class): self
62
    {
63
        $new = clone $this;
64
        Html::addCssClass(
65
            $new->attributes,
66
            array_filter($class, static fn ($c) => $c !== null),
67
        );
68
        return $new;
69
    }
70
71
    /**
72
     * Replace tag CSS classes with a new set of classes.
73
     *
74
     * @param string|null ...$class One or many CSS classes.
75
     */
76
    public function replaceClass(?string ...$class): self
77
    {
78
        $new = clone $this;
79
        $new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
80
        return $new;
81
    }
82
83
    public function setFor(bool $value): self
84
    {
85
        $new = clone $this;
86
        $new->setFor = $value;
87
        return $new;
88
    }
89
90
    /**
91
     * @return static
92
     */
93
    public function forId(?string $id): self
94
    {
95
        $new = clone $this;
96
        $new->forId = $id;
97
        return $new;
98
    }
99
100
    /**
101
     * @return static
102
     */
103
    public function useInputId(bool $value): self
104
    {
105
        $new = clone $this;
106
        $new->useInputId = $value;
107
        return $new;
108
    }
109
110
    /**
111
     * @return static
112
     */
113
    public function content(string|Stringable|null $content): self
114
    {
115
        $new = clone $this;
116
        $new->content = $content;
117
        return $new;
118
    }
119
120
    /**
121
     * Whether content should be HTML-encoded.
122
     *
123
     * @return static
124
     */
125
    public function encode(bool $value): self
126
    {
127
        $new = clone $this;
128
        $new->encode = $value;
129
        return $new;
130
    }
131
132
    protected function run(): string
133
    {
134
        $useModel = $this->hasFormModelAndAttribute();
135
136
        $content = $useModel
137
            ? $this->content ?? $this->getFormAttributeLabel()
138
            : (string) $this->content;
139
140
        if ($content === '') {
141
            return '';
142
        }
143
144
        $labelAttributes = $this->attributes;
145
146
        if ($this->setFor && !isset($labelAttributes['for'])) {
147
            $id = $this->forId;
148
            if ($useModel && $id === null && $this->useInputId) {
149
                $id = $this->getInputId();
150
            }
151
            if ($id !== null) {
152
                $labelAttributes['for'] = $id;
153
            }
154
        }
155
156
        $tag = Html::label($content)->attributes($labelAttributes);
157
158
        if (!$this->encode) {
159
            $tag = $tag->encode(false);
160
        }
161
162
        return $tag->render();
163
    }
164
}
165