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