Test Failed
Pull Request — master (#159)
by Alexander
02:49
created

FieldFactory::inputText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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\Error;
9
use Yiisoft\Form\Field\Part\Hint;
10
use Yiisoft\Form\Field\Part\Label;
11
12
final class FieldFactory
13
{
14
    private ?string $template;
15
16
    private ?bool $setInputIdAttribute;
17
18
    private array $labelConfig;
19
    private array $hintConfig;
20
    private array $errorConfig;
21
22
    private array $inputTextConfig;
23
24
    public function __construct(FieldFactoryConfig $config)
25
    {
26
        $this->template = $config->getTemplate();
27
28
        $this->setInputIdAttribute = $config->getSetInputIdAttribute();
29
30
        $this->labelConfig = $config->getLabelConfig();
31
        $this->hintConfig = $config->getHintConfig();
32
        $this->errorConfig = $config->getErrorConfig();
33
34
        $this->inputTextConfig = $config->getInputTextConfig();
35
    }
36
37
    public function label(FormModelInterface $formModel, string $attribute): Label
38
    {
39
        return Label::widget($this->makeLabelConfig())->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\Base\AbstractField or Yiisoft\Form\Field\Part\Error or Yiisoft\Form\Field\Part\Hint. ( Ignorable by Annotation )

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

39
        return Label::widget($this->makeLabelConfig())->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
40
    }
41
42
    public function hint(FormModelInterface $formModel, string $attribute): Hint
43
    {
44
        return Hint::widget($this->hintConfig)->attribute($formModel, $attribute);
45
    }
46
47
    public function error(FormModelInterface $formModel, string $attribute): Error
48
    {
49
        return Error::widget($this->errorConfig)->attribute($formModel, $attribute);
50
    }
51
52
    public function inputText(FormModelInterface $formModel, string $attribute): InputText
53
    {
54
        $config = array_merge(
55
            $this->makeFieldConfig([
56
                'template()',
57
                'setInputIdAttribute()',
58
                'labelConfig()',
59
                'hintConfig()',
60
            ]),
61
            $this->inputTextConfig
62
        );
63
64
        return InputText::widget($config)->attribute($formModel, $attribute);
65
    }
66
67
    /**
68
     * @param string[] $keys
69
     */
70
    private function makeFieldConfig(array $keys): array
71
    {
72
        $config = [];
73
        foreach ($keys as $key) {
74
            switch ($key) {
75
                case 'template()':
76
                    if ($this->template !== null) {
77
                        $config[$key] = [$this->template];
78
                    }
79
                    break;
80
81
                case 'setInputIdAttribute()':
82
                    if ($this->setInputIdAttribute !== null) {
83
                        $config[$key] = [$this->setInputIdAttribute];
84
                    }
85
                    break;
86
87
                case 'labelConfig()':
88
                    $labelConfig = $this->makeLabelConfig();
89
                    if ($labelConfig !== []) {
90
                        $config[$key] = [$labelConfig];
91
                    }
92
                    break;
93
94
                case 'hintConfig()':
95
                    if ($this->hintConfig !== []) {
96
                        $config[$key] = [$this->hintConfig];
97
                    }
98
                    break;
99
100
                case 'errorConfig()':
101
                    if ($this->errorConfig !== []) {
102
                        $config[$key] = [$this->errorConfig];
103
                    }
104
                    break;
105
            }
106
        }
107
        return $config;
108
    }
109
110
    private function makeLabelConfig(): array
111
    {
112
        $config = [];
113
114
        if ($this->setInputIdAttribute !== null) {
115
            $config['useInputIdAttribute()'] = [$this->setInputIdAttribute];
116
        }
117
118
        return array_merge($config, $this->labelConfig);
119
    }
120
}
121