|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\InputText; |
|
8
|
|
|
use Yiisoft\Form\Field\Part\Error; |
|
9
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
|
10
|
|
|
use Yiisoft\Form\Field\Part\Label; |
|
11
|
|
|
|
|
12
|
|
|
final class FieldFactory |
|
13
|
|
|
{ |
|
14
|
|
|
private const PLACEHOLDER = 1; |
|
15
|
|
|
|
|
16
|
|
|
// |
|
17
|
|
|
// Common |
|
18
|
|
|
// |
|
19
|
|
|
|
|
20
|
|
|
private ?string $containerTag; |
|
21
|
|
|
private array $containerTagAttributes; |
|
22
|
|
|
private ?bool $useContainer; |
|
23
|
|
|
|
|
24
|
|
|
private ?string $template; |
|
25
|
|
|
|
|
26
|
|
|
private ?bool $setInputIdAttribute; |
|
27
|
|
|
|
|
28
|
|
|
private array $formElementTagAttributes; |
|
29
|
|
|
|
|
30
|
|
|
private array $labelConfig; |
|
31
|
|
|
private array $hintConfig; |
|
32
|
|
|
private array $errorConfig; |
|
33
|
|
|
|
|
34
|
|
|
// |
|
35
|
|
|
// Placeholder |
|
36
|
|
|
// |
|
37
|
|
|
|
|
38
|
|
|
private ?bool $usePlaceholder; |
|
39
|
|
|
|
|
40
|
|
|
// |
|
41
|
|
|
// Field configurations |
|
42
|
|
|
// |
|
43
|
|
|
|
|
44
|
|
|
private array $inputTextConfig; |
|
45
|
|
|
|
|
46
|
|
|
// |
|
47
|
|
|
// Internal properties |
|
48
|
|
|
// |
|
49
|
|
|
|
|
50
|
|
|
private ?array $baseFieldConfig = null; |
|
51
|
|
|
|
|
52
|
24 |
|
public function __construct(FieldFactoryConfig $config) |
|
53
|
|
|
{ |
|
54
|
24 |
|
$this->containerTag = $config->getContainerTag(); |
|
55
|
24 |
|
$this->containerTagAttributes = $config->getContainerTagAttributes(); |
|
56
|
24 |
|
$this->useContainer = $config->getUseContainer(); |
|
57
|
|
|
|
|
58
|
24 |
|
$this->template = $config->getTemplate(); |
|
59
|
|
|
|
|
60
|
24 |
|
$this->setInputIdAttribute = $config->getSetInputIdAttribute(); |
|
61
|
|
|
|
|
62
|
24 |
|
$this->formElementTagAttributes = $config->getFormElementTagAttributes(); |
|
63
|
|
|
|
|
64
|
24 |
|
$this->labelConfig = $config->getLabelConfig(); |
|
65
|
24 |
|
$this->hintConfig = $config->getHintConfig(); |
|
66
|
24 |
|
$this->errorConfig = $config->getErrorConfig(); |
|
67
|
|
|
|
|
68
|
24 |
|
$this->usePlaceholder = $config->getUsePlaceholder(); |
|
69
|
|
|
|
|
70
|
24 |
|
$this->inputTextConfig = $config->getInputTextConfig(); |
|
71
|
24 |
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
public function label(FormModelInterface $formModel, string $attribute, array $config = []): Label |
|
74
|
|
|
{ |
|
75
|
5 |
|
$widgetConfig = array_merge( |
|
76
|
5 |
|
$this->makeLabelConfig(), |
|
77
|
|
|
$config, |
|
78
|
|
|
); |
|
79
|
5 |
|
return Label::widget($widgetConfig)->attribute($formModel, $attribute); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
public function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint |
|
83
|
|
|
{ |
|
84
|
4 |
|
$widgetConfig = array_merge( |
|
85
|
4 |
|
$this->hintConfig, |
|
86
|
|
|
$config, |
|
87
|
|
|
); |
|
88
|
4 |
|
return Hint::widget($widgetConfig)->attribute($formModel, $attribute); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
public function error(FormModelInterface $formModel, string $attribute, array $config = []): Error |
|
92
|
|
|
{ |
|
93
|
4 |
|
$widgetConfig = array_merge( |
|
94
|
4 |
|
$this->errorConfig, |
|
95
|
|
|
$config, |
|
96
|
|
|
); |
|
97
|
4 |
|
return Error::widget($widgetConfig)->attribute($formModel, $attribute); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
11 |
|
public function inputText(FormModelInterface $formModel, string $attribute, array $config = []): InputText |
|
101
|
|
|
{ |
|
102
|
11 |
|
$config = array_merge( |
|
103
|
11 |
|
$this->makeFieldConfig(self::PLACEHOLDER), |
|
104
|
11 |
|
$this->inputTextConfig, |
|
105
|
|
|
$config, |
|
106
|
|
|
); |
|
107
|
11 |
|
return InputText::widget($config)->attribute($formModel, $attribute); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
11 |
|
private function makeFieldConfig(int ...$supports): array |
|
111
|
|
|
{ |
|
112
|
11 |
|
$config = $this->makeBaseFieldConfig(); |
|
113
|
11 |
|
foreach ($supports as $support) { |
|
114
|
|
|
switch ($support) { |
|
115
|
11 |
|
case self::PLACEHOLDER: |
|
116
|
11 |
|
if ($this->usePlaceholder !== null) { |
|
117
|
2 |
|
$config['usePlaceholder()'] = [$this->usePlaceholder]; |
|
118
|
|
|
} |
|
119
|
11 |
|
break; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
11 |
|
return $config; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
11 |
|
private function makeBaseFieldConfig(): array |
|
126
|
|
|
{ |
|
127
|
11 |
|
if ($this->baseFieldConfig === null) { |
|
128
|
11 |
|
$this->baseFieldConfig = []; |
|
129
|
|
|
|
|
130
|
11 |
|
if ($this->containerTag !== null) { |
|
131
|
2 |
|
$this->baseFieldConfig['containerTag()'] = [$this->containerTag]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
11 |
|
if ($this->containerTagAttributes !== []) { |
|
135
|
2 |
|
$this->baseFieldConfig['containerTagAttributes()'] = [$this->containerTagAttributes]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
11 |
|
if ($this->useContainer !== null) { |
|
139
|
1 |
|
$this->baseFieldConfig['useContainer()'] = [$this->useContainer]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
11 |
|
if ($this->template !== null) { |
|
143
|
1 |
|
$this->baseFieldConfig['template()'] = [$this->template]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
11 |
|
if ($this->setInputIdAttribute !== null) { |
|
147
|
1 |
|
$this->baseFieldConfig['setInputIdAttribute()'] = [$this->setInputIdAttribute]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
11 |
|
if ($this->formElementTagAttributes !== []) { |
|
151
|
2 |
|
$this->baseFieldConfig['formElementTagAttributes()'] = [$this->formElementTagAttributes]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
11 |
|
$labelConfig = $this->makeLabelConfig(); |
|
155
|
11 |
|
if ($labelConfig !== []) { |
|
156
|
2 |
|
$this->baseFieldConfig['labelConfig()'] = [$labelConfig]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
11 |
|
if ($this->hintConfig !== []) { |
|
160
|
1 |
|
$this->baseFieldConfig['hintConfig()'] = [$this->hintConfig]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
11 |
|
if ($this->errorConfig !== []) { |
|
164
|
1 |
|
$this->baseFieldConfig['errorConfig()'] = [$this->errorConfig]; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
11 |
|
return $this->baseFieldConfig; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
16 |
|
private function makeLabelConfig(): array |
|
171
|
|
|
{ |
|
172
|
16 |
|
$config = []; |
|
173
|
|
|
|
|
174
|
16 |
|
if ($this->setInputIdAttribute === false) { |
|
175
|
2 |
|
$config['useInputIdAttribute()'] = [false]; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
16 |
|
return array_merge($config, $this->labelConfig); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|