Passed
Pull Request — master (#192)
by Alexander
05:43 queued 02:47
created

FieldFactory::image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
9
use Yiisoft\Form\Field\Base\InputField;
10
use Yiisoft\Form\Field\Base\PartsField;
11
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface;
12
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
13
use Yiisoft\Form\Field\Button;
14
use Yiisoft\Form\Field\ButtonGroup;
15
use Yiisoft\Form\Field\Checkbox;
16
use Yiisoft\Form\Field\CheckboxList;
17
use Yiisoft\Form\Field\Date;
18
use Yiisoft\Form\Field\DateTime;
19
use Yiisoft\Form\Field\DateTimeLocal;
20
use Yiisoft\Form\Field\Email;
21
use Yiisoft\Form\Field\ErrorSummary;
22
use Yiisoft\Form\Field\Fieldset;
23
use Yiisoft\Form\Field\File;
24
use Yiisoft\Form\Field\Hidden;
25
use Yiisoft\Form\Field\Image;
26
use Yiisoft\Form\Field\Number;
27
use Yiisoft\Form\Field\Part\Error;
28
use Yiisoft\Form\Field\Part\Hint;
29
use Yiisoft\Form\Field\Part\Label;
30
use Yiisoft\Form\Field\Password;
31
use Yiisoft\Form\Field\RadioList;
32
use Yiisoft\Form\Field\Range;
33
use Yiisoft\Form\Field\ResetButton;
34
use Yiisoft\Form\Field\Select;
35
use Yiisoft\Form\Field\SubmitButton;
36
use Yiisoft\Form\Field\Telephone;
37
use Yiisoft\Form\Field\Text;
38
use Yiisoft\Form\Field\Textarea;
39
use Yiisoft\Form\Field\Url;
40
use Yiisoft\Widget\WidgetFactory;
41
42
final class FieldFactory
43
{
44
    /**
45
     * @param array[] $fieldConfigs
46
     */
47 26
    public function __construct(
48
        private ?string $containerTag = null,
49
        private array $containerTagAttributes = [],
50
        private ?bool $useContainer = null,
51
        private ?string $template = null,
52
        private ?string $templateBegin = null,
53
        private ?string $templateEnd = null,
54
        private ?bool $setInputIdAttribute = null,
55
        private array $inputTagAttributes = [],
56
        private array $labelConfig = [],
57
        private array $hintConfig = [],
58
        private array $errorConfig = [],
59
        private ?bool $usePlaceholder = null,
60
        private ?string $validClass = null,
61
        private ?string $invalidClass = null,
62
        private ?bool $enrichmentFromRules = null,
63
        private array $fieldConfigs = [],
64
    ) {
65
    }
66
67 1
    public function button(array $config = []): Button
68
    {
69 1
        return $this->field(Button::class, $config);
70
    }
71
72 1
    public function buttonGroup(array $config = []): ButtonGroup
73
    {
74 1
        return $this->field(ButtonGroup::class, $config);
75
    }
76
77 1
    public function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox
78
    {
79 1
        return $this->input(Checkbox::class, $formModel, $attribute, $config);
80
    }
81
82 1
    public function checkboxList(FormModelInterface $formModel, string $attribute, array $config = []): CheckboxList
83
    {
84
        return $this
85 1
            ->field(CheckboxList::class, $config)
86 1
            ->attribute($formModel, $attribute);
87
    }
88
89 1
    public function date(FormModelInterface $formModel, string $attribute, array $config = []): Date
90
    {
91 1
        return $this->input(Date::class, $formModel, $attribute, $config);
92
    }
93
94 1
    public function dateTime(FormModelInterface $formModel, string $attribute, array $config = []): DateTime
95
    {
96 1
        return $this->input(DateTime::class, $formModel, $attribute, $config);
97
    }
98
99 1
    public function dateTimeLocal(FormModelInterface $formModel, string $attribute, array $config = []): DateTimeLocal
100
    {
101 1
        return $this->input(DateTimeLocal::class, $formModel, $attribute, $config);
102
    }
103
104 1
    public function email(FormModelInterface $formModel, string $attribute, array $config = []): Email
105
    {
106 1
        return $this->input(Email::class, $formModel, $attribute, $config);
107
    }
108
109 3
    public function errorSummary(FormModelInterface $formModel, array $config = []): ErrorSummary
110
    {
111
        return $this
112 3
            ->field(ErrorSummary::class, $config)
113 3
            ->formModel($formModel);
114
    }
115
116 4
    public function fieldset(array $config = []): Fieldset
117
    {
118 4
        return $this->field(Fieldset::class, $config);
119
    }
120
121 1
    public function file(FormModelInterface $formModel, string $attribute, array $config = []): File
122
    {
123 1
        return $this->input(File::class, $formModel, $attribute, $config);
124
    }
125
126 2
    public function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden
127
    {
128 2
        return $this->input(Hidden::class, $formModel, $attribute, $config);
129
    }
130
131 1
    public function image(array $config = []): Image
132
    {
133 1
        return $this->field(Image::class, $config);
134
    }
135
136 1
    public function number(FormModelInterface $formModel, string $attribute, array $config = []): Number
137
    {
138 1
        return $this->input(Number::class, $formModel, $attribute, $config);
139
    }
140
141 1
    public function password(FormModelInterface $formModel, string $attribute, array $config = []): Password
142
    {
143 1
        return $this->input(Password::class, $formModel, $attribute, $config);
144
    }
145
146 1
    public function radioList(FormModelInterface $formModel, string $attribute, array $config = []): RadioList
147
    {
148
        return $this
149 1
            ->field(RadioList::class, $config)
150 1
            ->attribute($formModel, $attribute);
151
    }
152
153 1
    public function range(FormModelInterface $formModel, string $attribute, array $config = []): Range
154
    {
155 1
        return $this->input(Range::class, $formModel, $attribute, $config);
156
    }
157
158 1
    public function resetButton(array $config = []): ResetButton
159
    {
160 1
        return $this->field(ResetButton::class, $config);
161
    }
162
163 1
    public function select(FormModelInterface $formModel, string $attribute, array $config = []): Select
164
    {
165 1
        return $this->input(Select::class, $formModel, $attribute, $config);
166
    }
167
168 1
    public function submitButton(array $config = []): SubmitButton
169
    {
170 1
        return $this->field(SubmitButton::class, $config);
171
    }
172
173 1
    public function telephone(FormModelInterface $formModel, string $attribute, array $config = []): Telephone
174
    {
175 1
        return $this->input(Telephone::class, $formModel, $attribute, $config);
176
    }
177
178 15
    public function text(FormModelInterface $formModel, string $attribute, array $config = []): Text
179
    {
180 15
        return $this->input(Text::class, $formModel, $attribute, $config);
181
    }
182
183 1
    public function textarea(FormModelInterface $formModel, string $attribute, array $config = []): Textarea
184
    {
185 1
        return $this->input(Textarea::class, $formModel, $attribute, $config);
186
    }
187
188 1
    public function url(FormModelInterface $formModel, string $attribute, array $config = []): Url
189
    {
190 1
        return $this->input(Url::class, $formModel, $attribute, $config);
191
    }
192
193 4
    public function label(FormModelInterface $formModel, string $attribute, array $config = []): Label
194
    {
195 4
        $widgetConfig = array_merge(
196 4
            $this->labelConfig,
197
            $config,
198
        );
199 4
        return Label::widget($widgetConfig)->attribute($formModel, $attribute);
0 ignored issues
show
Bug introduced by
The method attribute() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Field\Part\Label or Yiisoft\Form\Field\Part\Error or Yiisoft\Form\Field\Part\Hint or Yiisoft\Form\Field\RadioList or Yiisoft\Form\Field\Base\InputField or Yiisoft\Form\Field\CheckboxList. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        return Label::widget($widgetConfig)->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
200
    }
201
202 3
    public function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
203
    {
204 3
        $widgetConfig = array_merge(
205 3
            $this->hintConfig,
206
            $config,
207
        );
208 3
        return Hint::widget($widgetConfig)->attribute($formModel, $attribute);
209
    }
210
211 3
    public function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
212
    {
213 3
        $widgetConfig = array_merge(
214 3
            $this->errorConfig,
215
            $config,
216
        );
217 3
        return Error::widget($widgetConfig)->attribute($formModel, $attribute);
218
    }
219
220
    /**
221
     * @psalm-template T
222
     * @psalm-param class-string<T> $class
223
     * @psalm-return T
224
     */
225 32
    public function input(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object
226
    {
227 32
        $widget = $this->field($class, $config);
228 32
        if (!$widget instanceof InputField) {
229 1
            throw new InvalidArgumentException(
230 1
                sprintf(
231
                    'Input widget must be instance of "%s".',
232
                    InputField::class
233
                )
234
            );
235
        }
236
237 31
        return $widget->attribute($formModel, $attribute);
238
    }
239
240
    /**
241
     * @psalm-template T
242
     * @psalm-param class-string<T> $class
243
     * @psalm-return T
244
     */
245 47
    public function field(string $class, array $config = []): object
246
    {
247 47
        $config = array_merge(
248 47
            $this->makeFieldConfig($class),
249 47
            $this->fieldConfigs[$class] ?? [],
250
            $config,
251 47
            ['class' => $class],
252
        );
253
254
        /** @psalm-var T */
255 47
        return WidgetFactory::createWidget($config);
256
    }
257
258
    /**
259
     * @psalm-param class-string $class
260
     */
261 47
    private function makeFieldConfig(string $class): array
262
    {
263 47
        $config = [];
264
265 47
        if ($this->containerTag !== null) {
266 2
            $config['containerTag()'] = [$this->containerTag];
267
        }
268 47
        if ($this->containerTagAttributes !== []) {
269 4
            $config['containerTagAttributes()'] = [$this->containerTagAttributes];
270
        }
271 47
        if ($this->useContainer !== null) {
272 1
            $config['useContainer()'] = [$this->useContainer];
273
        }
274
275 47
        if (is_a($class, PartsField::class, true)) {
276 43
            if ($this->template !== null) {
277 1
                $config['template()'] = [$this->template];
278
            }
279 43
            if ($this->templateBegin !== null) {
280 1
                $config['templateBegin()'] = [$this->templateBegin];
281
            }
282 43
            if ($this->templateEnd !== null) {
283 1
                $config['templateEnd()'] = [$this->templateEnd];
284
            }
285 43
            if ($this->labelConfig !== []) {
286 1
                $config['labelConfig()'] = [$this->labelConfig];
287
            }
288 43
            if ($this->hintConfig !== []) {
289 1
                $config['hintConfig()'] = [$this->hintConfig];
290
            }
291 43
            if ($this->errorConfig !== []) {
292 1
                $config['errorConfig()'] = [$this->errorConfig];
293
            }
294
        }
295
296 47
        if (is_a($class, InputField::class, true)) {
297 31
            if ($this->setInputIdAttribute !== null) {
298 1
                $config['setInputIdAttribute()'] = [$this->setInputIdAttribute];
299 1
                if ($this->setInputIdAttribute === false) {
300 1
                    $config['labelConfig()'] = [
301 1
                        $this->labelConfig + ['useInputIdAttribute()' => [false]],
302
                    ];
303
                }
304
            }
305 31
            if ($this->inputTagAttributes !== []) {
306 2
                $config['inputTagAttributes()'] = [$this->inputTagAttributes];
307
            }
308
        }
309
310 47
        if (is_a($class, PlaceholderInterface::class, true)) {
311 22
            if ($this->usePlaceholder !== null) {
312 2
                $config['usePlaceholder()'] = [$this->usePlaceholder];
313
            }
314
        }
315
316 47
        if (is_a($class, EnrichmentFromRulesInterface::class, true)) {
317 28
            if ($this->enrichmentFromRules !== null) {
318 1
                $config['enrichmentFromRules()'] = [$this->enrichmentFromRules];
319
            }
320
        }
321
322 47
        if (is_a($class, ValidationClassInterface::class, true)) {
323 31
            if ($this->validClass !== null) {
324 1
                $config['validClass()'] = [$this->validClass];
325
            }
326 31
            if ($this->invalidClass !== null) {
327 1
                $config['invalidClass()'] = [$this->invalidClass];
328
            }
329
        }
330
331 47
        return $config;
332
    }
333
}
334