Test Failed
Pull Request — master (#244)
by Sergei
03:29
created

Label::getThemeConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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