Test Failed
Pull Request — master (#244)
by Sergei
03:19
created

ThemeContainer::getTheme()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
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
9
use function array_key_exists;
10
11
final class ThemeContainer
12
{
13
    /**
14
     * @psalm-var array<string,array>
15
     */
16
    private static array $configs = [];
17
18
    private static ?string $defaultConfig = null;
19
20
    /**
21
     * @psalm-var array<string,Theme|null>
22
     */
23
    private static array $themes = [];
24
25
    /**
26
     * @param array<string,array> $configs Array of configurations with {@see Theme::__construct()}
27
     * arguments indexed by name. For example:
28
     * ```php
29
     * [
30
     *     'default' => [
31
     *         'containerClass' => 'formField',
32
     *     ],
33
     *     'bulma' => [
34
     *         'containerClass' => 'field',
35
     *         'inputClass' => 'input',
36
     *         'invalidClass' => 'has-background-danger',
37
     *         'validClass' => 'has-background-success',
38
     *         'template' => "{label}<div class=\"control\">\n{input}</div>\n{hint}\n{error}",
39
     *         'labelClass' => 'label',
40
     *         'errorClass' => 'has-text-danger is-italic',
41
     *         'hintClass' => 'help',
42
     *     ],
43
     *     'bootstrap5' => [
44
     *         'containerClass' => 'mb-3',
45
     *         'invalidClass' => 'is-invalid',
46
     *         'errorClass' => 'text-danger fst-italic',
47
     *         'hintClass' => 'form-text',
48
     *         'inputClass' => 'form-control',
49
     *         'labelClass' => 'form-label',
50
     *         'validClass' => 'is-valid',
51
     *     ],
52
     * ]
53
     * ```
54
     * @param string|null $defaultConfig Configuration name that will be used for create fields by default.
55
     */
56
    public static function initialize(array $configs = [], ?string $defaultConfig = null): void
57
    {
58
        self::$configs = $configs;
59
        self::$defaultConfig = $defaultConfig;
60
        self::$themes = [];
61
    }
62
63
    public static function getTheme(?string $name = null): ?Theme
64
    {
65
        $name ??= self::$defaultConfig;
66
        if ($name === null) {
67
            return null;
68
        }
69
70
        if (!array_key_exists($name, self::$themes)) {
71
            /** @psalm-suppress MixedArgument */
72
            self::$themes[$name] = array_key_exists($name, self::$configs)
73
                ? new Theme(...self::$configs[$name])
0 ignored issues
show
Bug introduced by
self::configs[$name] is expanded, but the parameter $containerTag of Yiisoft\Form\Theme::__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

73
                ? new Theme(/** @scrutinizer ignore-type */ ...self::$configs[$name])
Loading history...
74
                : null;
75
        }
76
77
        return self::$themes[$name];
78
    }
79
}
80