Passed
Pull Request — master (#192)
by Alexander
04:59 queued 02:28
created

Field::hidden()   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 3
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 RuntimeException;
8
use Yiisoft\Form\Field\Hidden;
9
use Yiisoft\Form\Field\Part\Error;
10
use Yiisoft\Form\Field\Part\Hint;
11
use Yiisoft\Form\Field\Part\Label;
12
use Yiisoft\Form\Field\Text;
13
14
use function array_key_exists;
15
16
final class Field
17
{
18
    /**
19
     * @psalm-var array<string,array>
20
     */
21
    private static array $configs = [
22
        'default' => [],
23
    ];
24
25
    private static string $defaultConfigName = 'default';
26
27
    /**
28
     * @psalm-var array<string,FieldFactory>
29
     */
30
    private static array $factories = [];
31
32
    /**
33
     * @psalm-param array<string,array> $configs
34
     */
35
    public static function initialize(array $configs = [], string $defaultConfigName = 'default'): void
36
    {
37
        self::$configs = array_merge(self::$configs, $configs);
38
        self::$defaultConfigName = $defaultConfigName;
39
    }
40
41 1
    public static function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden
42
    {
43 1
        return self::getFactory()->hidden($formModel, $attribute, $config);
44
    }
45
46 1
    public static function text(FormModelInterface $formModel, string $attribute, array $config = []): Text
47
    {
48 1
        return self::getFactory()->text($formModel, $attribute, $config);
49
    }
50
51 1
    public static function label(FormModelInterface $formModel, string $attribute, array $config = []): Label
52
    {
53 1
        return self::getFactory()->label($formModel, $attribute, $config);
54
    }
55
56 1
    public static function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
57
    {
58 1
        return self::getFactory()->hint($formModel, $attribute, $config);
59
    }
60
61 1
    public static function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
62
    {
63 1
        return self::getFactory()->error($formModel, $attribute, $config);
64
    }
65
66
    /**
67
     * @psalm-template T
68
     * @psalm-param class-string<T> $class
69
     * @psalm-return T
70
     */
71
    public function field(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object
72
    {
73
        return self::getFactory()->field($class, $formModel, $attribute, $config);
74
    }
75
76 5
    public static function getFactory(?string $name = null): FieldFactory
77
    {
78 5
        $name = $name ?? self::$defaultConfigName;
79 5
        if ($name === null) {
0 ignored issues
show
introduced by
The condition $name === null is always false.
Loading history...
80
            if (self::$defaultConfigName === null) {
81
                $name = array_key_first(self::$configs);
82
                if ($name === null) {
83
                    throw new RuntimeException('Not found default configuration of fields.');
84
                }
85
            } else {
86
                $name = self::$defaultConfigName;
87
            }
88
        }
89
90 5
        if (!array_key_exists($name, self::$factories)) {
91 1
            if (!array_key_exists($name, self::$configs)) {
92
                throw new RuntimeException(
93
                    sprintf('Configuration with name "%s" not found.', $name)
94
                );
95
            }
96
97
            /** @psalm-suppress MixedArgument */
98 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

98
            self::$factories[$name] = new FieldFactory(/** @scrutinizer ignore-type */ ...self::$configs[$name]);
Loading history...
99
        }
100
101 5
        return self::$factories[$name];
102
    }
103
}
104