Test Failed
Pull Request — master (#244)
by Sergei
13:14
created

ThemeContainer::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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