Test Failed
Pull Request — master (#244)
by Alexander
05:33 queued 02:46
created

ThemeDispatcher::getTheme()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

78
            self::$themes[$name] = new Theme(/** @scrutinizer ignore-type */ ...self::$themeConfigs[$name]);
Loading history...
79
        }
80
81
        return self::$themes[$name];
82
    }
83
}
84