1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
8
|
|
|
use Yiisoft\Form\Field\Part\Label; |
9
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
10
|
|
|
use Yiisoft\Html\Tag\Base\ContentTagInterface; |
|
|
|
|
11
|
|
|
use Yiisoft\Html\Tag\Base\Tag; |
12
|
|
|
use Yiisoft\Html\Tag\Div; |
13
|
|
|
use Yiisoft\Widget\Widget; |
14
|
|
|
|
15
|
|
|
abstract class AbstractField extends Widget |
16
|
|
|
{ |
17
|
|
|
use FormAttributeTrait; |
18
|
|
|
|
19
|
|
|
private ?ContentTagInterface $containerTag = null; |
20
|
|
|
private bool $withoutContainer = false; |
21
|
|
|
|
22
|
|
|
private string $template = "{label}\n{input}\n{hint}\n{error}"; |
23
|
|
|
|
24
|
|
|
private ?string $inputId = null; |
25
|
|
|
private bool $setInputIdAttribute = true; |
26
|
|
|
|
27
|
|
|
private array $labelConfig = []; |
28
|
|
|
private array $hintConfig = []; |
29
|
|
|
|
30
|
|
|
final public function containerTag(?ContentTagInterface $tag): self |
31
|
|
|
{ |
32
|
|
|
$new = clone $this; |
33
|
|
|
$new->containerTag = $tag; |
34
|
|
|
return $new; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
final public function withoutContainer(): self |
38
|
|
|
{ |
39
|
|
|
$new = clone $this; |
40
|
|
|
$new->withoutContainer = true; |
41
|
|
|
return $new; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set layout template for render a field. |
46
|
|
|
* |
47
|
|
|
* @param string $template |
48
|
|
|
* |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
final public function template(string $template): self |
52
|
|
|
{ |
53
|
|
|
$new = clone $this; |
54
|
|
|
$new->template = $template; |
55
|
|
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
final public function inputId(?string $inputId): self |
62
|
|
|
{ |
63
|
|
|
$new = clone $this; |
64
|
|
|
$new->inputId = $inputId; |
65
|
|
|
return $new; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
|
|
final public function setInputIdAttribute(bool $value): self |
72
|
|
|
{ |
73
|
|
|
$new = clone $this; |
74
|
|
|
$new->setInputIdAttribute = $value; |
75
|
|
|
return $new; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
|
|
final public function labelConfig(array $config): self |
82
|
|
|
{ |
83
|
|
|
$new = clone $this; |
84
|
|
|
$new->labelConfig = $config; |
85
|
|
|
return $new; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
|
|
final public function label(?string $content): self |
92
|
|
|
{ |
93
|
|
|
$new = clone $this; |
94
|
|
|
$new->labelConfig['content()'] = [$content]; |
95
|
|
|
return $new; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return static |
100
|
|
|
*/ |
101
|
|
|
final public function hintConfig(array $config): self |
102
|
|
|
{ |
103
|
|
|
$new = clone $this; |
104
|
|
|
$new->hintConfig = $config; |
105
|
|
|
return $new; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
final public function hint(?string $content): self |
109
|
|
|
{ |
110
|
|
|
$new = clone $this; |
111
|
|
|
$new->hintConfig['content()'] = [$content]; |
112
|
|
|
return $new; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
final protected function getInputName(): string |
116
|
|
|
{ |
117
|
|
|
return HtmlForm::getInputName($this->getFormModel(), $this->attribute); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
final protected function getInputId(): ?string |
121
|
|
|
{ |
122
|
|
|
if (!$this->setInputIdAttribute) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->inputId ?? HtmlForm::getInputId($this->getFormModel(), $this->attribute); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
final protected function prepareIdInInputTag(Tag $tag): Tag |
130
|
|
|
{ |
131
|
|
|
if ( |
132
|
|
|
$this->setInputIdAttribute |
133
|
|
|
&& $tag->getAttribute('id') === null |
|
|
|
|
134
|
|
|
) { |
135
|
|
|
$id = $this->getInputId(); |
136
|
|
|
if ($id !== null) { |
137
|
|
|
$tag = $tag->id($id); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $tag; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
final protected function run(): string |
145
|
|
|
{ |
146
|
|
|
if ($this->withoutContainer) { |
147
|
|
|
return $this->generateContent(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$containerTag = $this->containerTag ?? Div::tag(); |
151
|
|
|
|
152
|
|
|
return $containerTag->open() |
153
|
|
|
. PHP_EOL |
154
|
|
|
. $this->generateContent() |
155
|
|
|
. PHP_EOL |
156
|
|
|
. $containerTag->close(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
abstract protected function generateInput(): string; |
160
|
|
|
|
161
|
|
|
private function generateContent(): string |
162
|
|
|
{ |
163
|
|
|
$parts = [ |
164
|
|
|
'{label}' => $this->generateLabel(), |
165
|
|
|
'{input}' => $this->generateInput(), |
166
|
|
|
'{hint}' => $this->generateHint(), |
167
|
|
|
'{error}' => $this->generateError(), |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
return preg_replace('/^\h*\v+/m', '', trim(strtr($this->template, $parts))); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function generateLabel(): string |
174
|
|
|
{ |
175
|
|
|
$config = $this->labelConfig; |
176
|
|
|
|
177
|
|
|
if ( |
178
|
|
|
$this->setInputIdAttribute === false |
179
|
|
|
&& ($config['useInputIdAttribute()'] ?? [null]) === [null] |
180
|
|
|
) { |
181
|
|
|
$config['useInputIdAttribute()'] = [false]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return Label::widget($config) |
185
|
|
|
->attribute($this->getFormModel(), $this->attribute) |
|
|
|
|
186
|
|
|
->render(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function generateHint(): string |
190
|
|
|
{ |
191
|
|
|
return Hint::widget($this->hintConfig) |
192
|
|
|
->attribute($this->getFormModel(), $this->attribute) |
193
|
|
|
->render(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
private function generateError(): string |
197
|
|
|
{ |
198
|
|
|
return ''; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths