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

ThemeContainer::enrichmentValidationRules()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Closure;
8
use Yiisoft\Form\Field\Base\BaseField;
9
use Yiisoft\Form\Field\Base\InputData\InputDataInterface;
10
11
use function array_key_exists;
12
13
final class ThemeContainer
14
{
15
    /**
16
     * @psalm-var array<string,array>
17
     */
18
    private static array $configs = [];
19
20
    private static ?string $defaultConfig = null;
21
22
    /**
23
     * @psalm-var array<string,Theme|null>
24
     */
25
    private static array $themes = [];
26
27
    /**
28
     * @psalm-var array<class-string, array<array-key,Closure>>
29
     */
30
    private static array $validationRulesEnrichmenters = [];
31
32
    /**
33
     * @param array<string,array> $configs Array of configurations with {@see Theme::__construct()}
34
     * arguments indexed by name. For example:
35
     * ```php
36
     * [
37
     *     'default' => [
38
     *         'containerClass' => 'formField',
39
     *     ],
40
     *     'bulma' => [
41
     *         'containerClass' => 'field',
42
     *         'inputClass' => 'input',
43
     *         'invalidClass' => 'has-background-danger',
44
     *         'validClass' => 'has-background-success',
45
     *         'template' => "{label}<div class=\"control\">\n{input}</div>\n{hint}\n{error}",
46
     *         'labelClass' => 'label',
47
     *         'errorClass' => 'has-text-danger is-italic',
48
     *         'hintClass' => 'help',
49
     *     ],
50
     *     'bootstrap5' => [
51
     *         'containerClass' => 'mb-3',
52
     *         'invalidClass' => 'is-invalid',
53
     *         'errorClass' => 'text-danger fst-italic',
54
     *         'hintClass' => 'form-text',
55
     *         'inputClass' => 'form-control',
56
     *         'labelClass' => 'form-label',
57
     *         'validClass' => 'is-valid',
58
     *     ],
59
     * ]
60
     * ```
61
     * @param string|null $defaultConfig Configuration name that will be used for create fields by default.
62
     * @param string[] $validationRulesEnrichmenters
63
     *
64
     * @psalm-param array<class-string, array<array-key,Closure>> $validationRulesEnrichmenters
65
     */
66 686
    public static function initialize(
67
        array $configs = [],
68
        ?string $defaultConfig = null,
69
        array $validationRulesEnrichmenters = [],
70
    ): void {
71 686
        self::$configs = $configs;
72 686
        self::$defaultConfig = $defaultConfig;
73 686
        self::$themes = [];
74
75 686
        self::$validationRulesEnrichmenters = [];
76 686
        foreach ($validationRulesEnrichmenters as $inputDataClass => $file) {
77
            /**
78
             * @psalm-suppress UnresolvableInclude
79
             * @psalm-suppress MixedPropertyTypeCoercion
80
             */
81 306
            self::$validationRulesEnrichmenters[$inputDataClass] = require $file;
82
        }
83
    }
84
85 676
    public static function getTheme(?string $name = null): ?Theme
86
    {
87 676
        $name ??= self::$defaultConfig;
88 676
        if ($name === null) {
89 633
            return null;
90
        }
91
92 44
        if (!array_key_exists($name, self::$themes)) {
93
            /** @psalm-suppress MixedArgument */
94 44
            self::$themes[$name] = array_key_exists($name, self::$configs)
95 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

95
                ? new Theme(/** @scrutinizer ignore-type */ ...self::$configs[$name])
Loading history...
96
                : null;
97
        }
98
99 44
        return self::$themes[$name];
100
    }
101
102 45
    public static function enrichmentValidationRules(BaseField $field, InputDataInterface $inputData): void
103
    {
104 45
        $enrichmenters = self::$validationRulesEnrichmenters[$inputData::class] ?? null;
105 45
        if ($enrichmenters === null) {
106
            return;
107
        }
108
109 45
        foreach ($enrichmenters as $key => $closure) {
110 45
            if (is_int($key) || $key === $field::class) {
111
                /** @psalm-suppress PossiblyNullFunctionCall */
112 45
                $closure->bindTo($field, $field::class)($inputData->getValidationRules());
113
            }
114
        }
115
    }
116
}
117