Label::render()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

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