ThemeContainer::getTheme()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Form\Field\Base\BaseField;
8
use Yiisoft\Form\Field\Base\InputData\InputDataInterface;
9
10
use function array_key_exists;
11
12
final class ThemeContainer
13
{
14
    /**
15
     * @psalm-var array<string,array>
16
     */
17
    private static array $configs = [];
18
19
    private static ?string $defaultConfig = null;
20
21
    /**
22
     * @psalm-var array<string,Theme|null>
23
     */
24
    private static array $themes = [];
25
26
    private static ?ValidationRulesEnricherInterface $validationRulesEnricher = null;
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|null $defaultConfig Configuration name that will be used for create fields by default.
58
     */
59 877
    public static function initialize(
60
        array $configs = [],
61
        ?string $defaultConfig = null,
62
        ?ValidationRulesEnricherInterface $validationRulesEnricher = null,
63
    ): void {
64 877
        self::$configs = $configs;
65 877
        self::$defaultConfig = $defaultConfig;
66 877
        self::$themes = [];
67 877
        self::$validationRulesEnricher = $validationRulesEnricher;
68
    }
69
70 862
    public static function getTheme(?string $name = null): ?Theme
71
    {
72 862
        $name ??= self::$defaultConfig;
73 862
        if ($name === null) {
74 765
            return null;
75
        }
76
77 146
        if (!array_key_exists($name, self::$themes)) {
78
            /** @psalm-suppress MixedArgument */
79 146
            self::$themes[$name] = array_key_exists($name, self::$configs)
80 146
                ? 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

80
                ? new Theme(/** @scrutinizer ignore-type */ ...self::$configs[$name])
Loading history...
81 1
                : null;
82
        }
83
84 146
        return self::$themes[$name];
85
    }
86
87 11
    public static function getEnrichment(BaseField $field, InputDataInterface $inputData): array
88
    {
89 11
        return self::$validationRulesEnricher?->process($field, $inputData->getValidationRules()) ?? [];
90
    }
91
}
92