Passed
Pull Request — master (#283)
by Sergei
03:13
created

ThemeContainer::getEnrichment()   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
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 ?ValidationRulesEnrichmenterInterface $validationRulesEnrichmenter = 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 686
    public static function initialize(
60
        array $configs = [],
61
        ?string $defaultConfig = null,
62
        ?ValidationRulesEnrichmenterInterface $validationRulesEnrichmenter = null,
63
    ): void
64
    {
65 686
        self::$configs = $configs;
66 686
        self::$defaultConfig = $defaultConfig;
67 686
        self::$themes = [];
68 686
        self::$validationRulesEnrichmenter = $validationRulesEnrichmenter;
69
    }
70
71 676
    public static function getTheme(?string $name = null): ?Theme
72
    {
73 676
        $name ??= self::$defaultConfig;
74 676
        if ($name === null) {
75 633
            return null;
76
        }
77
78 44
        if (!array_key_exists($name, self::$themes)) {
79
            /** @psalm-suppress MixedArgument */
80 44
            self::$themes[$name] = array_key_exists($name, self::$configs)
81 44
                ? 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

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