|
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 $tagAttributes = []; |
|
20
|
|
|
|
|
21
|
|
|
private bool $setForAttribute = true; |
|
22
|
|
|
|
|
23
|
|
|
private ?string $forId = null; |
|
24
|
|
|
private bool $useInputIdAttribute = true; |
|
25
|
|
|
|
|
26
|
|
|
private string|Stringable|null $content = null; |
|
27
|
|
|
|
|
28
|
|
|
private bool $encode = true; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return static |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function tagAttributes(array $attributes): self |
|
34
|
|
|
{ |
|
35
|
2 |
|
$new = clone $this; |
|
36
|
2 |
|
$new->tagAttributes = $attributes; |
|
37
|
2 |
|
return $new; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function setForAttribute(bool $value): self |
|
41
|
|
|
{ |
|
42
|
5 |
|
$new = clone $this; |
|
43
|
5 |
|
$new->setForAttribute = $value; |
|
44
|
5 |
|
return $new; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return static |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function forId(?string $id): self |
|
51
|
|
|
{ |
|
52
|
6 |
|
$new = clone $this; |
|
53
|
6 |
|
$new->forId = $id; |
|
54
|
6 |
|
return $new; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
21 |
|
public function useInputIdAttribute(bool $value): self |
|
61
|
|
|
{ |
|
62
|
21 |
|
$new = clone $this; |
|
63
|
21 |
|
$new->useInputIdAttribute = $value; |
|
64
|
21 |
|
return $new; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return static |
|
69
|
|
|
*/ |
|
70
|
9 |
|
public function content(string|Stringable|null $content): self |
|
71
|
|
|
{ |
|
72
|
9 |
|
$new = clone $this; |
|
73
|
9 |
|
$new->content = $content; |
|
74
|
9 |
|
return $new; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Whether content should be HTML-encoded. |
|
79
|
|
|
* |
|
80
|
|
|
* @return static |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function encode(bool $value): self |
|
83
|
|
|
{ |
|
84
|
2 |
|
$new = clone $this; |
|
85
|
2 |
|
$new->encode = $value; |
|
86
|
2 |
|
return $new; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
218 |
|
protected function run(): string |
|
90
|
|
|
{ |
|
91
|
218 |
|
$useModel = $this->hasFormModelAndAttribute(); |
|
92
|
|
|
|
|
93
|
218 |
|
$content = $useModel |
|
94
|
139 |
|
? $this->content ?? $this->getAttributeLabel() |
|
95
|
81 |
|
: (string) $this->content; |
|
96
|
|
|
|
|
97
|
218 |
|
if ($content === '') { |
|
98
|
81 |
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
139 |
|
$tagAttributes = $this->tagAttributes; |
|
102
|
|
|
|
|
103
|
139 |
|
if ($this->setForAttribute && !isset($tagAttributes['for'])) { |
|
104
|
135 |
|
$id = $this->forId; |
|
105
|
135 |
|
if ($useModel && $id === null && $this->useInputIdAttribute) { |
|
106
|
110 |
|
$id = $this->getInputId(); |
|
107
|
|
|
} |
|
108
|
135 |
|
if ($id !== null) { |
|
109
|
115 |
|
$tagAttributes['for'] = $id; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
139 |
|
$tag = Html::label($content)->attributes($tagAttributes); |
|
114
|
|
|
|
|
115
|
139 |
|
if (!$this->encode) { |
|
116
|
1 |
|
$tag = $tag->encode(false); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
139 |
|
return $tag->render(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|