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