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