1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
9
|
|
|
use Yiisoft\Html\Tag\Label as LabelTag; |
10
|
|
|
use Yiisoft\Widget\Widget; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generates a label tag for the given form attribute. |
14
|
|
|
* |
15
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html |
16
|
|
|
* |
17
|
|
|
* @psalm-suppress MissingConstructor |
18
|
|
|
*/ |
19
|
|
|
final class Label extends Widget |
20
|
|
|
{ |
21
|
|
|
private string $attribute = ''; |
22
|
|
|
private ?string $for = ''; |
23
|
|
|
private bool $encode = true; |
24
|
|
|
private FormModelInterface $formModel; |
25
|
|
|
private ?string $label = ''; |
26
|
|
|
private array $tagAttributes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Specify a form, its attribute. |
30
|
|
|
* |
31
|
|
|
* @param FormModelInterface $formModel Form instance. |
32
|
|
|
* @param string $attribute Form model's property name this widget is rendered for. |
33
|
|
|
* |
34
|
|
|
* @return static |
35
|
|
|
* |
36
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
37
|
|
|
*/ |
38
|
145 |
|
public function config(FormModelInterface $formModel, string $attribute): self |
39
|
|
|
{ |
40
|
145 |
|
$new = clone $this; |
41
|
145 |
|
$new->formModel = $formModel; |
42
|
145 |
|
$new->attribute = $attribute; |
43
|
145 |
|
return $new; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Whether content should be HTML-encoded. |
48
|
|
|
* |
49
|
|
|
* @param bool $value |
50
|
|
|
* |
51
|
|
|
* @return static |
52
|
|
|
*/ |
53
|
142 |
|
public function encode(bool $value): self |
54
|
|
|
{ |
55
|
142 |
|
$new = clone $this; |
56
|
142 |
|
$new->encode = $value; |
57
|
142 |
|
return $new; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The id of a labelable form-related element in the same document as the tag label element. |
62
|
|
|
* |
63
|
|
|
* The first element in the document with an id matching the value of the for attribute is the labeled control for |
64
|
|
|
* this label element, if it is a labelable element. |
65
|
|
|
* |
66
|
|
|
* @param string|null $value The id of a labelable form-related element in the same document as the tag label |
67
|
|
|
* element. If null, the attribute will be removed. |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
* |
71
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html#label.attrs.for |
72
|
|
|
*/ |
73
|
143 |
|
public function for(?string $value): self |
74
|
|
|
{ |
75
|
143 |
|
$new = clone $this; |
76
|
143 |
|
$new->for = $value; |
77
|
143 |
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* This specifies the label to be displayed. |
82
|
|
|
* |
83
|
|
|
* @param string $value The label to be displayed. |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
* |
87
|
|
|
* Note that this will NOT be encoded. |
88
|
|
|
* - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for |
89
|
|
|
* display (after encoding). |
90
|
|
|
*/ |
91
|
143 |
|
public function label(?string $value): self |
92
|
|
|
{ |
93
|
143 |
|
$new = clone $this; |
94
|
143 |
|
$new->label = $value; |
95
|
143 |
|
return $new; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* HTML attributes for the widget container tag. |
100
|
|
|
* |
101
|
|
|
* @param array $value |
102
|
|
|
* |
103
|
|
|
* @return static |
104
|
|
|
* |
105
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
106
|
|
|
*/ |
107
|
140 |
|
public function tagAttributes(array $value): self |
108
|
|
|
{ |
109
|
140 |
|
$new = clone $this; |
110
|
140 |
|
$new->tagAttributes = $value; |
111
|
140 |
|
return $new; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string the generated label tag. |
116
|
|
|
*/ |
117
|
145 |
|
protected function run(): string |
118
|
|
|
{ |
119
|
145 |
|
$new = clone $this; |
120
|
|
|
|
121
|
145 |
|
if ($new->label === '') { |
122
|
136 |
|
$new->label = HtmlForm::getAttributeLabel($new->formModel, $new->attribute); |
123
|
|
|
} |
124
|
|
|
|
125
|
145 |
|
if ($new->for === '') { |
126
|
141 |
|
$new->for = HtmlForm::getInputId($new->formModel, $new->attribute); |
127
|
|
|
} |
128
|
|
|
|
129
|
145 |
|
return $new->label !== null |
130
|
142 |
|
? LabelTag::tag() |
131
|
142 |
|
->attributes($new->tagAttributes) |
132
|
142 |
|
->content($new->label) |
133
|
142 |
|
->encode($new->encode) |
134
|
142 |
|
->forId($new->for) |
135
|
142 |
|
->render() |
136
|
145 |
|
: ''; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|