Passed
Pull Request — master (#192)
by Alexander
02:53
created

Field::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use RuntimeException;
8
use Yiisoft\Form\Field\Checkbox;
9
use Yiisoft\Form\Field\Date;
10
use Yiisoft\Form\Field\DateTimeLocal;
11
use Yiisoft\Form\Field\Email;
12
use Yiisoft\Form\Field\Hidden;
13
use Yiisoft\Form\Field\Number;
14
use Yiisoft\Form\Field\Part\Error;
15
use Yiisoft\Form\Field\Part\Hint;
16
use Yiisoft\Form\Field\Part\Label;
17
use Yiisoft\Form\Field\Password;
18
use Yiisoft\Form\Field\Telephone;
19
use Yiisoft\Form\Field\Text;
20
use Yiisoft\Form\Field\Textarea;
21
use Yiisoft\Form\Field\Url;
22
23
use function array_key_exists;
24
25
final class Field
26
{
27
    /**
28
     * @psalm-var array<string,array>
29
     */
30
    private static array $configs = [
31
        'default' => [],
32
    ];
33
34
    private static string $defaultConfigName = 'default';
35
36
    /**
37
     * @psalm-var array<string,FieldFactory>
38
     */
39
    private static array $factories = [];
40
41
    /**
42
     * @psalm-param array<string,array> $configs
43
     */
44
    public static function initialize(array $configs = [], string $defaultConfigName = 'default'): void
45
    {
46
        self::$configs = array_merge(self::$configs, $configs);
47
        self::$defaultConfigName = $defaultConfigName;
48
    }
49
50 1
    public static function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox
51
    {
52 1
        return self::getFactory()->checkbox($formModel, $attribute, $config);
53
    }
54
55 1
    public static function date(FormModelInterface $formModel, string $attribute, array $config = []): Date
56
    {
57 1
        return self::getFactory()->date($formModel, $attribute, $config);
58
    }
59
60 1
    public static function dateTimeLocal(
61
        FormModelInterface $formModel,
62
        string $attribute,
63
        array $config = []
64
    ): DateTimeLocal {
65 1
        return self::getFactory()->dateTimeLocal($formModel, $attribute, $config);
66
    }
67
68 1
    public static function email(FormModelInterface $formModel, string $attribute, array $config = []): Email
69
    {
70 1
        return self::getFactory()->email($formModel, $attribute, $config);
71
    }
72
73 1
    public static function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden
74
    {
75 1
        return self::getFactory()->hidden($formModel, $attribute, $config);
76
    }
77
78 1
    public static function number(FormModelInterface $formModel, string $attribute, array $config = []): Number
79
    {
80 1
        return self::getFactory()->number($formModel, $attribute, $config);
81
    }
82
83 1
    public static function password(FormModelInterface $formModel, string $attribute, array $config = []): Password
84
    {
85 1
        return self::getFactory()->password($formModel, $attribute, $config);
86
    }
87
88 1
    public static function telephone(FormModelInterface $formModel, string $attribute, array $config = []): Telephone
89
    {
90 1
        return self::getFactory()->telephone($formModel, $attribute, $config);
91
    }
92
93 1
    public static function text(FormModelInterface $formModel, string $attribute, array $config = []): Text
94
    {
95 1
        return self::getFactory()->text($formModel, $attribute, $config);
96
    }
97
98 1
    public static function textarea(FormModelInterface $formModel, string $attribute, array $config = []): Textarea
99
    {
100 1
        return self::getFactory()->textarea($formModel, $attribute, $config);
101
    }
102
103 1
    public static function url(FormModelInterface $formModel, string $attribute, array $config = []): Url
104
    {
105 1
        return self::getFactory()->url($formModel, $attribute, $config);
106
    }
107
108 1
    public static function label(FormModelInterface $formModel, string $attribute, array $config = []): Label
109
    {
110 1
        return self::getFactory()->label($formModel, $attribute, $config);
111
    }
112
113 1
    public static function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
114
    {
115 1
        return self::getFactory()->hint($formModel, $attribute, $config);
116
    }
117
118 1
    public static function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
119
    {
120 1
        return self::getFactory()->error($formModel, $attribute, $config);
121
    }
122
123
    /**
124
     * @psalm-template T
125
     * @psalm-param class-string<T> $class
126
     * @psalm-return T
127
     */
128
    public function field(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object
129
    {
130
        return self::getFactory()->field($class, $formModel, $attribute, $config);
131
    }
132
133 14
    public static function getFactory(?string $name = null): FieldFactory
134
    {
135 14
        $name = $name ?? self::$defaultConfigName;
136
137 14
        if (!array_key_exists($name, self::$factories)) {
138 1
            if (!array_key_exists($name, self::$configs)) {
139
                throw new RuntimeException(
140
                    sprintf('Configuration with name "%s" not found.', $name)
141
                );
142
            }
143
144
            /** @psalm-suppress MixedArgument */
145 1
            self::$factories[$name] = new FieldFactory(...self::$configs[$name]);
0 ignored issues
show
Bug introduced by
self::configs[$name] is expanded, but the parameter $containerTag of Yiisoft\Form\FieldFactory::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

145
            self::$factories[$name] = new FieldFactory(/** @scrutinizer ignore-type */ ...self::$configs[$name]);
Loading history...
146
        }
147
148 14
        return self::$factories[$name];
149
    }
150
}
151