|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\InputText; |
|
8
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
|
9
|
|
|
use Yiisoft\Html\Tag\Label; |
|
10
|
|
|
|
|
11
|
|
|
final class FieldFactory |
|
12
|
|
|
{ |
|
13
|
|
|
private ?string $template; |
|
14
|
|
|
|
|
15
|
|
|
private ?bool $setInputIdAttribute; |
|
16
|
|
|
|
|
17
|
|
|
private ?Label $labelTag; |
|
18
|
|
|
private ?bool $setLabelForAttribute; |
|
19
|
|
|
|
|
20
|
|
|
private array $inputTextConfig; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(FieldFactoryConfig $config) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->template = $config->getTemplate(); |
|
25
|
|
|
|
|
26
|
|
|
$this->setInputIdAttribute = $config->getSetInputIdAttribute(); |
|
27
|
|
|
|
|
28
|
|
|
$this->labelTag = $config->getLabelTag(); |
|
29
|
|
|
$this->setLabelForAttribute = $config->getSetLabelForAttribute(); |
|
30
|
|
|
|
|
31
|
|
|
$this->inputTextConfig = $config->getInputTextConfig(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function label(FormModelInterface $formModel, string $attribute): Label |
|
35
|
|
|
{ |
|
36
|
|
|
$tag = $this->labelTag ?? Label::tag(); |
|
37
|
|
|
$tag = $tag->content(HtmlForm::getAttributeLabel($formModel, $attribute)); |
|
38
|
|
|
|
|
39
|
|
|
if ( |
|
40
|
|
|
($this->setLabelForAttribute ?? true) |
|
41
|
|
|
&& $tag->getAttribute('for') === null |
|
|
|
|
|
|
42
|
|
|
) { |
|
43
|
|
|
$id = $this->getInputId($formModel, $attribute); |
|
44
|
|
|
if ($id !== null) { |
|
45
|
|
|
$tag = $tag->forId($id); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $tag; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function inputText(FormModelInterface $formModel, string $attribute): InputText |
|
53
|
|
|
{ |
|
54
|
|
|
$config = array_merge( |
|
55
|
|
|
$this->makeWidgetConfig([ |
|
56
|
|
|
'template()', |
|
57
|
|
|
'setInputIdAttribute()', |
|
58
|
|
|
'labelTag()', |
|
59
|
|
|
'setLabelForAttribute()', |
|
60
|
|
|
]), |
|
61
|
|
|
$this->inputTextConfig |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
return InputText::widget($config)->attribute($formModel, $attribute); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function makeWidgetConfig(array $keys): array |
|
68
|
|
|
{ |
|
69
|
|
|
$config = []; |
|
70
|
|
|
foreach ($keys as $key) { |
|
71
|
|
|
switch ($key) { |
|
72
|
|
|
case 'template()': |
|
73
|
|
|
if ($this->template !== null) { |
|
74
|
|
|
$config[$key] = [$this->template]; |
|
75
|
|
|
} |
|
76
|
|
|
break; |
|
77
|
|
|
|
|
78
|
|
|
case 'setInputIdAttribute()': |
|
79
|
|
|
if ($this->setInputIdAttribute !== null) { |
|
80
|
|
|
$config[$key] = [$this->setInputIdAttribute]; |
|
81
|
|
|
} |
|
82
|
|
|
break; |
|
83
|
|
|
|
|
84
|
|
|
case 'labelTag()': |
|
85
|
|
|
if ($this->labelTag !== null) { |
|
86
|
|
|
$config[$key] = [$this->labelTag]; |
|
87
|
|
|
} |
|
88
|
|
|
break; |
|
89
|
|
|
|
|
90
|
|
|
case 'setLabelForAttribute()': |
|
91
|
|
|
if ($this->setLabelForAttribute !== null) { |
|
92
|
|
|
$config[$key] = [$this->setLabelForAttribute]; |
|
93
|
|
|
} |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $config; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getInputId(FormModelInterface $formModel, string $attribute): ?string |
|
101
|
|
|
{ |
|
102
|
|
|
if (!($this->setInputIdAttribute ?? true)) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->inputId ?? HtmlForm::getInputId($formModel, $attribute); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.