|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Html\Html; |
|
9
|
|
|
use Yiisoft\Html\Tag\CustomTag; |
|
10
|
|
|
use Yiisoft\Widget\Widget; |
|
11
|
|
|
|
|
12
|
|
|
abstract class BaseField extends Widget |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @psalm-var non-empty-string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected string $containerTag = 'div'; |
|
18
|
|
|
protected array $containerTagAttributes = []; |
|
19
|
|
|
protected bool $useContainer = true; |
|
20
|
|
|
|
|
21
|
|
|
private bool $isStartedByBegin = false; |
|
22
|
|
|
|
|
23
|
4 |
|
final public function containerTag(string $tag): static |
|
24
|
|
|
{ |
|
25
|
4 |
|
if ($tag === '') { |
|
26
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
$new = clone $this; |
|
30
|
3 |
|
$new->containerTag = $tag; |
|
31
|
3 |
|
return $new; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
final public function containerTagAttributes(array $attributes): static |
|
35
|
|
|
{ |
|
36
|
5 |
|
$new = clone $this; |
|
37
|
5 |
|
$new->containerTagAttributes = $attributes; |
|
38
|
5 |
|
return $new; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
171 |
|
final public function useContainer(bool $use): static |
|
42
|
|
|
{ |
|
43
|
171 |
|
$new = clone $this; |
|
44
|
171 |
|
$new->useContainer = $use; |
|
45
|
171 |
|
return $new; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
final public function begin(): ?string |
|
49
|
|
|
{ |
|
50
|
5 |
|
parent::begin(); |
|
51
|
5 |
|
$this->isStartedByBegin = true; |
|
52
|
|
|
|
|
53
|
5 |
|
$this->beforeRender(); |
|
54
|
|
|
|
|
55
|
5 |
|
$content = $this->generateBeginContent(); |
|
56
|
|
|
|
|
57
|
5 |
|
$html = $this->renderOpenContainerAndContent($content); |
|
58
|
5 |
|
if ($html === '') { |
|
59
|
1 |
|
return ''; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
return $html; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
407 |
|
final protected function run(): string |
|
66
|
|
|
{ |
|
67
|
407 |
|
if ($this->isStartedByBegin) { |
|
68
|
5 |
|
$this->isStartedByBegin = false; |
|
69
|
5 |
|
return $this->renderEnd(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
403 |
|
$this->beforeRender(); |
|
73
|
|
|
|
|
74
|
403 |
|
$content = $this->generateContent(); |
|
75
|
385 |
|
if ($content === null) { |
|
76
|
1 |
|
return ''; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
384 |
|
$result = $this->renderOpenContainerAndContent($content); |
|
80
|
|
|
|
|
81
|
384 |
|
if ($this->useContainer) { |
|
82
|
212 |
|
$result .= "\n" . Html::closeTag($this->containerTag); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
384 |
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function beforeRender(): void |
|
89
|
|
|
{ |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
abstract protected function generateContent(): ?string; |
|
93
|
|
|
|
|
94
|
2 |
|
protected function generateBeginContent(): string |
|
95
|
|
|
{ |
|
96
|
2 |
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
protected function generateEndContent(): string |
|
100
|
|
|
{ |
|
101
|
2 |
|
return ''; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function prepareContainerTagAttributes(array &$attributes): void |
|
105
|
|
|
{ |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
private function renderEnd(): string |
|
109
|
|
|
{ |
|
110
|
5 |
|
$content = $this->generateEndContent(); |
|
111
|
|
|
|
|
112
|
5 |
|
if (!$this->useContainer) { |
|
113
|
1 |
|
return $content; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
4 |
|
$containerTag = CustomTag::name($this->containerTag); |
|
117
|
|
|
|
|
118
|
4 |
|
return ($content !== '' ? $content . "\n" : '') . $containerTag->close(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
388 |
|
private function renderOpenContainerAndContent(string $content): string |
|
122
|
|
|
{ |
|
123
|
388 |
|
if (!$this->useContainer) { |
|
124
|
174 |
|
return $content; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
216 |
|
$containerTag = CustomTag::name($this->containerTag); |
|
128
|
|
|
|
|
129
|
216 |
|
$attributes = $this->containerTagAttributes; |
|
130
|
216 |
|
$this->prepareContainerTagAttributes($attributes); |
|
131
|
216 |
|
if ($attributes !== []) { |
|
132
|
7 |
|
$containerTag = $containerTag->attributes($attributes); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
216 |
|
return $containerTag->open() |
|
136
|
216 |
|
. ($content === '' ? '' : ("\n" . $content)); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|