1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Part; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Form\Field\Base\FormAttributeTrait; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
use Yiisoft\Widget\Widget; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Represents hint for a form field. |
15
|
|
|
*/ |
16
|
|
|
final class Hint extends Widget |
17
|
|
|
{ |
18
|
|
|
use FormAttributeTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @psalm-var non-empty-string |
22
|
|
|
*/ |
23
|
|
|
private string $tag = 'div'; |
24
|
|
|
private array $attributes = []; |
25
|
|
|
|
26
|
|
|
private string|Stringable|null $content = null; |
27
|
|
|
|
28
|
|
|
private bool $encode = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set the container tag name for the hint. |
32
|
|
|
* |
33
|
|
|
* @param string $tag Container tag name. |
34
|
|
|
*/ |
35
|
5 |
|
public function tag(string $tag): self |
36
|
|
|
{ |
37
|
5 |
|
if ($tag === '') { |
38
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
$new = clone $this; |
42
|
4 |
|
$new->tag = $tag; |
43
|
4 |
|
return $new; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
public function attributes(array $attributes): self |
47
|
|
|
{ |
48
|
5 |
|
$new = clone $this; |
49
|
5 |
|
$new->attributes = $attributes; |
50
|
5 |
|
return $new; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
public function addAttributes(array $attributes): self |
54
|
|
|
{ |
55
|
6 |
|
$new = clone $this; |
56
|
6 |
|
$new->attributes = array_merge($this->attributes, $attributes); |
57
|
6 |
|
return $new; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set tag ID. |
62
|
|
|
* |
63
|
|
|
* @param string|null $id Tag ID. |
64
|
|
|
*/ |
65
|
2 |
|
public function id(?string $id): self |
66
|
|
|
{ |
67
|
2 |
|
$new = clone $this; |
68
|
2 |
|
$new->attributes['id'] = $id; |
69
|
2 |
|
return $new; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Replace tag CSS classes with a new set of classes. |
74
|
|
|
* |
75
|
|
|
* @param string|null ...$class One or many CSS classes. |
76
|
|
|
*/ |
77
|
9 |
|
public function class(?string ...$class): self |
78
|
|
|
{ |
79
|
9 |
|
$new = clone $this; |
80
|
9 |
|
$new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null); |
81
|
9 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add one or more CSS classes to the tag. |
86
|
|
|
* |
87
|
|
|
* @param string|null ...$class One or many CSS classes. |
88
|
|
|
*/ |
89
|
10 |
|
public function addClass(?string ...$class): self |
90
|
|
|
{ |
91
|
10 |
|
$new = clone $this; |
92
|
10 |
|
Html::addCssClass( |
93
|
10 |
|
$new->attributes, |
94
|
10 |
|
array_filter($class, static fn ($c) => $c !== null), |
95
|
10 |
|
); |
96
|
10 |
|
return $new; |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
public function content(string|Stringable|null $content): self |
100
|
|
|
{ |
101
|
12 |
|
$new = clone $this; |
102
|
12 |
|
$new->content = $content; |
103
|
12 |
|
return $new; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Whether content should be HTML-encoded. |
108
|
|
|
*/ |
109
|
2 |
|
public function encode(bool $value): self |
110
|
|
|
{ |
111
|
2 |
|
$new = clone $this; |
112
|
2 |
|
$new->encode = $value; |
113
|
2 |
|
return $new; |
114
|
|
|
} |
115
|
|
|
|
116
|
517 |
|
public function render(): string |
117
|
|
|
{ |
118
|
517 |
|
$content = $this->hasFormModelAndAttribute() |
119
|
405 |
|
? $this->content ?? $this->getFormAttributeHint() |
120
|
114 |
|
: (string) $this->content; |
121
|
|
|
|
122
|
517 |
|
return $content === '' |
123
|
437 |
|
? '' |
124
|
517 |
|
: Html::tag($this->tag, $content, $this->attributes) |
125
|
517 |
|
->encode($this->encode) |
126
|
517 |
|
->render(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|