Passed
Pull Request — master (#192)
by Sergei
13:59
created

Label::useInputId()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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 7
    public function attributes(array $attributes): self
31
    {
32 7
        $new = clone $this;
33 7
        $new->attributes = array_merge($this->attributes, $attributes);
34 7
        return $new;
35
    }
36
37 2
    public function replaceAttributes(array $attributes): self
38
    {
39 2
        $new = clone $this;
40 2
        $new->attributes = $attributes;
41 2
        return $new;
42
    }
43
44
    /**
45
     * Set tag ID.
46
     *
47
     * @param string|null $id Tag ID.
48
     */
49 2
    public function id(?string $id): self
50
    {
51 2
        $new = clone $this;
52 2
        $new->attributes['id'] = $id;
53 2
        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 18
    public function class(?string ...$class): self
62
    {
63 18
        $new = clone $this;
64 18
        Html::addCssClass(
65 18
            $new->attributes,
66 18
            array_filter($class, static fn ($c) => $c !== null),
67
        );
68 18
        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 7
    public function replaceClass(?string ...$class): self
77
    {
78 7
        $new = clone $this;
79 7
        $new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
80 7
        return $new;
81
    }
82
83 5
    public function setFor(bool $value): self
84
    {
85 5
        $new = clone $this;
86 5
        $new->setFor = $value;
87 5
        return $new;
88
    }
89
90
    /**
91
     * @return static
92
     */
93 6
    public function forId(?string $id): self
94
    {
95 6
        $new = clone $this;
96 6
        $new->forId = $id;
97 6
        return $new;
98
    }
99
100
    /**
101
     * @return static
102
     */
103 21
    public function useInputId(bool $value): self
104
    {
105 21
        $new = clone $this;
106 21
        $new->useInputId = $value;
107 21
        return $new;
108
    }
109
110
    /**
111
     * @return static
112
     */
113 14
    public function content(string|Stringable|null $content): self
114
    {
115 14
        $new = clone $this;
116 14
        $new->content = $content;
117 14
        return $new;
118
    }
119
120
    /**
121
     * Whether content should be HTML-encoded.
122
     *
123
     * @return static
124
     */
125 2
    public function encode(bool $value): self
126
    {
127 2
        $new = clone $this;
128 2
        $new->encode = $value;
129 2
        return $new;
130
    }
131
132 260
    protected function run(): string
133
    {
134 260
        $useModel = $this->hasFormModelAndAttribute();
135
136 260
        $content = $useModel
137 165
            ? $this->content ?? $this->getFormAttributeLabel()
138 97
            : (string) $this->content;
139
140 260
        if ($content === '') {
141 92
            return '';
142
        }
143
144 170
        $labelAttributes = $this->attributes;
145
146 170
        if ($this->setFor && !isset($labelAttributes['for'])) {
147 166
            $id = $this->forId;
148 166
            if ($useModel && $id === null && $this->useInputId) {
149 136
                $id = $this->getInputId();
150
            }
151 166
            if ($id !== null) {
152 141
                $labelAttributes['for'] = $id;
153
            }
154
        }
155
156 170
        $tag = Html::label($content)->attributes($labelAttributes);
157
158 170
        if (!$this->encode) {
159 1
            $tag = $tag->encode(false);
160
        }
161
162 170
        return $tag->render();
163
    }
164
}
165