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