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\Label; |
9
|
|
|
|
10
|
|
|
final class FieldFactory |
11
|
|
|
{ |
12
|
|
|
private ?string $template; |
13
|
|
|
|
14
|
|
|
private ?bool $setInputIdAttribute; |
15
|
|
|
|
16
|
|
|
private array $labelConfig; |
17
|
|
|
private array $hintConfig; |
18
|
|
|
|
19
|
|
|
private array $inputTextConfig; |
20
|
|
|
|
21
|
|
|
public function __construct(FieldFactoryConfig $config) |
22
|
|
|
{ |
23
|
|
|
$this->template = $config->getTemplate(); |
24
|
|
|
|
25
|
|
|
$this->setInputIdAttribute = $config->getSetInputIdAttribute(); |
26
|
|
|
|
27
|
|
|
$this->labelConfig = $config->getLabelConfig(); |
28
|
|
|
$this->hintConfig = $config->getHintConfig(); |
29
|
|
|
|
30
|
|
|
$this->inputTextConfig = $config->getInputTextConfig(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function label(FormModelInterface $formModel, string $attribute): Label |
34
|
|
|
{ |
35
|
|
|
return Label::widget($this->makeLabelConfig())->attribute($formModel, $attribute); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function hint(FormModelInterface $formModel, string $attribute): Label |
39
|
|
|
{ |
40
|
|
|
return Label::widget($this->hintConfig)->attribute($formModel, $attribute); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function inputText(FormModelInterface $formModel, string $attribute): InputText |
44
|
|
|
{ |
45
|
|
|
$config = array_merge( |
46
|
|
|
$this->makeFieldConfig([ |
47
|
|
|
'template()', |
48
|
|
|
'setInputIdAttribute()', |
49
|
|
|
'labelConfig()', |
50
|
|
|
'hintConfig()', |
51
|
|
|
]), |
52
|
|
|
$this->inputTextConfig |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
return InputText::widget($config)->attribute($formModel, $attribute); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string[] $keys |
60
|
|
|
*/ |
61
|
|
|
private function makeFieldConfig(array $keys): array |
62
|
|
|
{ |
63
|
|
|
$config = []; |
64
|
|
|
foreach ($keys as $key) { |
65
|
|
|
switch ($key) { |
66
|
|
|
case 'template()': |
67
|
|
|
if ($this->template !== null) { |
68
|
|
|
$config[$key] = [$this->template]; |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
|
72
|
|
|
case 'setInputIdAttribute()': |
73
|
|
|
if ($this->setInputIdAttribute !== null) { |
74
|
|
|
$config[$key] = [$this->setInputIdAttribute]; |
75
|
|
|
} |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'labelConfig()': |
79
|
|
|
$labelConfig = $this->makeLabelConfig(); |
80
|
|
|
if ($labelConfig !== []) { |
81
|
|
|
$config[$key] = [$labelConfig]; |
82
|
|
|
} |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case 'hintConfig()': |
86
|
|
|
if ($this->hintConfig !== []) { |
87
|
|
|
$config[$key] = [$this->hintConfig]; |
88
|
|
|
} |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $config; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function makeLabelConfig(): array |
96
|
|
|
{ |
97
|
|
|
$config = []; |
98
|
|
|
|
99
|
|
|
if ($this->setInputIdAttribute !== null) { |
100
|
|
|
$config['useInputIdAttribute()'] = [$this->setInputIdAttribute]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array_merge($config, $this->labelConfig); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|